Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep a deleted chat out of a list asked for before
Yudhi Armyndharisf17750970630
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.tsindex d71be32b4..3066a3cb6 100644--- a/dashboard/src/pages/Chats.features.test.ts+++ b/dashboard/src/pages/Chats.features.test.ts@@ -1765,7 +1765,7 @@ for (const kind of ['clear', 'delete'] as const) {const chatListReads = () =>fetchCalls.filter(c => c.method === 'GET' && c.path.startsWith(`/api/sessions/${SESSION.id}/chats?`)).length;-const RELOAD_RACES: Array<{ title: string; act: () => Promise<void>; shows: () => void }> = [+const RELOAD_RACES: Array<{ title: string; engine?: string; act: () => Promise<void>; shows: () => void }> = [{title: 'an archive',act: async () => pickRowAction('Alice', 'Archive chat'),@@ -1799,11 +1799,21 @@ const RELOAD_RACES: Array<{ title: string; act: () => Promise<void>; shows: () =},shows: () => assert.ok(rtl.screen.getByText('Alicia'), 'the renamed chat has its old name back'),},+ {+ title: 'a delete',+ engine: 'whatsapp-web.js',+ act: async () => {+ pickRowAction('Alice', 'Delete chat');+ await confirmDialog('Delete chat');+ },+ shows: () => assert.ok(!rtl.screen.queryByText('Alice'), 'the deleted chat is back in the sidebar'),+ },];for (const race of RELOAD_RACES) {test(`a chat list asked for before ${race.title} does not undo it when it answers`, async () => {const { screen, waitFor } = rtl;+ if (race.engine) window.sessionStorage.setItem('openwa_engine_type', race.engine);renderPage();await screen.findByText('Alice');const release = hold('chats');@@ -1826,6 +1836,31 @@ for (const race of RELOAD_RACES) {});}+test('a chat that writes again after its delete is listed by the list that message asks for', async () => {+ const { screen, waitFor } = rtl;+ window.sessionStorage.setItem('openwa_engine_type', 'whatsapp-web.js');+ renderPage();+ await screen.findByText('Alice');+ const release = hold('chats');+ receiveEvent('message.edited', {+ messageId: 'wamid.edit.2',+ chatId: CHAT.id,+ body: 'edited',+ timestamp: 1_700_001_000,+ });+ await waitFor(() => assert.equal(chatListReads(), 2, 'the edit did not ask for the list'));++ pickRowAction('Alice', 'Delete chat');+ await confirmDialog('Delete chat');+ await waitFor(() => assert.ok(!screen.queryByText('Alice'), 'the chat was not deleted'));+ // The conversation is gone, so its next message finds no row and asks for the list again.+ receiveFromAlice('wamid.after.delete', 'hello again');++ release();+ await waitFor(() => assert.equal(chatListReads(), 3, 'the message did not ask for the list'));+ await screen.findByText('Alice');+});+test('a chat list asked for before Mark as unread does not mark a chat that was opened and read since', async () => {const { screen, fireEvent, waitFor, within } = rtl;const { container } = renderPage();diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 5bafc6338..8c0614f5d 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -433,6 +433,9 @@ export function Chats() {const held = patchedRowsRef.current.get(chatId);patchedRowsRef.current.set(chatId, { request: chatsRequestRef.current, patch: { ...held?.patch, ...patch } });}, []);+ // A chat the page deleted, with the newest request number out then. A list asked for at or before that still+ // holds the chat and would bring it back; one asked for after it is as good as the engine's own answer.+ const deletedRowsRef = useRef(new Map<string, number>());// A chat this page has read shows nothing unread, and a reload asked for before the read keeps it so. That also// replaces a Mark as unread still remembered for the chat, which the read has undone. A session switch leaves the// open chat the previous account's for one render, and that account's row must not be remembered for this one.@@ -477,12 +480,18 @@ export function Chats() {if (entry.request >= request) patches.set(id, entry.patch);else patchedRowsRef.current.delete(id);}+ const gone = new Set<string>();+ for (const [id, deletedAt] of deletedRowsRef.current) {+ if (deletedAt >= request) gone.add(id);+ else deletedRowsRef.current.delete(id);+ }+ const listed = data.filter(c => !gone.has(c.id));const merge = background && listedSessionRef.current === sessionId;listedSessionRef.current = sessionId;setChats(prev => {const live = new Map(merge ? prev.map(c => [c.id, c] as const) : []);- const fetchedIds = new Set(data.map(c => c.id));- const rows = [...data, ...prev.filter(c => merge && moved(c.id) && !fetchedIds.has(c.id))];+ const fetchedIds = new Set(listed.map(c => c.id));+ const rows = [...listed, ...prev.filter(c => merge && moved(c.id) && !fetchedIds.has(c.id))];return rows.map(c => {const row = live.get(c.id);@@ -1511,7 +1520,10 @@ export function Chats() {} else {expectSuccess(await sessionApi.deleteChat(sessionId, chat.id));showSuccessToast(t('chats.rowActions.deleted', { name }));- if (stillOn(sessionId)) setChats(prev => prev.filter(c => c.id !== chat.id));+ if (stillOn(sessionId)) {+ deletedRowsRef.current.set(chat.id, chatsRequestRef.current);+ setChats(prev => prev.filter(c => c.id !== chat.id));+ }queryClient.removeQueries({ queryKey: messagesQueryKey(sessionId, chat.id) });}if (!stillOn(sessionId)) return;