import type { ChangeEventHandler, KeyboardEventHandler } from 'react'; import { useState, useRef, useCallback, useId } from 'react'; import { defineMessages, useIntl, FormattedMessage } from 'react-intl'; import Textarea from 'react-textarea-autosize'; import { submitAccountNote } from '@/mastodon/actions/account_notes'; import { LoadingIndicator } from '@/mastodon/components/loading_indicator'; import { useAppDispatch, useAppSelector } from '@/mastodon/store'; const messages = defineMessages({ placeholder: { id: 'account_note.placeholder', defaultMessage: 'Click to add a note', }, }); const AccountNoteUI: React.FC<{ initialValue: string | undefined; onSubmit: (newNote: string) => void; wasSaved: boolean; }> = ({ initialValue, onSubmit, wasSaved }) => { const intl = useIntl(); const uniqueId = useId(); const [value, setValue] = useState(initialValue ?? ''); const isLoading = initialValue === undefined; const canSubmitOnBlurRef = useRef(true); const handleChange = useCallback>( (e) => { setValue(e.target.value); }, [], ); const handleKeyDown = useCallback>( (e) => { if (e.key === 'Escape') { e.preventDefault(); setValue(initialValue ?? ''); canSubmitOnBlurRef.current = false; e.currentTarget.blur(); } else if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); onSubmit(value); canSubmitOnBlurRef.current = false; e.currentTarget.blur(); } }, [initialValue, onSubmit, value], ); const handleBlur = useCallback(() => { if (initialValue !== value && canSubmitOnBlurRef.current) { onSubmit(value); } canSubmitOnBlurRef.current = true; }, [initialValue, onSubmit, value]); return (
{isLoading ? (
) : (