pdf-logs-entries.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. contentDetails={logEntry.contentDetails}
  49. entryAriaLabel={t('log_entry_description', {
  50. level: logEntry.level,
  51. })}
  52. sourceLocation={{
  53. file: logEntry.file,
  54. line: logEntry.line,
  55. column: logEntry.column,
  56. }}
  57. onSourceLocationClick={syncToEntry}
  58. />
  59. ))}
  60. </>
  61. )
  62. }
  63. PdfLogsEntries.propTypes = {
  64. entries: PropTypes.arrayOf(PropTypes.object),
  65. hasErrors: PropTypes.bool,
  66. }
  67. export default memo(PdfLogsEntries)