word-counts-client.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { FC, useMemo } from 'react'
  2. import { WordCountData } from '@/features/word-count-modal/components/word-count-data'
  3. import { useTranslation } from 'react-i18next'
  4. import { Container, Row, Col, Form } from 'react-bootstrap'
  5. import OLNotification from '@/features/ui/components/ol/ol-notification'
  6. import usePersistedState from '@/shared/hooks/use-persisted-state'
  7. export const WordCountsClient: FC<{ data: WordCountData }> = ({ data }) => {
  8. const { t } = useTranslation()
  9. const [included, setIncluded] = usePersistedState<string[]>(
  10. 'word-count-total',
  11. ['text']
  12. )
  13. const items = useMemo(() => {
  14. return [
  15. {
  16. key: 'text',
  17. label: t('text'),
  18. words: data.textWords,
  19. chars: data.textCharacters,
  20. },
  21. {
  22. key: 'headers',
  23. label: t('headers'),
  24. words: data.headWords,
  25. chars: data.headCharacters,
  26. },
  27. {
  28. key: 'abstract',
  29. label: t('abstract'),
  30. words: data.abstractWords,
  31. chars: data.abstractCharacters,
  32. },
  33. {
  34. key: 'captions',
  35. label: t('captions'),
  36. words: data.captionWords,
  37. chars: data.captionCharacters,
  38. },
  39. {
  40. key: 'footnotes',
  41. label: t('footnotes'),
  42. words: data.footnoteWords,
  43. chars: data.footnoteCharacters,
  44. },
  45. {
  46. key: 'other',
  47. label: t('other'),
  48. words: data.otherWords,
  49. chars: data.otherCharacters,
  50. },
  51. ]
  52. }, [data, t])
  53. const totals = useMemo(() => {
  54. const totals = {
  55. words: 0,
  56. chars: 0,
  57. }
  58. for (const item of items) {
  59. if (included.includes(item.key)) {
  60. totals.words += item.words
  61. totals.chars += item.chars
  62. }
  63. }
  64. return totals
  65. }, [included, items])
  66. return (
  67. <Container fluid>
  68. {data.messages && (
  69. <Row>
  70. <Col xs={12}>
  71. <OLNotification
  72. type="error"
  73. content={
  74. <p style={{ whiteSpace: 'pre-wrap' }}>{data.messages}</p>
  75. }
  76. />
  77. </Col>
  78. </Row>
  79. )}
  80. {items.map(item => (
  81. <Row
  82. key={item.key}
  83. style={{
  84. borderBottom: '1px solid #eee',
  85. padding: 5,
  86. marginBottom: 5,
  87. }}
  88. >
  89. <Col
  90. style={{
  91. display: 'flex',
  92. alignItems: 'top',
  93. justifyContent: 'space-between',
  94. }}
  95. >
  96. <Form.Check
  97. type="checkbox"
  98. id={`word-count-${item.key}`}
  99. label={item.label}
  100. checked={included.includes(item.key)}
  101. onChange={event =>
  102. setIncluded(prevValue => {
  103. return event.target.checked
  104. ? prevValue.concat(item.key)
  105. : prevValue.filter(key => key !== item.key)
  106. })
  107. }
  108. aria-label={`Include ${item.label} in total`}
  109. />
  110. </Col>
  111. <Col>
  112. {item.words} words
  113. <br />
  114. {item.chars} chars
  115. </Col>
  116. </Row>
  117. ))}
  118. <Row>
  119. <Col style={{ textAlign: 'right' }}>
  120. <span style={{ fontWeight: 'bold' }}>
  121. {t('total')}: {totals.words} words
  122. <br />
  123. {totals.chars} chars
  124. </span>
  125. </Col>
  126. </Row>
  127. </Container>
  128. )
  129. }