import debounce from 'lodash/debounce'; import React, { useEffect, useRef, useState } from 'react'; import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; import Textarea from 'react-textarea-autosize'; import { submitAccountNote } from 'soapbox/actions/account-notes'; import { HStack, Text, Widget } from 'soapbox/components/ui'; import { useAppDispatch } from 'soapbox/hooks'; import type { AppDispatch } from 'soapbox/store'; import type { Account as AccountEntity } from 'soapbox/types/entities'; const onSave = debounce( (dispatch: AppDispatch, id: string, value: string, callback: () => void) => dispatch(submitAccountNote(id, value)).then(() => callback()), 900, ); const messages = defineMessages({ placeholder: { id: 'account_note.placeholder', defaultMessage: 'Click to add a note' }, saved: { id: 'generic.saved', defaultMessage: 'Saved' }, }); interface IAccountNotePanel { account: AccountEntity } const AccountNotePanel: React.FC = ({ account }) => { const intl = useIntl(); const dispatch = useAppDispatch(); const textarea = useRef(null); const [value, setValue] = useState(account.relationship?.note); const [saved, setSaved] = useState(false); const handleChange: React.ChangeEventHandler = e => { setValue(e.target.value); onSave(dispatch, account.id, e.target.value, () => { setSaved(true); setTimeout(() => setSaved(false), 2000); }); }; useEffect(() => { setValue(account.relationship?.note); }, [account.relationship?.note]); if (!account) { return null; } return ( {saved && ( )} } >