fix: resolve session switching stuck on loading spinner

Race condition when quickly switching chat sessions caused the UI to get
stuck on loading. Added cancellation flags and key-based remounting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
7418andClaude Opus 4.6 committed 2026-02-09 10:19:25 +08:00
1 parent e17482e652
commit a42c0e312d
2 files changed
+15 -3

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "codepilot",
"version": "0.5.0",
"version": "0.5.1",
"private": true,
"main": "dist-electron/main.js",
"scripts": {
+14 -2
View File
@@ -48,9 +48,17 @@ export default function ChatSessionPage({ params }: ChatSessionPageProps) {
}, [id, setWorkingDirectory, setSessionId, setPanelSessionTitle, setPanelOpen]);
useEffect(() => {
// Reset state when switching sessions
setLoading(true);
setError(null);
setMessages([]);
let cancelled = false;
async function loadMessages() {
try {
const res = await fetch(`/api/chat/sessions/${id}/messages`);
if (cancelled) return;
if (!res.ok) {
if (res.status === 404) {
setError('Session not found');
@@ -59,15 +67,19 @@ export default function ChatSessionPage({ params }: ChatSessionPageProps) {
throw new Error('Failed to load messages');
}
const data: MessagesResponse = await res.json();
if (cancelled) return;
setMessages(data.messages);
} catch (err) {
if (cancelled) return;
setError(err instanceof Error ? err.message : 'Failed to load messages');
} finally {
setLoading(false);
if (!cancelled) setLoading(false);
}
}
loadMessages();
return () => { cancelled = true; };
}, [id]);
if (loading) {
@@ -104,7 +116,7 @@ export default function ChatSessionPage({ params }: ChatSessionPageProps) {
</h2>
</div>
)}
<ChatView sessionId={id} initialMessages={messages} modelName={sessionModel} initialMode={sessionMode} />
<ChatView key={id} sessionId={id} initialMessages={messages} modelName={sessionModel} initialMode={sessionMode} />
</div>
);
}