pdf-log-entry.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. function PdfLogEntry({
  9. ruleId,
  10. headerTitle,
  11. headerIcon,
  12. rawContent,
  13. logType,
  14. formattedContent,
  15. extraInfoURL,
  16. level,
  17. sourceLocation,
  18. showSourceLocationLink = true,
  19. showCloseButton = false,
  20. entryAriaLabel = null,
  21. customClass,
  22. contentDetails,
  23. onSourceLocationClick,
  24. onClose,
  25. index,
  26. logEntry,
  27. }) {
  28. if (ruleId && HumanReadableLogsHints[ruleId]) {
  29. const hint = HumanReadableLogsHints[ruleId]
  30. formattedContent = hint.formattedContent(contentDetails)
  31. extraInfoURL = hint.extraInfoURL
  32. }
  33. const handleLogEntryLinkClick = useCallback(
  34. event => {
  35. event.preventDefault()
  36. onSourceLocationClick(sourceLocation)
  37. const parts = sourceLocation?.file?.split('.')
  38. const extension = parts?.length > 1 ? parts.pop() : ''
  39. sendMB('log-entry-link-click', { level, ruleId, extension })
  40. },
  41. [level, onSourceLocationClick, ruleId, sourceLocation]
  42. )
  43. return (
  44. <div
  45. className={classNames('log-entry', customClass)}
  46. aria-label={entryAriaLabel}
  47. data-ruleid={ruleId}
  48. >
  49. <PreviewLogEntryHeader
  50. level={level}
  51. sourceLocation={sourceLocation}
  52. headerTitle={headerTitle}
  53. headerIcon={headerIcon}
  54. logType={logType}
  55. showSourceLocationLink={showSourceLocationLink}
  56. onSourceLocationClick={handleLogEntryLinkClick}
  57. showCloseButton={showCloseButton}
  58. onClose={onClose}
  59. />
  60. {(rawContent || formattedContent || window.user.alphaProgram) && (
  61. <PdfLogEntryContent
  62. rawContent={rawContent}
  63. formattedContent={formattedContent}
  64. extraInfoURL={extraInfoURL}
  65. index={index}
  66. logEntry={logEntry}
  67. />
  68. )}
  69. </div>
  70. )
  71. }
  72. PdfLogEntry.propTypes = {
  73. ruleId: PropTypes.string,
  74. sourceLocation: PreviewLogEntryHeader.propTypes.sourceLocation,
  75. headerTitle: PreviewLogEntryHeader.propTypes.headerTitle,
  76. headerIcon: PropTypes.element,
  77. rawContent: PropTypes.string,
  78. logType: PropTypes.string,
  79. formattedContent: PropTypes.node,
  80. extraInfoURL: PropTypes.string,
  81. level: PropTypes.oneOf([
  82. 'error',
  83. 'warning',
  84. 'info',
  85. 'typesetting',
  86. 'raw',
  87. 'success',
  88. ]).isRequired,
  89. customClass: PropTypes.string,
  90. showSourceLocationLink: PropTypes.bool,
  91. showCloseButton: PropTypes.bool,
  92. entryAriaLabel: PropTypes.string,
  93. contentDetails: PropTypes.arrayOf(PropTypes.string),
  94. onSourceLocationClick: PropTypes.func,
  95. onClose: PropTypes.func,
  96. index: PropTypes.number,
  97. logEntry: PropTypes.any,
  98. }
  99. export default memo(PdfLogEntry)