pdf-log-entry-raw-content.jsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { useCallback, useState } from 'react'
  2. import { useResizeObserver } from '../../../shared/hooks/use-resize-observer'
  3. import { useTranslation } from 'react-i18next'
  4. import classNames from 'classnames'
  5. import OLButton from '@/features/ui/components/ol/ol-button'
  6. import Icon from '../../../shared/components/icon'
  7. import PropTypes from 'prop-types'
  8. export default function PdfLogEntryRawContent({
  9. rawContent,
  10. collapsedSize = 0,
  11. }) {
  12. const [expanded, setExpanded] = useState(false)
  13. const [needsExpander, setNeedsExpander] = useState(true)
  14. const { elementRef } = useResizeObserver(
  15. useCallback(
  16. element => {
  17. if (element.scrollHeight === 0) return // skip update when logs-pane is closed
  18. setNeedsExpander(element.scrollHeight > collapsedSize)
  19. },
  20. [collapsedSize]
  21. )
  22. )
  23. const { t } = useTranslation()
  24. return (
  25. <div className="log-entry-content-raw-container">
  26. <div
  27. className="expand-collapse-container"
  28. style={{
  29. height: expanded || !needsExpander ? 'auto' : collapsedSize,
  30. }}
  31. >
  32. <pre className="log-entry-content-raw" ref={elementRef}>
  33. {rawContent.trim()}
  34. </pre>
  35. </div>
  36. {needsExpander && (
  37. <div
  38. className={classNames('log-entry-content-button-container', {
  39. 'log-entry-content-button-container-collapsed': !expanded,
  40. })}
  41. >
  42. <OLButton
  43. variant="secondary"
  44. size="sm"
  45. bs3Props={{
  46. bsSize: 'xsmall',
  47. className: 'log-entry-btn-expand-collapse',
  48. }}
  49. onClick={() => setExpanded(value => !value)}
  50. >
  51. {expanded ? (
  52. <>
  53. <Icon type="angle-up" /> {t('collapse')}
  54. </>
  55. ) : (
  56. <>
  57. <Icon type="angle-down" /> {t('expand')}
  58. </>
  59. )}
  60. </OLButton>
  61. </div>
  62. )}
  63. </div>
  64. )
  65. }
  66. PdfLogEntryRawContent.propTypes = {
  67. rawContent: PropTypes.string.isRequired,
  68. collapsedSize: PropTypes.number,
  69. }