pdf-log-entry.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import PropTypes from 'prop-types'
  2. import classNames from 'classnames'
  3. import { memo, useCallback } from 'react'
  4. import PreviewLogEntryHeader from '../../preview/components/preview-log-entry-header'
  5. import PdfLogEntryContent from './pdf-log-entry-content'
  6. import HumanReadableLogsHints from '../../../ide/human-readable-logs/HumanReadableLogsHints'
  7. import { sendMB } from '@/infrastructure/event-tracking'
  8. import getMeta from '@/utils/meta'
  9. function PdfLogEntry({
  10. ruleId,
  11. headerTitle,
  12. headerIcon,
  13. rawContent,
  14. logType,
  15. formattedContent,
  16. extraInfoURL,
  17. level,
  18. sourceLocation,
  19. showSourceLocationLink = true,
  20. showCloseButton = false,
  21. entryAriaLabel = null,
  22. customClass,
  23. contentDetails,
  24. onSourceLocationClick,
  25. onClose,
  26. index,
  27. logEntry,
  28. id,
  29. }) {
  30. const showAiErrorAssistant = getMeta('ol-showAiErrorAssistant')
  31. if (ruleId && HumanReadableLogsHints[ruleId]) {
  32. const hint = HumanReadableLogsHints[ruleId]
  33. formattedContent = hint.formattedContent(contentDetails)
  34. extraInfoURL = hint.extraInfoURL
  35. }
  36. const handleLogEntryLinkClick = useCallback(
  37. event => {
  38. event.preventDefault()
  39. onSourceLocationClick(sourceLocation)
  40. const parts = sourceLocation?.file?.split('.')
  41. const extension = parts?.length > 1 ? parts.pop() : ''
  42. sendMB('log-entry-link-click', { level, ruleId, extension })
  43. },
  44. [level, onSourceLocationClick, ruleId, sourceLocation]
  45. )
  46. const logEntryRef = useCallback(
  47. element => {
  48. if (element) {
  49. window.addEventListener('editor:view-compile-log-entry', event => {
  50. if (event.detail.id === id) {
  51. element.scrollIntoView({ block: 'start', inline: 'nearest' })
  52. if (event.detail.suggestFix) {
  53. element
  54. .querySelector('button[data-action="suggest-fix"]')
  55. ?.click()
  56. }
  57. }
  58. })
  59. }
  60. },
  61. [id]
  62. )
  63. return (
  64. <div
  65. className={classNames('log-entry', customClass)}
  66. aria-label={entryAriaLabel}
  67. data-ruleid={ruleId}
  68. ref={logEntryRef}
  69. >
  70. <PreviewLogEntryHeader
  71. level={level}
  72. sourceLocation={sourceLocation}
  73. headerTitle={headerTitle}
  74. headerIcon={headerIcon}
  75. logType={logType}
  76. showSourceLocationLink={showSourceLocationLink}
  77. onSourceLocationClick={handleLogEntryLinkClick}
  78. showCloseButton={showCloseButton}
  79. onClose={onClose}
  80. />
  81. {(rawContent || formattedContent || showAiErrorAssistant) && (
  82. <PdfLogEntryContent
  83. rawContent={rawContent}
  84. formattedContent={formattedContent}
  85. extraInfoURL={extraInfoURL}
  86. index={index}
  87. logEntry={logEntry}
  88. />
  89. )}
  90. </div>
  91. )
  92. }
  93. PdfLogEntry.propTypes = {
  94. ruleId: PropTypes.string,
  95. sourceLocation: PreviewLogEntryHeader.propTypes.sourceLocation,
  96. headerTitle: PreviewLogEntryHeader.propTypes.headerTitle,
  97. headerIcon: PropTypes.element,
  98. rawContent: PropTypes.string,
  99. logType: PropTypes.string,
  100. formattedContent: PropTypes.node,
  101. extraInfoURL: PropTypes.string,
  102. level: PropTypes.oneOf([
  103. 'error',
  104. 'warning',
  105. 'info',
  106. 'typesetting',
  107. 'raw',
  108. 'success',
  109. ]).isRequired,
  110. customClass: PropTypes.string,
  111. showSourceLocationLink: PropTypes.bool,
  112. showCloseButton: PropTypes.bool,
  113. entryAriaLabel: PropTypes.string,
  114. contentDetails: PropTypes.arrayOf(PropTypes.string),
  115. onSourceLocationClick: PropTypes.func,
  116. onClose: PropTypes.func,
  117. index: PropTypes.number,
  118. logEntry: PropTypes.any,
  119. id: PropTypes.string,
  120. }
  121. export default memo(PdfLogEntry)