import { Dispatch, MouseEventHandler, useCallback, memo, SetStateAction, useState, } from 'react' import HumanReadableLogsHints from '../../../../ide/human-readable-logs/HumanReadableLogsHints' import { sendMB } from '@/infrastructure/event-tracking' import { ErrorLevel, LogEntry as LogEntryData, SourceLocation, } from '@/features/pdf-preview/util/types' import LogEntryHeader from './log-entry-header' import PdfLogEntryContent from '@/features/pdf-preview/components/pdf-log-entry-content' import classNames from 'classnames' type LogEntryProps = { headerTitle: string | React.ReactNode level: ErrorLevel ruleId?: string rawContent?: string logType?: string formattedContent?: React.ReactNode extraInfoURL?: string | null sourceLocation?: SourceLocation showSourceLocationLink?: boolean entryAriaLabel?: string contentDetails?: string[] onSourceLocationClick?: (sourceLocation: SourceLocation) => void index?: number logEntry?: LogEntryData id?: string alwaysExpandRawContent?: boolean className?: string actionButtonsOverride?: React.ReactNode openCollapseIconOverride?: string } function LogEntry(props: LogEntryProps) { const [collapsed, setCollapsed] = useState(props.index !== 0) return ( ) } export function ControlledLogEntry({ ruleId, headerTitle, rawContent, logType, formattedContent, extraInfoURL, level, sourceLocation, showSourceLocationLink = true, entryAriaLabel = undefined, contentDetails, onSourceLocationClick, index, logEntry, id, alwaysExpandRawContent = false, className, collapsed, setCollapsed, actionButtonsOverride, openCollapseIconOverride, }: LogEntryProps & { collapsed: boolean setCollapsed: Dispatch> }) { if (ruleId && HumanReadableLogsHints[ruleId]) { const hint = HumanReadableLogsHints[ruleId] formattedContent = hint.formattedContent(contentDetails) extraInfoURL = hint.extraInfoURL } const handleLogEntryLinkClick: MouseEventHandler = useCallback( event => { event.preventDefault() if (onSourceLocationClick && sourceLocation) { onSourceLocationClick(sourceLocation) const parts = sourceLocation?.file?.split('.') const extension = parts?.length && parts?.length > 1 ? parts.pop() : '' sendMB('log-entry-link-click', { level, ruleId, extension }) } }, [level, onSourceLocationClick, ruleId, sourceLocation] ) return (
setCollapsed(collapsed => !collapsed)} id={id} logEntry={logEntry} actionButtonsOverride={actionButtonsOverride} openCollapseIconOverride={openCollapseIconOverride} />
) } export default memo(LogEntry)