pdf-log-entry-raw-content.jsx 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 { Button } from 'react-bootstrap'
  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. <Button
  43. bsSize="xs"
  44. bsStyle={null}
  45. className="log-entry-btn-expand-collapse btn-secondary"
  46. onClick={() => setExpanded(value => !value)}
  47. >
  48. {expanded ? (
  49. <>
  50. <Icon type="angle-up" /> {t('collapse')}
  51. </>
  52. ) : (
  53. <>
  54. <Icon type="angle-down" /> {t('expand')}
  55. </>
  56. )}
  57. </Button>
  58. </div>
  59. )}
  60. </div>
  61. )
  62. }
  63. PdfLogEntryRawContent.propTypes = {
  64. rawContent: PropTypes.string.isRequired,
  65. collapsedSize: PropTypes.number,
  66. }