import { useCallback, useMemo } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { isFulfilled } from '@reduxjs/toolkit'; import { hasSpecialCharacters, inputToHashtag, } from '@/mastodon/utils/hashtags'; import type { ApiCreateCollectionPayload, ApiUpdateCollectionPayload, } from 'mastodon/api_types/collections'; import { Button } from 'mastodon/components/button'; import { CheckboxField, Fieldset, FormStack, RadioButtonField, TextAreaField, } from 'mastodon/components/form_fields'; import { TextInputField } from 'mastodon/components/form_fields/text_input_field'; import { createCollection, updateCollection, updateCollectionEditorField, } from 'mastodon/reducers/slices/collections'; import { useAppDispatch, useAppSelector } from 'mastodon/store'; import classes from './styles.module.scss'; import { WizardStepHeader } from './wizard_step_header'; export const CollectionDetails: React.FC = () => { const intl = useIntl(); const dispatch = useAppDispatch(); const history = useHistory(); const { id, name, description, topic, discoverable, sensitive, accountIds } = useAppSelector((state) => state.collections.editor); const handleNameChange = useCallback( (event: React.ChangeEvent) => { dispatch( updateCollectionEditorField({ field: 'name', value: event.target.value, }), ); }, [dispatch], ); const handleDescriptionChange = useCallback( (event: React.ChangeEvent) => { dispatch( updateCollectionEditorField({ field: 'description', value: event.target.value, }), ); }, [dispatch], ); const handleTopicChange = useCallback( (event: React.ChangeEvent) => { dispatch( updateCollectionEditorField({ field: 'topic', value: inputToHashtag(event.target.value), }), ); }, [dispatch], ); const handleDiscoverableChange = useCallback( (event: React.ChangeEvent) => { dispatch( updateCollectionEditorField({ field: 'discoverable', value: event.target.value === 'public', }), ); }, [dispatch], ); const handleSensitiveChange = useCallback( (event: React.ChangeEvent) => { dispatch( updateCollectionEditorField({ field: 'sensitive', value: event.target.checked, }), ); }, [dispatch], ); const handleSubmit = useCallback( (e: React.FormEvent) => { e.preventDefault(); if (id) { const payload: ApiUpdateCollectionPayload = { id, name, description, tag_name: topic || null, discoverable, sensitive, }; void dispatch(updateCollection({ payload })).then(() => { history.goBack(); }); } else { const payload: ApiCreateCollectionPayload = { name, description, discoverable, sensitive, account_ids: accountIds, }; if (topic) { payload.tag_name = topic; } void dispatch( createCollection({ payload, }), ).then((result) => { if (isFulfilled(result)) { history.replace(`/collections`); history.push(`/collections/${result.payload.collection.id}`, { newCollection: true, }); } }); } }, [ id, name, description, topic, discoverable, sensitive, dispatch, history, accountIds, ], ); const topicHasSpecialCharacters = useMemo( () => hasSpecialCharacters(topic), [topic], ); return (
{!id && ( } /> )} } hint={ } value={name} onChange={handleNameChange} maxLength={40} /> } hint={ } value={description} onChange={handleDescriptionChange} maxLength={100} /> } hint={ } value={topic} onChange={handleTopicChange} autoCapitalize='off' autoCorrect='off' spellCheck='false' maxLength={40} status={ topicHasSpecialCharacters ? { variant: 'warning', message: intl.formatMessage({ id: 'collections.topic_special_chars_hint', defaultMessage: 'Special characters will be removed when saving', }), } : undefined } />
} > } hint={ } value='public' checked={discoverable} onChange={handleDiscoverableChange} /> } hint={ } value='unlisted' checked={!discoverable} onChange={handleDiscoverableChange} />
} > } hint={ } checked={sensitive} onChange={handleSensitiveChange} />
); };