import { useCallback, useMemo, useState } from 'react'; import type { FC } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import type { Map as ImmutableMap } from 'immutable'; import { Button } from '@/mastodon/components/button'; import { Callout } from '@/mastodon/components/callout'; import { EmojiTextInputField } from '@/mastodon/components/form_fields'; import { removeField, selectFieldById, updateField, } from '@/mastodon/reducers/slices/profile_edit'; import { createAppSelector, useAppDispatch, useAppSelector, } from '@/mastodon/store'; import { isUrlWithoutProtocol } from '@/mastodon/utils/checks'; import { ConfirmationModal } from '../../ui/components/confirmation_modals'; import type { DialogModalProps } from '../../ui/components/dialog_modal'; import { DialogModal } from '../../ui/components/dialog_modal'; import classes from './styles.module.scss'; const messages = defineMessages({ editTitle: { id: 'account_edit.field_edit_modal.edit_title', defaultMessage: 'Edit custom field', }, addTitle: { id: 'account_edit.field_edit_modal.add_title', defaultMessage: 'Add custom field', }, editLabelField: { id: 'account_edit.field_edit_modal.name_label', defaultMessage: 'Label', }, editLabelHint: { id: 'account_edit.field_edit_modal.name_hint', defaultMessage: 'E.g. “Personal website”', }, editValueField: { id: 'account_edit.field_edit_modal.value_label', defaultMessage: 'Value', }, editValueHint: { id: 'account_edit.field_edit_modal.value_hint', defaultMessage: 'E.g. “https://example.me”', }, limitHeader: { id: 'account_edit.field_edit_modal.limit_header', defaultMessage: 'Recommended character limit exceeded', }, save: { id: 'account_edit.save', defaultMessage: 'Save', }, }); // We have two different values- the hard limit set by the server, // and the soft limit for mobile display. const selectFieldLimits = createAppSelector( [ (state) => state.server.getIn(['server', 'configuration', 'accounts']) as | ImmutableMap | undefined, ], (accounts) => ({ nameLimit: accounts?.get('profile_field_name_limit'), valueLimit: accounts?.get('profile_field_value_limit'), }), ); const RECOMMENDED_LIMIT = 40; const selectEmojiCodes = createAppSelector( [(state) => state.custom_emojis], (emojis) => emojis.map((emoji) => emoji.get('shortcode')).toArray(), ); export const EditFieldModal: FC = ({ onClose, fieldKey, }) => { const intl = useIntl(); const field = useAppSelector((state) => selectFieldById(state, fieldKey)); const [newLabel, setNewLabel] = useState(field?.name ?? ''); const [newValue, setNewValue] = useState(field?.value ?? ''); const { nameLimit, valueLimit } = useAppSelector(selectFieldLimits); const isPending = useAppSelector((state) => state.profileEdit.isPending); const disabled = !nameLimit || !valueLimit || newLabel.length > nameLimit || newValue.length > valueLimit; const customEmojiCodes = useAppSelector(selectEmojiCodes); const hasLinkAndEmoji = useMemo(() => { const text = `${newLabel} ${newValue}`; // Combine text, as we're searching it all. const hasLink = /https?:\/\//.test(text); const hasEmoji = customEmojiCodes.some((code) => text.includes(`:${code}:`), ); return hasLink && hasEmoji; }, [customEmojiCodes, newLabel, newValue]); const hasLinkWithoutProtocol = useMemo( () => isUrlWithoutProtocol(newValue), [newValue], ); const dispatch = useAppDispatch(); const handleSave = useCallback(() => { if (disabled || isPending) { return; } void dispatch( updateField({ id: fieldKey, name: newLabel, value: newValue }), ).then(onClose); }, [disabled, dispatch, fieldKey, isPending, newLabel, newValue, onClose]); return ( {hasLinkAndEmoji && ( )} {(newLabel.length > RECOMMENDED_LIMIT || newValue.length > RECOMMENDED_LIMIT) && ( )} {hasLinkWithoutProtocol && ( https://, }} /> )} ); }; export const DeleteFieldModal: FC = ({ onClose, fieldKey, }) => { const isPending = useAppSelector((state) => state.profileEdit.isPending); const dispatch = useAppDispatch(); const handleDelete = useCallback(() => { void dispatch(removeField({ key: fieldKey })).then(onClose); }, [dispatch, fieldKey, onClose]); return ( } buttons={ } > ); };