log-entry-header.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import classNames from 'classnames'
  2. import { useState, useRef, MouseEventHandler, ElementType } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import OLTooltip from '@/features/ui/components/ol/ol-tooltip'
  5. import {
  6. ErrorLevel,
  7. SourceLocation,
  8. LogEntry as LogEntryData,
  9. } from '@/features/pdf-preview/util/types'
  10. import useResizeObserver from '@/features/preview/hooks/use-resize-observer'
  11. import OLIconButton from '@/features/ui/components/ol/ol-icon-button'
  12. import importOverleafModules from '../../../../../macros/import-overleaf-module.macro'
  13. import MaterialIcon from '@/shared/components/material-icon'
  14. const actionComponents = importOverleafModules(
  15. 'pdfLogEntryHeaderActionComponents'
  16. ) as {
  17. import: { default: ElementType }
  18. path: string
  19. }[]
  20. function LogEntryHeader({
  21. sourceLocation,
  22. level,
  23. headerTitle,
  24. logType,
  25. showSourceLocationLink = true,
  26. onSourceLocationClick,
  27. collapsed,
  28. onToggleCollapsed,
  29. id,
  30. logEntry,
  31. actionButtonsOverride,
  32. openCollapseIconOverride,
  33. }: {
  34. headerTitle: string | React.ReactNode
  35. level: ErrorLevel
  36. logType?: string
  37. sourceLocation?: SourceLocation
  38. showSourceLocationLink?: boolean
  39. onSourceLocationClick?: MouseEventHandler<HTMLButtonElement>
  40. collapsed: boolean
  41. onToggleCollapsed: () => void
  42. id?: string
  43. logEntry?: LogEntryData
  44. actionButtonsOverride?: React.ReactNode
  45. openCollapseIconOverride?: string
  46. }) {
  47. const { t } = useTranslation()
  48. const logLocationSpanRef = useRef<HTMLSpanElement>(null)
  49. const [locationSpanOverflown, setLocationSpanOverflown] = useState(false)
  50. useResizeObserver(
  51. logLocationSpanRef,
  52. locationSpanOverflown,
  53. checkLocationSpanOverflow
  54. )
  55. const file = sourceLocation ? sourceLocation.file : null
  56. const line = sourceLocation ? sourceLocation.line : null
  57. const logEntryHeaderTextClasses = classNames('log-entry-header-text', {
  58. 'log-entry-header-text-error': level === 'error',
  59. 'log-entry-header-text-warning': level === 'warning',
  60. 'log-entry-header-text-info': level === 'info' || level === 'typesetting',
  61. 'log-entry-header-text-success': level === 'success',
  62. 'log-entry-header-text-raw': level === 'raw',
  63. })
  64. function checkLocationSpanOverflow(observedElement: ResizeObserverEntry) {
  65. const spanEl = observedElement.target
  66. const isOverflowing = spanEl.scrollWidth > spanEl.clientWidth
  67. setLocationSpanOverflown(isOverflowing)
  68. }
  69. const locationText =
  70. showSourceLocationLink && file ? `${file}${line ? `, ${line}` : ''}` : null
  71. // Because we want an ellipsis on the left-hand side (e.g. "...longfilename.tex"), the
  72. // `log-entry-location` class has text laid out from right-to-left using the CSS
  73. // rule `direction: rtl;`.
  74. // This works most of the times, except when the first character of the filename is considered
  75. // a punctuation mark, like `/` (e.g. `/foo/bar/baz.sty`). In this case, because of
  76. // right-to-left writing rules, the punctuation mark is moved to the right-side of the string,
  77. // resulting in `...bar/baz.sty/` instead of `...bar/baz.sty`.
  78. // To avoid this edge-case, we wrap the `logLocationLinkText` in two directional formatting
  79. // characters:
  80. // * \u202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
  81. // * \u202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.
  82. // This essentially tells the browser that, althought the text is laid out from right-to-left,
  83. // the wrapped portion of text should follow left-to-right writing rules.
  84. const formattedLocationText = locationText ? (
  85. <span ref={logLocationSpanRef} className="log-entry-location">
  86. {`\u202A${locationText}\u202C`}
  87. </span>
  88. ) : null
  89. const headerTitleText = logType ? `${logType} ${headerTitle}` : headerTitle
  90. return (
  91. <header className="log-entry-header-card">
  92. <OLTooltip
  93. id={`expand-collapse-${locationText}`}
  94. description={collapsed ? t('expand') : t('collapse')}
  95. overlayProps={{ placement: 'bottom' }}
  96. >
  97. <button
  98. data-action="expand-collapse"
  99. data-collapsed={collapsed}
  100. className="log-entry-header-button"
  101. onClick={onToggleCollapsed}
  102. aria-label={collapsed ? t('expand') : t('collapse')}
  103. >
  104. <MaterialIcon
  105. type={
  106. openCollapseIconOverride ??
  107. (collapsed ? 'chevron_right' : 'expand_more')
  108. }
  109. />
  110. <div className="log-entry-header-content">
  111. <h3 className={logEntryHeaderTextClasses}>{headerTitleText}</h3>
  112. {locationSpanOverflown && formattedLocationText && locationText ? (
  113. <OLTooltip
  114. id={locationText}
  115. description={locationText}
  116. overlayProps={{ placement: 'left' }}
  117. tooltipProps={{ className: 'log-location-tooltip' }}
  118. >
  119. {formattedLocationText}
  120. </OLTooltip>
  121. ) : (
  122. formattedLocationText
  123. )}
  124. </div>
  125. </button>
  126. </OLTooltip>
  127. {actionButtonsOverride ?? (
  128. <div className="log-entry-header-actions">
  129. {showSourceLocationLink && (
  130. <OLTooltip
  131. id={`go-to-location-${locationText}`}
  132. description={t('go_to_code_location')}
  133. overlayProps={{ placement: 'bottom' }}
  134. >
  135. <OLIconButton
  136. onClick={onSourceLocationClick}
  137. variant="ghost"
  138. icon="my_location"
  139. accessibilityLabel={t('go_to_code_location')}
  140. />
  141. </OLTooltip>
  142. )}
  143. {actionComponents.map(({ import: { default: Component }, path }) => (
  144. <Component key={path} logEntry={logEntry} id={id} />
  145. ))}
  146. </div>
  147. )}
  148. </header>
  149. )
  150. }
  151. export default LogEntryHeader