pdf-log-entry.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { memo, MouseEventHandler, useCallback } from 'react'
  2. import PreviewLogEntryHeader from '../../preview/components/preview-log-entry-header'
  3. import PdfLogEntryContent from './pdf-log-entry-content'
  4. import HumanReadableLogsHints from '../../../ide/human-readable-logs/HumanReadableLogsHints'
  5. import getMeta from '@/utils/meta'
  6. import { ErrorLevel, LogEntry, SourceLocation } from '../util/types'
  7. import { useAreNewErrorLogsEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  8. import NewLogEntry from '@/features/ide-redesign/components/error-logs/log-entry'
  9. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  10. function PdfLogEntry({
  11. autoExpand,
  12. ruleId,
  13. headerTitle,
  14. headerIcon,
  15. rawContent,
  16. logType,
  17. formattedContent,
  18. extraInfoURL,
  19. level,
  20. sourceLocation,
  21. showSourceLocationLink = true,
  22. entryAriaLabel = undefined,
  23. contentDetails,
  24. onSourceLocationClick,
  25. index,
  26. logEntry,
  27. id,
  28. }: {
  29. headerTitle: string | React.ReactNode
  30. level: ErrorLevel
  31. autoExpand?: boolean
  32. ruleId?: string
  33. headerIcon?: React.ReactElement
  34. rawContent?: string
  35. logType?: string
  36. formattedContent?: React.ReactNode
  37. extraInfoURL?: string | null
  38. sourceLocation?: SourceLocation
  39. showSourceLocationLink?: boolean
  40. entryAriaLabel?: string
  41. contentDetails?: string[]
  42. onSourceLocationClick?: (sourceLocation: SourceLocation) => void
  43. index?: number
  44. logEntry?: LogEntry
  45. id?: string
  46. }) {
  47. const showAiErrorAssistant = getMeta('ol-showAiErrorAssistant')
  48. const { sendEvent } = useEditorAnalytics()
  49. if (ruleId && HumanReadableLogsHints[ruleId]) {
  50. const hint = HumanReadableLogsHints[ruleId]
  51. formattedContent = hint.formattedContent(contentDetails)
  52. extraInfoURL = hint.extraInfoURL
  53. }
  54. const handleLogEntryLinkClick: MouseEventHandler<HTMLButtonElement> =
  55. useCallback(
  56. event => {
  57. event.preventDefault()
  58. if (onSourceLocationClick && sourceLocation) {
  59. onSourceLocationClick(sourceLocation)
  60. const parts = sourceLocation?.file?.split('.')
  61. const extension =
  62. parts?.length && parts?.length > 1 ? parts.pop() : ''
  63. sendEvent('log-entry-link-click', { level, ruleId, extension })
  64. }
  65. },
  66. [level, onSourceLocationClick, ruleId, sourceLocation, sendEvent]
  67. )
  68. const newErrorlogs = useAreNewErrorLogsEnabled()
  69. if (newErrorlogs) {
  70. return (
  71. <NewLogEntry
  72. autoExpand={autoExpand}
  73. index={index}
  74. id={id}
  75. logEntry={logEntry}
  76. ruleId={ruleId}
  77. headerTitle={headerTitle}
  78. formattedContent={formattedContent}
  79. rawContent={rawContent}
  80. logType={logType}
  81. level={level}
  82. contentDetails={contentDetails}
  83. entryAriaLabel={entryAriaLabel}
  84. sourceLocation={sourceLocation}
  85. onSourceLocationClick={handleLogEntryLinkClick}
  86. showSourceLocationLink={showSourceLocationLink}
  87. extraInfoURL={extraInfoURL}
  88. />
  89. )
  90. }
  91. return (
  92. <div
  93. className="log-entry"
  94. aria-label={entryAriaLabel}
  95. data-ruleid={ruleId}
  96. data-log-entry-id={id}
  97. >
  98. <PreviewLogEntryHeader
  99. level={level}
  100. sourceLocation={sourceLocation}
  101. headerTitle={headerTitle}
  102. headerIcon={headerIcon}
  103. logType={logType}
  104. showSourceLocationLink={showSourceLocationLink}
  105. onSourceLocationClick={handleLogEntryLinkClick}
  106. />
  107. {(rawContent || formattedContent || showAiErrorAssistant) && (
  108. <PdfLogEntryContent
  109. rawContent={rawContent}
  110. formattedContent={formattedContent}
  111. extraInfoURL={extraInfoURL}
  112. index={index}
  113. logEntry={logEntry}
  114. />
  115. )}
  116. </div>
  117. )
  118. }
  119. export default memo(PdfLogEntry)