2024-02-20 10:39:59 -05:00
|
|
|
import { useCallback, useState } from 'react'
|
2022-05-24 07:21:25 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import useAsync from '../../../shared/hooks/use-async'
|
|
|
|
import { postJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import ignoredWords from '../ignored-words'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-10-11 05:22:38 -04:00
|
|
|
import {
|
|
|
|
OLModalBody,
|
|
|
|
OLModalFooter,
|
|
|
|
OLModalHeader,
|
|
|
|
OLModalTitle,
|
|
|
|
} from '@/features/ui/components/ol/ol-modal'
|
|
|
|
import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
|
|
|
|
import OLNotification from '@/features/ui/components/ol/ol-notification'
|
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
|
|
|
import OLIconButton from '@/features/ui/components/ol/ol-icon-button'
|
|
|
|
import { bsVersion } from '@/features/utils/bootstrap-5'
|
2022-05-24 07:21:25 -04:00
|
|
|
|
|
|
|
type DictionaryModalContentProps = {
|
|
|
|
handleHide: () => void
|
|
|
|
}
|
|
|
|
|
2022-05-31 05:08:49 -04:00
|
|
|
const wordsSortFunction = (a: string, b: string) => a.localeCompare(b)
|
2022-05-26 08:58:10 -04:00
|
|
|
|
2022-05-24 07:21:25 -04:00
|
|
|
export default function DictionaryModalContent({
|
|
|
|
handleHide,
|
|
|
|
}: DictionaryModalContentProps) {
|
|
|
|
const { t } = useTranslation()
|
2024-02-20 10:39:59 -05:00
|
|
|
const [learnedWords, setLearnedWords] = useState(ignoredWords.learnedWords)
|
2022-05-24 07:21:25 -04:00
|
|
|
|
|
|
|
const { isError, runAsync } = useAsync()
|
|
|
|
|
|
|
|
const handleRemove = useCallback(
|
|
|
|
word => {
|
|
|
|
runAsync(
|
|
|
|
postJSON('/spelling/unlearn', {
|
|
|
|
body: {
|
|
|
|
word,
|
|
|
|
},
|
|
|
|
})
|
2024-02-20 10:39:59 -05:00
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
ignoredWords.remove(word)
|
|
|
|
setLearnedWords(new Set(ignoredWords.learnedWords))
|
|
|
|
})
|
|
|
|
.catch(debugConsole.error)
|
2022-05-24 07:21:25 -04:00
|
|
|
},
|
|
|
|
[runAsync]
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLModalHeader closeButton>
|
|
|
|
<OLModalTitle>{t('edit_dictionary')}</OLModalTitle>
|
|
|
|
</OLModalHeader>
|
2022-05-24 07:21:25 -04:00
|
|
|
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLModalBody>
|
2022-05-24 07:21:25 -04:00
|
|
|
{isError ? (
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLNotification
|
|
|
|
type="error"
|
|
|
|
content={t('generic_something_went_wrong')}
|
|
|
|
/>
|
2022-05-24 07:21:25 -04:00
|
|
|
) : null}
|
|
|
|
|
2024-02-20 10:39:59 -05:00
|
|
|
{learnedWords?.size > 0 ? (
|
2022-05-26 08:58:10 -04:00
|
|
|
<ul className="list-unstyled dictionary-entries-list">
|
2024-02-20 10:39:59 -05:00
|
|
|
{[...learnedWords].sort(wordsSortFunction).map(learnedWord => (
|
|
|
|
<li key={learnedWord} className="dictionary-entry">
|
|
|
|
<span className="dictionary-entry-name">{learnedWord}</span>
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLTooltip
|
2024-02-20 10:39:59 -05:00
|
|
|
id={`tooltip-remove-learned-word-${learnedWord}`}
|
|
|
|
description={t('edit_dictionary_remove')}
|
|
|
|
overlayProps={{ delay: 0 }}
|
|
|
|
>
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLIconButton
|
|
|
|
variant="danger"
|
|
|
|
size="sm"
|
2024-02-20 10:39:59 -05:00
|
|
|
onClick={() => handleRemove(learnedWord)}
|
2024-10-11 05:22:38 -04:00
|
|
|
bs3Props={{ bsSize: 'xsmall' }}
|
|
|
|
icon={
|
|
|
|
bsVersion({
|
|
|
|
bs5: 'delete',
|
|
|
|
bs3: 'trash-o',
|
|
|
|
}) as string
|
|
|
|
}
|
|
|
|
accessibilityLabel={t('edit_dictionary_remove')}
|
|
|
|
/>
|
|
|
|
</OLTooltip>
|
2024-02-20 10:39:59 -05:00
|
|
|
</li>
|
|
|
|
))}
|
2022-05-24 07:21:25 -04:00
|
|
|
</ul>
|
|
|
|
) : (
|
2022-05-30 06:19:41 -04:00
|
|
|
<p className="dictionary-empty-body text-center">
|
|
|
|
<i>{t('edit_dictionary_empty')}</i>
|
|
|
|
</p>
|
2022-05-24 07:21:25 -04:00
|
|
|
)}
|
2024-10-11 05:22:38 -04:00
|
|
|
</OLModalBody>
|
2022-05-24 07:21:25 -04:00
|
|
|
|
2024-10-11 05:22:38 -04:00
|
|
|
<OLModalFooter>
|
|
|
|
<OLButton variant="secondary" onClick={handleHide}>
|
2024-04-04 10:14:09 -04:00
|
|
|
{t('close')}
|
2024-10-11 05:22:38 -04:00
|
|
|
</OLButton>
|
|
|
|
</OLModalFooter>
|
2022-05-24 07:21:25 -04:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|