/** * 日志流展示组件。 * * - 等宽字体展示日志行 * - 自动滚动到底部(useRef + scrollIntoView) * - 提供"暂停自动滚动"按钮(toggle) */ import React, { useEffect, useRef, useState } from "react"; import { Button } from "antd"; import { PauseCircleOutlined, PlayCircleOutlined } from "@ant-design/icons"; export interface LogStreamProps { /** 可选的执行 ID,用于标题展示 */ executionId?: string; /** 日志行数组 */ lines: string[]; } const LogStream: React.FC = ({ lines }) => { const [autoscroll, setAutoscroll] = useState(true); const bottomRef = useRef(null); useEffect(() => { if (autoscroll && bottomRef.current) { bottomRef.current.scrollIntoView({ behavior: "smooth" }); } }, [lines, autoscroll]); const handleToggle = () => { const next = !autoscroll; setAutoscroll(next); // 恢复时立即滚动到底部 if (next && bottomRef.current) { bottomRef.current.scrollIntoView({ behavior: "smooth" }); } }; return (
{lines.length === 0 ? (
暂无日志
) : ( lines.map((line, i) => { let color = "#d4d4d4"; if (/\bERROR\b/i.test(line)) color = "#f56c6c"; else if (/\bWARN(?:ING)?\b/i.test(line)) color = "#e6a23c"; return (
{line}
); }) )}
); }; export default LogStream;