fix(history): query chat session and history in sidebar (#400)

This commit is contained in:
DigHuang
2026-03-11 10:27:54 +08:00
committed by GitHub
Unverified
parent ed40a3b7f4
commit ce0e5fd8af
2 changed files with 22 additions and 12 deletions

View File

@@ -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 === '/';

View File

@@ -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(() => {