fix(dashboard): guard pagination state writes against stale-session race

Add stale-session check to the isPaginationRequest branch in loadMessages.
If the user switches sessions while pagination is in progress, the old
session's messages and hasMoreMessages state should not overwrite the
newly active session's state.
This commit is contained in:
Josemi Liebana
2026-05-25 12:35:19 +02:00
parent 7b90f441fe
commit a3b6c1aab1

View File

@@ -473,10 +473,13 @@ export function useChat(
try {
if (isPaginationRequest) {
// Prepend older messages (forward pagination, used when hasMoreMessages)
const requestSessionId = sessionId;
const data = await fetchChatMessages(sessionId, { limit: 50, ...opts }, projectId);
const mappedMessages = data.messages.map(mapChatMessageToInfo);
setMessages((prev) => [...mappedMessages, ...prev]);
setHasMoreMessages(data.messages.length >= 50);
if (activeSessionRef.current?.id === requestSessionId) {
const mappedMessages = data.messages.map(mapChatMessageToInfo);
setMessages((prev) => [...mappedMessages, ...prev]);
setHasMoreMessages(data.messages.length >= 50);
}
} else {
// Initial full load — fetch all messages, never truncate at 50
const allMessages = await fetchAllMessagesInChat(sessionId, projectId);