Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): land a chat list refetch a newer one overtook

Yudhi Armyndharis677fe5d318d1
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index bc35752d7..0c06f4c50 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -954,6 +954,50 @@ test('a chat list that answers after the user switched sessions does not replace
}
});
+test('a chat list refetch lands while a newer one is out, and an older answer never overwrites a newer', async () => {
+ const { screen, waitFor } = rtl;
+ const { container } = renderChats();
+ await screen.findByText('Alice');
+
+ // Edits to a chat that was never opened refetch the list each, and they arrive faster than it answers.
+ const answers: Array<() => void> = [];
+ chatsResponder = () =>
+ new Promise<Response>(resolve => {
+ const version = answers.length + 1;
+ answers.push(() => resolve(jsonResponse([{ ...CHAT_2, lastMessage: `carol v${version}` }, CHAT])));
+ });
+ const socket = lastSocket();
+ assert.ok(socket, 'expected the page to have opened a socket');
+ const edit = (messageId: string): void =>
+ socket.receive('message', {
+ type: 'event',
+ timestamp: new Date(1_700_003_000_000).toISOString(),
+ payload: {
+ event: 'message.edited',
+ sessionId: SESSION.id,
+ data: { messageId, chatId: CHAT_2.id, body: 'edited', timestamp: 1_700_003_000 },
+ },
+ });
+ edit('wamid.carol.1');
+ edit('wamid.carol.2');
+ edit('wamid.carol.3');
+ await waitFor(() => assert.equal(answers.length, 3));
+ assert.ok(screen.queryByText('Alice'), 'a background refetch hid the chat list behind the loading spinner');
+
+ // The first answer is the newest one applied so far, so it lands although newer calls are still out.
+ answers[0]();
+ await screen.findByText('carol v1');
+ assert.equal(container.querySelector('.chats-list-loading'), null, 'the list stayed on the loading spinner');
+
+ answers[2]();
+ await screen.findByText('carol v3');
+ answers[1]();
+ await flush();
+ await flush();
+ assert.ok(screen.queryByText('carol v3'), 'an older answer replaced the newer list');
+ assert.ok(!screen.queryByText('carol v2'), 'an older answer replaced the newer list');
+});
+
test('changing the UI language keeps the selected session and the open chat', async () => {
const { screen, fireEvent, within, waitFor, act } = rtl;
const { default: i18n } = await import('../i18n/index.ts');
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index fe3020f33..bb0f4dae3 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -307,26 +307,34 @@ export function Chats() {
void loadSessions();
}, [showLoadError]);
- // 2. Fetch chats when active session changes. Only the newest call may write: a session switch
- // does not cancel the list still loading for the session left behind, and a list that lands
- // late would put that account's chats under the selected session.
+ // 2. Fetch chats when active session changes. A session switch does not cancel the list still
+ // loading for the session left behind, so an answer for any session but the latest call's is
+ // dropped, or it would put that account's chats under the selected session. Within one session an
+ // answer is dropped only once a newer one has landed: dropping every answer a newer call overtook
+ // left a burst of realtime refetches with no list at all, each miss firing the next refetch.
+ // A realtime refetch runs in the background, keeping the current list on screen.
const chatsRequestRef = useRef(0);
+ const chatsAppliedRef = useRef(0);
+ const chatsSessionRef = useRef('');
const loadChats = useCallback(
- async (sessionId: string) => {
+ async (sessionId: string, { background = false } = {}) => {
if (!sessionId) return;
const request = ++chatsRequestRef.current;
+ chatsSessionRef.current = sessionId;
+ const stale = () => sessionId !== chatsSessionRef.current || request < chatsAppliedRef.current;
try {
- setLoadingChats(true);
+ if (!background) setLoadingChats(true);
const data = await sessionApi.getChats(sessionId);
- if (request !== chatsRequestRef.current) return;
+ if (stale()) return;
+ chatsAppliedRef.current = request;
const sorted = [...data].sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0));
setChats(sorted);
} catch (err) {
- if (request !== chatsRequestRef.current) return;
+ if (stale()) return;
showLoadError('chats.errors.loadChats', err);
setChats([]);
} finally {
- if (request === chatsRequestRef.current) setLoadingChats(false);
+ if (sessionId === chatsSessionRef.current) setLoadingChats(false);
}
},
[showLoadError],
@@ -429,7 +437,7 @@ export function Chats() {
return result.chats;
});
if (needsSidebarRefetch) {
- void loadChats(selectedSessionId);
+ void loadChats(selectedSessionId, { background: true });
}
},
[selectedSessionId, activeChat, canWrite, loadChats, markChatRead, appendMessage, onMessageAppended, t],
@@ -527,7 +535,7 @@ export function Chats() {
// The chat may never have been opened, so there is no message cache from which to prove
// whether this was its latest row. Refresh summaries instead of guessing and overwriting the
// sidebar with the body of an older edited message.
- void loadChats(selectedSessionId);
+ void loadChats(selectedSessionId, { background: true });
}
},
[selectedSessionId, queryClient, loadChats],