log-entry-header.tsx 6.4 KB

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