import { useEffect, useRef, useState } from "react"; import type { AgentLogEntry } from "@hai/core"; interface AgentLogViewerProps { entries: AgentLogEntry[]; loading: boolean; } /** * Renders agent log entries in a scrollable, monospace container. * Auto-scrolls to the bottom as new entries arrive, but pauses * auto-scroll when the user scrolls up (scroll-lock). */ export function AgentLogViewer({ entries, loading }: AgentLogViewerProps) { const containerRef = useRef(null); const [autoScroll, setAutoScroll] = useState(true); // Auto-scroll to bottom when new entries arrive (if scroll-lock is not active) useEffect(() => { if (autoScroll && containerRef.current) { containerRef.current.scrollTop = containerRef.current.scrollHeight; } }, [entries, autoScroll]); const handleScroll = () => { const el = containerRef.current; if (!el) return; // If the user is within 50px of the bottom, re-enable auto-scroll const atBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 50; setAutoScroll(atBottom); }; if (loading && entries.length === 0) { return (
Loading agent logs…
); } if (entries.length === 0) { return (
No agent output yet.
); } return (
{entries.map((entry, i) => entry.type === "tool" ? (
⚡ {entry.text}
) : ( {entry.text} ), )}
); }