2022-05-26 08:58:10 -04:00
|
|
|
import { useCallback, useMemo } from 'react'
|
2022-05-24 07:21:25 -04:00
|
|
|
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'
|
2022-05-26 08:58:10 -04:00
|
|
|
import BetaBadge from '../../../shared/components/beta-badge'
|
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()
|
|
|
|
|
|
|
|
const { isError, runAsync } = useAsync()
|
|
|
|
|
|
|
|
const handleRemove = useCallback(
|
|
|
|
word => {
|
|
|
|
ignoredWords.remove(word)
|
|
|
|
runAsync(
|
|
|
|
postJSON('/spelling/unlearn', {
|
|
|
|
body: {
|
|
|
|
word,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
).catch(console.error)
|
|
|
|
},
|
|
|
|
[runAsync]
|
|
|
|
)
|
|
|
|
|
2022-05-26 08:58:10 -04:00
|
|
|
const betaBadgeTooltipProps = useMemo(() => {
|
|
|
|
return {
|
|
|
|
id: 'dictionary-modal-tooltip',
|
|
|
|
placement: 'bottom',
|
|
|
|
className: 'tooltip-wide',
|
|
|
|
text: (
|
|
|
|
<>
|
2022-06-22 05:34:42 -04:00
|
|
|
We are testing the dictionary manager.
|
2022-05-26 08:58:10 -04:00
|
|
|
<br />
|
|
|
|
Click to give feedback
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
2022-05-24 07:21:25 -04:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Modal.Header closeButton>
|
2022-05-26 08:58:10 -04:00
|
|
|
<Modal.Title>
|
|
|
|
{t('edit_dictionary')}{' '}
|
|
|
|
<BetaBadge
|
|
|
|
tooltip={betaBadgeTooltipProps}
|
|
|
|
url="https://forms.gle/8cLBEW6HU9mDKBPX9"
|
2022-06-22 05:34:42 -04:00
|
|
|
phase="release"
|
2022-05-26 08:58:10 -04:00
|
|
|
/>
|
|
|
|
</Modal.Title>
|
2022-05-24 07:21:25 -04:00
|
|
|
</Modal.Header>
|
|
|
|
|
|
|
|
<Modal.Body>
|
|
|
|
{isError ? (
|
|
|
|
<Alert bsStyle="danger">{t('generic_something_went_wrong')}</Alert>
|
|
|
|
) : null}
|
|
|
|
|
|
|
|
{ignoredWords.learnedWords?.size > 0 ? (
|
2022-05-26 08:58:10 -04:00
|
|
|
<ul className="list-unstyled dictionary-entries-list">
|
|
|
|
{[...ignoredWords.learnedWords]
|
|
|
|
.sort(wordsSortFunction)
|
|
|
|
.map(learnedWord => (
|
|
|
|
<li key={learnedWord} className="dictionary-entry">
|
|
|
|
<span className="dictionary-entry-name">{learnedWord}</span>
|
|
|
|
<Tooltip
|
|
|
|
id={`tooltip-remove-learned-word-${learnedWord}`}
|
|
|
|
description={t('edit_dictionary_remove')}
|
|
|
|
overlayProps={{ delay: 0 }}
|
2022-05-24 07:21:25 -04:00
|
|
|
>
|
2022-05-26 08:58:10 -04:00
|
|
|
<Button
|
|
|
|
bsStyle="danger"
|
|
|
|
bsSize="xs"
|
|
|
|
onClick={() => handleRemove(learnedWord)}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
type="trash-o"
|
|
|
|
accessibilityLabel={t('edit_dictionary_remove')}
|
|
|
|
/>
|
|
|
|
</Button>
|
|
|
|
</Tooltip>
|
|
|
|
</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
|
|
|
)}
|
|
|
|
</Modal.Body>
|
|
|
|
|
|
|
|
<Modal.Footer>
|
|
|
|
<Button onClick={handleHide}>{t('done')}</Button>
|
|
|
|
</Modal.Footer>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|