- collapse run log data blocks whose pretty-printed JSON exceeds a byte or line limit, with a per-block toggle and an expand/collapse-all button next to the Logs heading - align data blocks and toggles with the context/message column - add a "Copy logs" button that copies the filtered logs as plain text with every data block expanded - add GET /manifest.json with the app name, version and run log data limits (RUN_LOG_DATA_COLLAPSE_BYTES / RUN_LOG_DATA_COLLAPSE_LINES) - load the manifest in the client stale-while-revalidate: localStorage copy first, refetched per page load and on tab focus after a while - show the server version in the sidebar footer, with a reload hint when it differs from the client build Closes #3
148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
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<Set<LogOutputEntry['id']>>(() => 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 <div className={styles.empty}>{emptyMessage}</div>;
|
|
}
|
|
|
|
const startMs = new Date(startTime ?? entries[0].createdAt).getTime();
|
|
|
|
return (
|
|
<div
|
|
className={[styles.root, className].filter(Boolean).join(' ')}
|
|
style={maxHeight !== undefined ? { maxHeight } : undefined}
|
|
role="log"
|
|
tabIndex={0}
|
|
>
|
|
{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 (
|
|
<div key={entry.id} className={[styles.line, styles[entry.level]].join(' ')}>
|
|
<div className={styles.lineHeader}>
|
|
<span className={styles.timestamp} title={new Date(entryMs).toISOString()}>
|
|
{formatElapsed(entryMs - startMs)}
|
|
</span>
|
|
<span className={styles.level}>{entry.level}</span>
|
|
{entry.context && <span className={styles.context}>{entry.context}</span>}
|
|
<span className={styles.message}>{entry.message}</span>
|
|
</div>
|
|
{block && large && (
|
|
<button
|
|
type="button"
|
|
className={styles.dataToggle}
|
|
aria-expanded={open}
|
|
onClick={() => toggle(entry.id)}
|
|
>
|
|
{open ? (
|
|
<ChevronDown size={14} strokeWidth={3} aria-hidden="true" />
|
|
) : (
|
|
<ChevronRight size={14} strokeWidth={3} aria-hidden="true" />
|
|
)}
|
|
{open ? hideDataLabel : showDataLabel(block)}
|
|
</button>
|
|
)}
|
|
{block && open && (
|
|
<div className={styles.data}>
|
|
<CodeBlock code={block.pretty} language="json" />
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
}
|