import { useRef, useState, useEffect } from "react"; import type { FC } from "react"; import { Select } from "@douyinfe/semi-ui"; import { FlowChart } from "../FlowChart/FlowChart"; import type { FlowChartRef } from "../FlowChart/FlowChart"; import { useFlowChartData } from "../FlowChart/hooks/useFlowChartData"; import { traceApi } from "../../api/traceApi"; import type { Goal } from "../../types/goal"; import type { Edge, Message } from "../../types/message"; import type { TraceListItem } from "../../types/trace"; import styles from "./MainContent.module.css"; interface MainContentProps { traceId: string | null; onNodeClick?: (node: Goal | Message, edge?: Edge) => void; onTraceChange?: (traceId: string, title?: string) => void; refreshTrigger?: number; messageRefreshTrigger?: number; } export const MainContent: FC = ({ traceId, onNodeClick, onTraceChange, refreshTrigger, messageRefreshTrigger, }) => { const flowChartRef = useRef(null); const [isAllExpanded, setIsAllExpanded] = useState(true); const [traceList, setTraceList] = useState([]); const [cachedGoals, setCachedGoals] = useState([]); const [cachedMsgGroups, setCachedMsgGroups] = useState>({}); const [cachedInvalidBranches, setCachedInvalidBranches] = useState([]); const { goals, connected, msgGroups, reloading, invalidBranches } = useFlowChartData(traceId, messageRefreshTrigger); console.log("%c [ msgGroups ]-34", "font-size:13px; background:pink; color:#bf2c9f;", msgGroups); const displayGoals = goals.length > 0 ? goals : cachedGoals; const displayMsgGroups = Object.keys(msgGroups).length > 0 ? msgGroups : cachedMsgGroups; const displayInvalidBranches = invalidBranches && invalidBranches.length > 0 ? invalidBranches : cachedInvalidBranches; useEffect(() => { const fetchTraces = async () => { try { const data = await traceApi.fetchTraces({ limit: 100 }); setTraceList(data.traces); } catch (error) { console.error("Failed to load traces:", error); } }; fetchTraces(); }, [refreshTrigger]); useEffect(() => { // 移除 reload 调用,因为 useFlowChartData 内部会监听 messageRefreshTrigger 并重新加载 }, [messageRefreshTrigger]); useEffect(() => { if (goals.length > 0) { setCachedGoals(goals); } }, [goals]); useEffect(() => { if (Object.keys(msgGroups).length > 0) { setCachedMsgGroups(msgGroups); } }, [msgGroups]); useEffect(() => { if (invalidBranches && invalidBranches.length > 0) { setCachedInvalidBranches(invalidBranches); } }, [invalidBranches]); useEffect(() => { setCachedGoals([]); setCachedMsgGroups({}); setCachedInvalidBranches([]); }, [traceId]); if (!traceId && !reloading) { return (
暂无 Trace
未连接
暂无可展示的数据
); } return (
{connected ? "WebSocket 已连接" : "WebSocket 未连接"}