pdf-log-entry.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }) {
  26. if (ruleId && HumanReadableLogsHints[ruleId]) {
  27. const hint = HumanReadableLogsHints[ruleId]
  28. formattedContent = hint.formattedContent(contentDetails)
  29. extraInfoURL = hint.extraInfoURL
  30. }
  31. const handleLogEntryLinkClick = useCallback(
  32. event => {
  33. event.preventDefault()
  34. onSourceLocationClick(sourceLocation)
  35. const parts = sourceLocation?.file?.split('.')
  36. const extension = parts?.length > 1 ? parts.pop() : ''
  37. sendMB('log-entry-link-click', { level, ruleId, extension })
  38. },
  39. [level, onSourceLocationClick, ruleId, sourceLocation]
  40. )
  41. return (
  42. <div
  43. className={classNames('log-entry', customClass)}
  44. aria-label={entryAriaLabel}
  45. >
  46. <PreviewLogEntryHeader
  47. level={level}
  48. sourceLocation={sourceLocation}
  49. headerTitle={headerTitle}
  50. headerIcon={headerIcon}
  51. logType={logType}
  52. showSourceLocationLink={showSourceLocationLink}
  53. onSourceLocationClick={handleLogEntryLinkClick}
  54. showCloseButton={showCloseButton}
  55. onClose={onClose}
  56. />
  57. {(rawContent || formattedContent) && (
  58. <PdfLogEntryContent
  59. rawContent={rawContent}
  60. formattedContent={formattedContent}
  61. extraInfoURL={extraInfoURL}
  62. />
  63. )}
  64. </div>
  65. )
  66. }
  67. PdfLogEntry.propTypes = {
  68. ruleId: PropTypes.string,
  69. sourceLocation: PreviewLogEntryHeader.propTypes.sourceLocation,
  70. headerTitle: PreviewLogEntryHeader.propTypes.headerTitle,
  71. headerIcon: PropTypes.element,
  72. rawContent: PropTypes.string,
  73. logType: PropTypes.string,
  74. formattedContent: PropTypes.node,
  75. extraInfoURL: PropTypes.string,
  76. level: PropTypes.oneOf([
  77. 'error',
  78. 'warning',
  79. 'info',
  80. 'typesetting',
  81. 'raw',
  82. 'success',
  83. ]).isRequired,
  84. customClass: PropTypes.string,
  85. showSourceLocationLink: PropTypes.bool,
  86. showCloseButton: PropTypes.bool,
  87. entryAriaLabel: PropTypes.string,
  88. contentDetails: PropTypes.arrayOf(PropTypes.string),
  89. onSourceLocationClick: PropTypes.func,
  90. onClose: PropTypes.func,
  91. }
  92. export default memo(PdfLogEntry)