From ce0e5fd8afa53abb3dbdf791b7c292f9a82a9f3c Mon Sep 17 00:00:00 2001 From: DigHuang <114602213+DigHuang@users.noreply.github.com> Date: Wed, 11 Mar 2026 10:27:54 +0800 Subject: [PATCH] fix(history): query chat session and history in sidebar (#400) --- src/components/layout/Sidebar.tsx | 21 +++++++++++++++++++++ src/pages/Chat/index.tsx | 13 +------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/components/layout/Sidebar.tsx b/src/components/layout/Sidebar.tsx index ee4ed654c..67fec7b5e 100644 --- a/src/components/layout/Sidebar.tsx +++ b/src/components/layout/Sidebar.tsx @@ -22,6 +22,7 @@ import { import { cn } from '@/lib/utils'; import { useSettingsStore } from '@/stores/settings'; import { useChatStore } from '@/stores/chat'; +import { useGatewayStore } from '@/stores/gateway'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { ConfirmDialog } from '@/components/ui/confirm-dialog'; @@ -113,6 +114,26 @@ export function Sidebar() { const switchSession = useChatStore((s) => s.switchSession); const newSession = useChatStore((s) => s.newSession); const deleteSession = useChatStore((s) => s.deleteSession); + const loadSessions = useChatStore((s) => s.loadSessions); + const loadHistory = useChatStore((s) => s.loadHistory); + + const gatewayStatus = useGatewayStore((s) => s.status); + const isGatewayRunning = gatewayStatus.state === 'running'; + + useEffect(() => { + if (!isGatewayRunning) return; + let cancelled = false; + const hasExistingMessages = useChatStore.getState().messages.length > 0; + (async () => { + await loadSessions(); + if (cancelled) return; + await loadHistory(hasExistingMessages); + })(); + return () => { + cancelled = true; + }; + }, [isGatewayRunning, loadHistory, loadSessions]); + const navigate = useNavigate(); const isOnChat = useLocation().pathname === '/'; diff --git a/src/pages/Chat/index.tsx b/src/pages/Chat/index.tsx index 931b914f5..68c281168 100644 --- a/src/pages/Chat/index.tsx +++ b/src/pages/Chat/index.tsx @@ -29,8 +29,6 @@ export function Chat() { const streamingMessage = useChatStore((s) => s.streamingMessage); const streamingTools = useChatStore((s) => s.streamingTools); const pendingFinal = useChatStore((s) => s.pendingFinal); - const loadHistory = useChatStore((s) => s.loadHistory); - const loadSessions = useChatStore((s) => s.loadSessions); const sendMessage = useChatStore((s) => s.sendMessage); const abortRun = useChatStore((s) => s.abortRun); const clearError = useChatStore((s) => s.clearError); @@ -46,21 +44,12 @@ export function Chat() { // stay visible while fresh data loads in the background. This avoids // an unnecessary messages → spinner → messages flicker. useEffect(() => { - if (!isGatewayRunning) return; - let cancelled = false; - const hasExistingMessages = useChatStore.getState().messages.length > 0; - (async () => { - await loadSessions(); - if (cancelled) return; - await loadHistory(hasExistingMessages); - })(); return () => { - cancelled = true; // If the user navigates away without sending any messages, remove the // empty session so it doesn't linger as a ghost entry in the sidebar. cleanupEmptySession(); }; - }, [isGatewayRunning, loadHistory, loadSessions, cleanupEmptySession]); + }, [cleanupEmptySession]); // Auto-scroll on new messages, streaming, or activity changes useEffect(() => {