word-counts-client.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 '@/shared/components/ol/ol-notification'
  6. import usePersistedState from '@/shared/hooks/use-persisted-state'
  7. const numberFormat = new Intl.NumberFormat()
  8. export const WordCountsClient: FC<{ data: WordCountData }> = ({ data }) => {
  9. const { t } = useTranslation()
  10. const [included, setIncluded] = usePersistedState<string[]>(
  11. 'word-count-total',
  12. ['text']
  13. )
  14. const items = useMemo(() => {
  15. return [
  16. {
  17. key: 'text',
  18. label: t('main_text'),
  19. words: data.textWords,
  20. chars: data.textCharacters,
  21. },
  22. {
  23. key: 'headers',
  24. label: t('headers'),
  25. words: data.headWords,
  26. chars: data.headCharacters,
  27. },
  28. {
  29. key: 'abstract',
  30. label: t('abstract'),
  31. words: data.abstractWords,
  32. chars: data.abstractCharacters,
  33. },
  34. {
  35. key: 'captions',
  36. label: t('captions'),
  37. words: data.captionWords,
  38. chars: data.captionCharacters,
  39. },
  40. {
  41. key: 'footnotes',
  42. label: t('footnotes'),
  43. words: data.footnoteWords,
  44. chars: data.footnoteCharacters,
  45. },
  46. {
  47. key: 'other',
  48. label: t('other'),
  49. words: data.otherWords,
  50. chars: data.otherCharacters,
  51. },
  52. ]
  53. }, [data, t])
  54. const totals = useMemo(() => {
  55. const totals = {
  56. words: 0,
  57. chars: 0,
  58. }
  59. for (const item of items) {
  60. if (included.includes(item.key)) {
  61. totals.words += item.words
  62. totals.chars += item.chars
  63. }
  64. }
  65. return totals
  66. }, [included, items])
  67. return (
  68. <Container fluid>
  69. {data.messages && (
  70. <Row>
  71. <Col xs={12}>
  72. <OLNotification
  73. type="error"
  74. content={
  75. <p style={{ whiteSpace: 'pre-wrap' }}>{data.messages}</p>
  76. }
  77. />
  78. </Col>
  79. </Row>
  80. )}
  81. <Row className="mb-4">
  82. <table style={{ width: 'auto' }}>
  83. <thead>
  84. <tr>
  85. <th />
  86. <th className="visually-hidden">{t('words')}</th>
  87. <th className="visually-hidden">{t('characters')}</th>
  88. </tr>
  89. </thead>
  90. <tbody>
  91. <tr>
  92. <th style={{ width: 100 }}>{t('total_words_lower')}:</th>
  93. <td style={{ width: 100, textAlign: 'right' }}>
  94. {numberFormat.format(totals.words)}
  95. </td>
  96. <td style={{ width: 250, textAlign: 'right' }}>
  97. <b style={{ marginRight: 10 }} aria-hidden="true">
  98. {t('characters')}:
  99. </b>{' '}
  100. {numberFormat.format(totals.chars)}
  101. </td>
  102. </tr>
  103. {items.map(item => (
  104. <tr key={item.key}>
  105. <th style={{ fontWeight: 'normal', paddingLeft: 20 }}>
  106. <Form.Check
  107. type="checkbox"
  108. id={`word-count-${item.key}`}
  109. label={`${item.label}:`}
  110. checked={included.includes(item.key)}
  111. onChange={event =>
  112. setIncluded(prevValue => {
  113. return event.target.checked
  114. ? prevValue.concat(item.key)
  115. : prevValue.filter(key => key !== item.key)
  116. })
  117. }
  118. aria-label={`Include ${item.label} in total`}
  119. />
  120. </th>
  121. <td style={{ textAlign: 'right' }}>
  122. {numberFormat.format(item.words)}
  123. </td>
  124. <td style={{ textAlign: 'right' }}>
  125. {numberFormat.format(item.chars)}
  126. </td>
  127. </tr>
  128. ))}
  129. </tbody>
  130. </table>
  131. </Row>
  132. <Row className="border-top py-2">
  133. <Col xs={12}>
  134. <b>{t('headers')}:</b> {data.headers}
  135. </Col>
  136. </Row>
  137. <Row className="border-top py-2">
  138. <Col xs={12}>
  139. <b>{t('inline_math')}:</b> {data.mathInline}
  140. </Col>
  141. </Row>
  142. <Row className="border-top py-2 pb-0">
  143. <Col xs={12}>
  144. <b>{t('display_math')}:</b> {data.mathDisplay}
  145. </Col>
  146. </Row>
  147. </Container>
  148. )
  149. }