guard stale init effects

This commit is contained in:
2026-04-16 07:56:57 -06:00
parent 7b4874b8b1
commit 2c9dedb65e
2 changed files with 16 additions and 2 deletions

View File

@@ -152,6 +152,8 @@ function ChatPage() {
useEffect(() => {
if (!token || authLoading) return;
let stale = false;
async function init() {
setLoadingConv(true);
setError(null);
@@ -163,6 +165,7 @@ function ChatPage() {
const res = await fetch(`${API_BASE}/api/v1/chat/conversations`, {
headers: { Authorization: `Bearer ${token}` },
});
if (stale) return;
if (res.ok) {
const list = await res.json();
const open = Array.isArray(list)
@@ -171,12 +174,14 @@ function ChatPage() {
if (open) convId = open.id;
}
} catch {
if (stale) return;
setError("Failed to load conversations.");
}
}
if (!convId) {
await fetchConversations();
if (stale) return;
setLoadingConv(false);
setConversation(null);
return;
@@ -187,6 +192,7 @@ function ChatPage() {
fetchMessages(convId),
fetchConversations(),
]);
if (stale) return;
setLoadingConv(false);
startPolling(convId);
}
@@ -194,6 +200,7 @@ function ChatPage() {
init();
return () => {
stale = true;
if (pollRef.current) clearInterval(pollRef.current);
};
}, [token, authLoading, conversationIdParam, fetchConversation, fetchMessages, startPolling, fetchConversations]);