dictionary-modal-content.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import { Alert, Button, Modal } from 'react-bootstrap'
  4. import Icon from '../../../shared/components/icon'
  5. import SplitTestBadge from '../../../shared/components/split-test-badge'
  6. import Tooltip from '../../../shared/components/tooltip'
  7. import useAsync from '../../../shared/hooks/use-async'
  8. import { postJSON } from '../../../infrastructure/fetch-json'
  9. import ignoredWords from '../ignored-words'
  10. type DictionaryModalContentProps = {
  11. handleHide: () => void
  12. }
  13. const wordsSortFunction = (a: string, b: string) => a.localeCompare(b)
  14. export default function DictionaryModalContent({
  15. handleHide,
  16. }: DictionaryModalContentProps) {
  17. const { t } = useTranslation()
  18. const { isError, runAsync } = useAsync()
  19. const handleRemove = useCallback(
  20. word => {
  21. ignoredWords.remove(word)
  22. runAsync(
  23. postJSON('/spelling/unlearn', {
  24. body: {
  25. word,
  26. },
  27. })
  28. ).catch(console.error)
  29. },
  30. [runAsync]
  31. )
  32. return (
  33. <>
  34. <Modal.Header closeButton>
  35. <Modal.Title>
  36. {t('edit_dictionary')}{' '}
  37. <SplitTestBadge
  38. splitTestName="dictionary-editor"
  39. displayOnVariants={['enabled']}
  40. tooltip={{
  41. placement: 'bottom',
  42. className: 'tooltip-wide',
  43. }}
  44. />
  45. </Modal.Title>
  46. </Modal.Header>
  47. <Modal.Body>
  48. {isError ? (
  49. <Alert bsStyle="danger">{t('generic_something_went_wrong')}</Alert>
  50. ) : null}
  51. {ignoredWords.learnedWords?.size > 0 ? (
  52. <ul className="list-unstyled dictionary-entries-list">
  53. {[...ignoredWords.learnedWords]
  54. .sort(wordsSortFunction)
  55. .map(learnedWord => (
  56. <li key={learnedWord} className="dictionary-entry">
  57. <span className="dictionary-entry-name">{learnedWord}</span>
  58. <Tooltip
  59. id={`tooltip-remove-learned-word-${learnedWord}`}
  60. description={t('edit_dictionary_remove')}
  61. overlayProps={{ delay: 0 }}
  62. >
  63. <Button
  64. bsStyle="danger"
  65. bsSize="xs"
  66. onClick={() => handleRemove(learnedWord)}
  67. >
  68. <Icon
  69. type="trash-o"
  70. accessibilityLabel={t('edit_dictionary_remove')}
  71. />
  72. </Button>
  73. </Tooltip>
  74. </li>
  75. ))}
  76. </ul>
  77. ) : (
  78. <p className="dictionary-empty-body text-center">
  79. <i>{t('edit_dictionary_empty')}</i>
  80. </p>
  81. )}
  82. </Modal.Body>
  83. <Modal.Footer>
  84. <Button onClick={handleHide}>{t('done')}</Button>
  85. </Modal.Footer>
  86. </>
  87. )
  88. }