pdf-log-entry-raw-content.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. export default function PdfLogEntryRawContent({
  8. rawContent,
  9. collapsedSize = 0,
  10. alwaysExpanded = false,
  11. }: {
  12. rawContent: string
  13. collapsedSize?: number
  14. alwaysExpanded?: boolean
  15. }) {
  16. const [expanded, setExpanded] = useState(alwaysExpanded)
  17. const [needsExpander, setNeedsExpander] = useState(!alwaysExpanded)
  18. const { elementRef } = useResizeObserver(
  19. useCallback(
  20. (element: Element) => {
  21. if (element.scrollHeight === 0) return // skip update when logs-pane is closed
  22. setNeedsExpander(
  23. !alwaysExpanded && element.scrollHeight > collapsedSize
  24. )
  25. },
  26. [collapsedSize, alwaysExpanded]
  27. )
  28. )
  29. const { t } = useTranslation()
  30. return (
  31. <div className="log-entry-content-raw-container">
  32. <div
  33. className="expand-collapse-container"
  34. style={{
  35. height: expanded || !needsExpander ? 'auto' : collapsedSize,
  36. }}
  37. >
  38. <pre className="log-entry-content-raw" ref={elementRef} translate="no">
  39. {rawContent.trim()}
  40. </pre>
  41. </div>
  42. {needsExpander && (
  43. <div
  44. className={classNames('log-entry-content-button-container', {
  45. 'log-entry-content-button-container-collapsed': !expanded,
  46. })}
  47. >
  48. <OLButton
  49. variant="secondary"
  50. size="sm"
  51. onClick={() => setExpanded(value => !value)}
  52. >
  53. {expanded ? (
  54. <>
  55. <Icon type="angle-up" /> {t('collapse')}
  56. </>
  57. ) : (
  58. <>
  59. <Icon type="angle-down" /> {t('expand')}
  60. </>
  61. )}
  62. </OLButton>
  63. </div>
  64. )}
  65. </div>
  66. )
  67. }