import { useCallback, useState } from 'react' import { useTranslation } from 'react-i18next' import { Alert, Button, Modal } from 'react-bootstrap' import Icon from '../../../shared/components/icon' import Tooltip from '../../../shared/components/tooltip' import useAsync from '../../../shared/hooks/use-async' import { postJSON } from '../../../infrastructure/fetch-json' import ignoredWords from '../ignored-words' import { debugConsole } from '@/utils/debugging' type DictionaryModalContentProps = { handleHide: () => void } const wordsSortFunction = (a: string, b: string) => a.localeCompare(b) export default function DictionaryModalContent({ handleHide, }: DictionaryModalContentProps) { const { t } = useTranslation() const [learnedWords, setLearnedWords] = useState(ignoredWords.learnedWords) const { isError, runAsync } = useAsync() const handleRemove = useCallback( word => { runAsync( postJSON('/spelling/unlearn', { body: { word, }, }) ) .then(() => { ignoredWords.remove(word) setLearnedWords(new Set(ignoredWords.learnedWords)) }) .catch(debugConsole.error) }, [runAsync] ) return ( <> {t('edit_dictionary')} {isError ? ( {t('generic_something_went_wrong')} ) : null} {learnedWords?.size > 0 ? ( {[...learnedWords].sort(wordsSortFunction).map(learnedWord => ( {learnedWord} handleRemove(learnedWord)} > ))} ) : ( {t('edit_dictionary_empty')} )} {t('close')} > ) }
{t('edit_dictionary_empty')}