import { useMemo, useState } from 'react'; import { ChevronDown, ChevronRight } from 'lucide-react'; import { CodeBlock } from '../CodeBlock/CodeBlock'; import { DEFAULT_LOG_DATA_LIMITS, exceedsLimits, formatElapsed, measureData, prettyData, type LogDataLimits, } from './logText'; import styles from './LogOutput.module.css'; export type LogOutputLevel = 'debug' | 'log' | 'warn' | 'error'; export interface LogOutputEntry { id: string | number; level: LogOutputLevel; /** Where the message was generated, e.g. "step:2", "snippet:login", "assertion:pass". */ context?: string | null; message: string; /** JSON payload attached to the message, as a raw string; rendered pretty-printed, separately from the message. */ data?: string | null; createdAt: string | number | Date; } export interface LogOutputProps { entries: LogOutputEntry[]; /** Reference point for the elapsed-time column; defaults to the first entry's timestamp. */ startTime?: string | number | Date; /** Caps the block's height (e.g. "400px"), scrolling once entries overflow it. */ maxHeight?: string | number; emptyMessage?: string; className?: string; /** Data blocks whose pretty-printed JSON exceeds either limit start collapsed behind a toggle. */ dataLimits?: LogDataLimits; /** * Expands (true) or collapses (false) every large data block. Changing it * discards the per-entry toggles made since the last change. */ dataExpanded?: boolean; /** Label of a collapsed data block's toggle, e.g. "Show data (1.2 KB, 40 lines)". */ showDataLabel?: (size: { bytes: number; lines: number }) => string; hideDataLabel?: string; } function formatBytes(bytes: number): string { return bytes < 1024 ? `${bytes} B` : `${(bytes / 1024).toFixed(1)} KB`; } const defaultShowDataLabel = ({ bytes, lines }: { bytes: number; lines: number }) => `Show data (${formatBytes(bytes)}, ${lines} ${lines === 1 ? 'line' : 'lines'})`; /** Terminal-style log viewer: one line per entry, color-coded by level. */ export function LogOutput({ entries, startTime, maxHeight, emptyMessage = 'No logs.', className, dataLimits = DEFAULT_LOG_DATA_LIMITS, dataExpanded = false, showDataLabel = defaultShowDataLabel, hideDataLabel = 'Hide data', }: LogOutputProps) { // Per-entry toggles, reset whenever dataExpanded changes const [toggled, setToggled] = useState>(() => new Set()); const [prevDataExpanded, setPrevDataExpanded] = useState(dataExpanded); if (prevDataExpanded !== dataExpanded) { setPrevDataExpanded(dataExpanded); setToggled(new Set()); } const dataBlocks = useMemo(() => { const blocks = new Map< LogOutputEntry['id'], { pretty: string; bytes: number; lines: number } >(); for (const entry of entries) { if (!entry.data) continue; const pretty = prettyData(entry.data); blocks.set(entry.id, { pretty, ...measureData(pretty) }); } return blocks; }, [entries]); const toggle = (id: LogOutputEntry['id']) => setToggled((prev) => { const next = new Set(prev); if (!next.delete(id)) next.add(id); return next; }); if (entries.length === 0) { return
{emptyMessage}
; } const startMs = new Date(startTime ?? entries[0].createdAt).getTime(); return (
{entries.map((entry) => { const entryMs = new Date(entry.createdAt).getTime(); const block = dataBlocks.get(entry.id); const large = block != null && exceedsLimits(block, dataLimits); const open = !large || dataExpanded !== toggled.has(entry.id); return (
{formatElapsed(entryMs - startMs)} {entry.level} {entry.context && {entry.context}} {entry.message}
{block && large && ( )} {block && open && (
)}
); })}
); }