pdf-log-entry-raw-content.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { 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(false)
  14. const { elementRef } = useResizeObserver(element => {
  15. setNeedsExpander(element.scrollHeight > collapsedSize)
  16. })
  17. const { t } = useTranslation()
  18. return (
  19. <div className="log-entry-content-raw-container">
  20. <div
  21. className="expand-collapse-container"
  22. style={{
  23. height: expanded || !needsExpander ? 'auto' : collapsedSize,
  24. }}
  25. >
  26. <pre className="log-entry-content-raw" ref={elementRef}>
  27. {rawContent.trim()}
  28. </pre>
  29. </div>
  30. {needsExpander && (
  31. <div
  32. className={classNames('log-entry-content-button-container', {
  33. 'log-entry-content-button-container-collapsed': !expanded,
  34. })}
  35. >
  36. <Button
  37. bsSize="xs"
  38. bsStyle={null}
  39. className="log-entry-btn-expand-collapse btn-secondary"
  40. onClick={() => setExpanded(value => !value)}
  41. >
  42. {expanded ? (
  43. <>
  44. <Icon type="angle-up" /> {t('collapse')}
  45. </>
  46. ) : (
  47. <>
  48. <Icon type="angle-down" /> {t('expand')}
  49. </>
  50. )}
  51. </Button>
  52. </div>
  53. )}
  54. </div>
  55. )
  56. }
  57. PdfLogEntryRawContent.propTypes = {
  58. rawContent: PropTypes.string.isRequired,
  59. collapsedSize: PropTypes.number,
  60. }