preview-log-entry-header.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import classNames from 'classnames'
  2. import { useState, useRef, MouseEventHandler } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import useResizeObserver from '../hooks/use-resize-observer'
  5. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  6. import OLButton from '@/features/ui/components/ol/ol-button'
  7. import MaterialIcon from '@/shared/components/material-icon'
  8. import { ErrorLevel, SourceLocation } from '@/features/pdf-preview/util/types'
  9. function PreviewLogEntryHeader({
  10. sourceLocation,
  11. level,
  12. headerTitle,
  13. headerIcon,
  14. logType,
  15. showSourceLocationLink = true,
  16. showCloseButton = false,
  17. onSourceLocationClick,
  18. onClose,
  19. }: {
  20. headerTitle: string | React.ReactNode
  21. level: ErrorLevel
  22. headerIcon?: React.ReactElement
  23. logType?: string
  24. sourceLocation?: SourceLocation
  25. showSourceLocationLink?: boolean
  26. showCloseButton?: boolean
  27. onSourceLocationClick?: MouseEventHandler<HTMLButtonElement>
  28. onClose?: () => void
  29. }) {
  30. const { t } = useTranslation()
  31. const logLocationSpanRef = useRef<HTMLSpanElement>(null)
  32. const [locationSpanOverflown, setLocationSpanOverflown] = useState(false)
  33. useResizeObserver(
  34. logLocationSpanRef,
  35. locationSpanOverflown,
  36. checkLocationSpanOverflow
  37. )
  38. const file = sourceLocation ? sourceLocation.file : null
  39. const line = sourceLocation ? sourceLocation.line : null
  40. const logEntryHeaderClasses = classNames('log-entry-header', {
  41. 'log-entry-header-error': level === 'error',
  42. 'log-entry-header-warning': level === 'warning',
  43. 'log-entry-header-info': level === 'info',
  44. 'log-entry-header-typesetting': level === 'typesetting',
  45. 'log-entry-header-raw': level === 'raw',
  46. 'log-entry-header-success': level === 'success',
  47. })
  48. const logEntryLocationBtnClasses = classNames('log-entry-header-link', {
  49. 'log-entry-header-link-error': level === 'error',
  50. 'log-entry-header-link-warning': level === 'warning',
  51. 'log-entry-header-link-typesetting': level === 'typesetting',
  52. 'log-entry-header-link-raw': level === 'raw',
  53. 'log-entry-header-link-info': level === 'info',
  54. 'log-entry-header-link-success': level === 'success',
  55. })
  56. const headerLogLocationTitle = t('navigate_log_source', {
  57. location: file + (line ? `, ${line}` : ''),
  58. })
  59. function checkLocationSpanOverflow(observedElement: ResizeObserverEntry) {
  60. const spanEl = observedElement.target
  61. const isOverflowing = spanEl.scrollWidth > spanEl.clientWidth
  62. setLocationSpanOverflown(isOverflowing)
  63. }
  64. const locationLinkText =
  65. showSourceLocationLink && file ? `${file}${line ? `, ${line}` : ''}` : null
  66. // Because we want an ellipsis on the left-hand side (e.g. "...longfilename.tex"), the
  67. // `log-entry-header-link-location` class has text laid out from right-to-left using the CSS
  68. // rule `direction: rtl;`.
  69. // This works most of the times, except when the first character of the filename is considered
  70. // a punctuation mark, like `/` (e.g. `/foo/bar/baz.sty`). In this case, because of
  71. // right-to-left writing rules, the punctuation mark is moved to the right-side of the string,
  72. // resulting in `...bar/baz.sty/` instead of `...bar/baz.sty`.
  73. // To avoid this edge-case, we wrap the `logLocationLinkText` in two directional formatting
  74. // characters:
  75. // * \u202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
  76. // * \u202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.
  77. // This essentially tells the browser that, althought the text is laid out from right-to-left,
  78. // the wrapped portion of text should follow left-to-right writing rules.
  79. const locationLink = locationLinkText ? (
  80. <OLButton
  81. variant="ghost"
  82. className={logEntryLocationBtnClasses}
  83. aria-label={headerLogLocationTitle}
  84. onClick={onSourceLocationClick}
  85. >
  86. <MaterialIcon type="link" />
  87. <span ref={logLocationSpanRef} className="log-entry-header-link-location">
  88. {`\u202A${locationLinkText}\u202C`}
  89. </span>
  90. </OLButton>
  91. ) : null
  92. const headerTitleText = logType ? `${logType} ${headerTitle}` : headerTitle
  93. return (
  94. <header className={logEntryHeaderClasses}>
  95. {headerIcon ? (
  96. <div className="log-entry-header-icon-container">{headerIcon}</div>
  97. ) : null}
  98. <h3 className="log-entry-header-title">{headerTitleText}</h3>
  99. {locationSpanOverflown && locationLinkText && locationLink ? (
  100. <OLTooltip
  101. id={locationLinkText}
  102. description={locationLinkText}
  103. overlayProps={{ placement: 'left' }}
  104. tooltipProps={{ className: 'log-location-tooltip' }}
  105. >
  106. {locationLink}
  107. </OLTooltip>
  108. ) : (
  109. locationLink
  110. )}
  111. {showCloseButton ? (
  112. <OLButton
  113. variant="link"
  114. className="btn-inline-link log-entry-header-link"
  115. aria-label={t('dismiss_error_popup')}
  116. onClick={onClose}
  117. >
  118. <span aria-hidden="true">&times;</span>
  119. </OLButton>
  120. ) : null}
  121. </header>
  122. )
  123. }
  124. export default PreviewLogEntryHeader