pdf-log-entry.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 { sendMB } from '@/infrastructure/event-tracking'
  6. import getMeta from '@/utils/meta'
  7. import { ErrorLevel, LogEntry, SourceLocation } from '../util/types'
  8. import { useIsNewEditorEnabled } from '@/features/ide-redesign/utils/new-editor-utils'
  9. import NewLogEntry from '@/features/ide-redesign/components/error-logs/log-entry'
  10. import { useFeatureFlag } from '@/shared/context/split-test-context'
  11. function PdfLogEntry({
  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. ruleId?: string
  32. headerIcon?: React.ReactElement
  33. rawContent?: string
  34. logType?: string
  35. formattedContent?: React.ReactNode
  36. extraInfoURL?: string | null
  37. sourceLocation?: SourceLocation
  38. showSourceLocationLink?: boolean
  39. entryAriaLabel?: string
  40. contentDetails?: string[]
  41. onSourceLocationClick?: (sourceLocation: SourceLocation) => void
  42. index?: number
  43. logEntry?: LogEntry
  44. id?: string
  45. }) {
  46. const showAiErrorAssistant = getMeta('ol-showAiErrorAssistant')
  47. if (ruleId && HumanReadableLogsHints[ruleId]) {
  48. const hint = HumanReadableLogsHints[ruleId]
  49. formattedContent = hint.formattedContent(contentDetails)
  50. extraInfoURL = hint.extraInfoURL
  51. }
  52. const handleLogEntryLinkClick: MouseEventHandler<HTMLButtonElement> =
  53. useCallback(
  54. event => {
  55. event.preventDefault()
  56. if (onSourceLocationClick && sourceLocation) {
  57. onSourceLocationClick(sourceLocation)
  58. const parts = sourceLocation?.file?.split('.')
  59. const extension =
  60. parts?.length && parts?.length > 1 ? parts.pop() : ''
  61. sendMB('log-entry-link-click', { level, ruleId, extension })
  62. }
  63. },
  64. [level, onSourceLocationClick, ruleId, sourceLocation]
  65. )
  66. const newEditor = useIsNewEditorEnabled()
  67. const newErrorlogs = useFeatureFlag('new-editor-error-logs-redesign')
  68. if (newEditor && newErrorlogs) {
  69. return (
  70. <NewLogEntry
  71. index={index}
  72. id={id}
  73. logEntry={logEntry}
  74. ruleId={ruleId}
  75. headerTitle={headerTitle}
  76. formattedContent={formattedContent}
  77. rawContent={rawContent}
  78. logType={logType}
  79. level={level}
  80. contentDetails={contentDetails}
  81. entryAriaLabel={entryAriaLabel}
  82. sourceLocation={sourceLocation}
  83. onSourceLocationClick={onSourceLocationClick}
  84. showSourceLocationLink={showSourceLocationLink}
  85. extraInfoURL={extraInfoURL}
  86. />
  87. )
  88. }
  89. return (
  90. <div
  91. className="log-entry"
  92. aria-label={entryAriaLabel}
  93. data-ruleid={ruleId}
  94. data-log-entry-id={id}
  95. >
  96. <PreviewLogEntryHeader
  97. level={level}
  98. sourceLocation={sourceLocation}
  99. headerTitle={headerTitle}
  100. headerIcon={headerIcon}
  101. logType={logType}
  102. showSourceLocationLink={showSourceLocationLink}
  103. onSourceLocationClick={handleLogEntryLinkClick}
  104. />
  105. {(rawContent || formattedContent || showAiErrorAssistant) && (
  106. <PdfLogEntryContent
  107. rawContent={rawContent}
  108. formattedContent={formattedContent}
  109. extraInfoURL={extraInfoURL}
  110. index={index}
  111. logEntry={logEntry}
  112. />
  113. )}
  114. </div>
  115. )
  116. }
  117. export default memo(PdfLogEntry)