pdf-log-entry.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import classNames from 'classnames'
  2. import { memo, MouseEventHandler, useCallback } from 'react'
  3. import PreviewLogEntryHeader from '../../preview/components/preview-log-entry-header'
  4. import PdfLogEntryContent from './pdf-log-entry-content'
  5. import HumanReadableLogsHints from '../../../ide/human-readable-logs/HumanReadableLogsHints'
  6. import { sendMB } from '@/infrastructure/event-tracking'
  7. import getMeta from '@/utils/meta'
  8. import { ErrorLevel, LogEntry, SourceLocation } from '../util/types'
  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 = undefined,
  22. customClass,
  23. contentDetails,
  24. onSourceLocationClick,
  25. onClose,
  26. index,
  27. logEntry,
  28. id,
  29. }: {
  30. headerTitle: string | React.ReactNode
  31. level: ErrorLevel
  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. showCloseButton?: boolean
  41. entryAriaLabel?: string
  42. customClass?: string
  43. contentDetails?: string[]
  44. onSourceLocationClick?: (sourceLocation: SourceLocation) => void
  45. onClose?: () => void
  46. index?: number
  47. logEntry?: LogEntry
  48. id?: string
  49. }) {
  50. const showAiErrorAssistant = getMeta('ol-showAiErrorAssistant')
  51. if (ruleId && HumanReadableLogsHints[ruleId]) {
  52. const hint = HumanReadableLogsHints[ruleId]
  53. formattedContent = hint.formattedContent(contentDetails)
  54. extraInfoURL = hint.extraInfoURL
  55. }
  56. const handleLogEntryLinkClick: MouseEventHandler<HTMLButtonElement> =
  57. useCallback(
  58. event => {
  59. event.preventDefault()
  60. if (onSourceLocationClick && sourceLocation) {
  61. onSourceLocationClick(sourceLocation)
  62. const parts = sourceLocation?.file?.split('.')
  63. const extension =
  64. parts?.length && parts?.length > 1 ? parts.pop() : ''
  65. sendMB('log-entry-link-click', { level, ruleId, extension })
  66. }
  67. },
  68. [level, onSourceLocationClick, ruleId, sourceLocation]
  69. )
  70. return (
  71. <div
  72. className={classNames('log-entry', customClass)}
  73. aria-label={entryAriaLabel}
  74. data-ruleid={ruleId}
  75. data-log-entry-id={id}
  76. >
  77. <PreviewLogEntryHeader
  78. level={level}
  79. sourceLocation={sourceLocation}
  80. headerTitle={headerTitle}
  81. headerIcon={headerIcon}
  82. logType={logType}
  83. showSourceLocationLink={showSourceLocationLink}
  84. onSourceLocationClick={handleLogEntryLinkClick}
  85. showCloseButton={showCloseButton}
  86. onClose={onClose}
  87. />
  88. {(rawContent || formattedContent || showAiErrorAssistant) && (
  89. <PdfLogEntryContent
  90. rawContent={rawContent}
  91. formattedContent={formattedContent}
  92. extraInfoURL={extraInfoURL}
  93. index={index}
  94. logEntry={logEntry}
  95. />
  96. )}
  97. </div>
  98. )
  99. }
  100. export default memo(PdfLogEntry)