pdf-logs-entries.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { memo, useCallback } from 'react'
  2. import PropTypes from 'prop-types'
  3. import { useTranslation } from 'react-i18next'
  4. import PreviewLogsPaneMaxEntries from '../../preview/components/preview-logs-pane-max-entries'
  5. import PdfLogEntry from './pdf-log-entry'
  6. import { useIdeContext } from '../../../shared/context/ide-context'
  7. import useDetachAction from '../../../shared/hooks/use-detach-action'
  8. const LOG_PREVIEW_LIMIT = 100
  9. function PdfLogsEntries({ entries, hasErrors }) {
  10. const { t } = useTranslation()
  11. const ide = useIdeContext()
  12. const _syncToEntry = useCallback(
  13. entry => {
  14. const entity = ide.fileTreeManager.findEntityByPath(entry.file)
  15. if (entity && entity.type === 'doc') {
  16. ide.editorManager.openDoc(entity, {
  17. gotoLine: entry.line ?? undefined,
  18. gotoColumn: entry.column ?? undefined,
  19. })
  20. }
  21. },
  22. [ide]
  23. )
  24. const syncToEntry = useDetachAction(
  25. 'sync-to-entry',
  26. _syncToEntry,
  27. 'detached',
  28. 'detacher'
  29. )
  30. const logEntries = entries.slice(0, LOG_PREVIEW_LIMIT)
  31. return (
  32. <>
  33. {entries.length > LOG_PREVIEW_LIMIT && (
  34. <PreviewLogsPaneMaxEntries
  35. totalEntries={entries.length}
  36. entriesShown={LOG_PREVIEW_LIMIT}
  37. hasErrors={hasErrors}
  38. />
  39. )}
  40. {logEntries.map(logEntry => (
  41. <PdfLogEntry
  42. key={logEntry.key}
  43. ruleId={logEntry.ruleId}
  44. headerTitle={logEntry.message}
  45. rawContent={logEntry.content}
  46. logType={logEntry.type}
  47. level={logEntry.level}
  48. entryAriaLabel={t('log_entry_description', {
  49. level: logEntry.level,
  50. })}
  51. sourceLocation={{
  52. file: logEntry.file,
  53. line: logEntry.line,
  54. column: logEntry.column,
  55. }}
  56. onSourceLocationClick={syncToEntry}
  57. />
  58. ))}
  59. </>
  60. )
  61. }
  62. PdfLogsEntries.propTypes = {
  63. entries: PropTypes.arrayOf(PropTypes.object),
  64. hasErrors: PropTypes.bool,
  65. }
  66. export default memo(PdfLogsEntries)