Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep a message written while a chat's first page loads
Yudhi Armyndharis4e3b5f1ec671
diff --git a/dashboard/src/hooks/useChatMessages.ts b/dashboard/src/hooks/useChatMessages.tsindex 751b1a609..4c75577e2 100644--- a/dashboard/src/hooks/useChatMessages.ts+++ b/dashboard/src/hooks/useChatMessages.ts@@ -167,7 +167,9 @@ function scheduleReplayOnSettle(queryClient: QueryClient, key: MessagesQueryKey)* Apply one change to a chat's paged cache, recording it for replay when a page is in flight.** Never seeds a slice: an entry created here would be "fresh" under staleTime: Infinity, so opening- * the chat would skip the queryFn and show this write alone.+ * the chat would skip the queryFn and show this write alone. A write while the FIRST page is in+ * flight has no data to land on, so it is only queued, then replayed onto that page when it+ * settles; the composer is live before the thread loads, and dropping the write lost the message.** `apply` must be idempotent: a replay can re-run it against a cache that already reflects it.*/@@ -177,7 +179,7 @@ function writeMessagesCache(apply: (data: MessagesData) => MessagesData,): void {const state = queryClient.getQueryState<MessagesData>(key);- if (state?.data === undefined) return;+ if (state === undefined || (state.data === undefined && state.fetchStatus === 'idle')) return;queryClient.setQueryData<MessagesData>(key, old => (old === undefined ? undefined : apply(old)));// Anything but `idle` still has a page coming that will overwrite this write when it lands, so// it has to be queued. `paused` is the case a bare `=== 'fetching'` test misses: the browserdiff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex c4e1df987..219f0fd9c 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -155,6 +155,9 @@ function holdOlderPage(): () => void {// Hold a text send open, so a test can land it at a chosen moment relative to an older-page fetch.let sendGate: Promise<void> | null = null;+// Hold Alice's first page open, so a test can write to the thread before it has any data.+let firstPageGate: Promise<void> | null = null;+function holdSend(): () => void {let release!: () => void;sendGate = new Promise<void>(resolve => {@@ -294,9 +297,9 @@ function installFetchStub(): void {const gate = isFirstPage ? null : olderPageGate;return gate ? gate.then(answer) : Promise.resolve(answer());}- return Promise.resolve(- jsonResponse({ messages: [DB_MESSAGE, OMITTED_MEDIA_MESSAGE, OMITTED_MEDIA_MESSAGE_2], total: 3 }),- );+ const firstPage = () =>+ jsonResponse({ messages: [DB_MESSAGE, OMITTED_MEDIA_MESSAGE, OMITTED_MEDIA_MESSAGE_2], total: 3 });+ return firstPageGate ? firstPageGate.then(firstPage) : Promise.resolve(firstPage());}// The media route answers bytes, not JSON — Content-Disposition: attachment.if (method === 'GET' && (path === MEDIA_PATH || path === MEDIA_PATH_2)) {@@ -373,6 +376,7 @@ afterEach(() => {mediaGates.clear();olderPageGate = null;sendGate = null;+ firstPageGate = null;olderPageFails = false;chatsResponder = null;});@@ -1308,3 +1312,28 @@ test('a send that reconciles after the older page has already settled does not rconst icon = bubbles[0].closest('.message-bubble')?.querySelector('.message-status-icon');assert.ok(icon?.classList.contains('sent'), 'expected the reconciled (sent) row, not a reverted pending ghost');});++test('a message sent while the first page is still loading survives the page landing', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ resetFetchCalls();+ let releaseFirstPage!: () => void;+ firstPageGate = new Promise<void>(resolve => {+ releaseFirstPage = resolve;+ });+ const { container } = renderChats();++ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ // The composer is live before the thread has loaded, so a send can land in a cache with no data.+ fireEvent.change(await screen.findByPlaceholderText('Type a message...'), {+ target: { value: 'sent while loading' },+ });+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));+ await waitFor(() => assert.ok(findFetchCall('POST', `/api/sessions/${SESSION.id}/messages/send-text`)));+ await flush();++ releaseFirstPage();+ const thread = container.querySelector('.room-messages') as HTMLElement;+ await within(thread).findByText('hello from alice');+ await waitFor(() => assert.equal(within(thread).queryAllByText('sent while loading').length, 1));+});