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