Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): unstage the replaced file when a new one is picked
Yudhi Armyndharis5e87f95404b7
diff --git a/dashboard/src/components/chats/ChatComposer.test.ts b/dashboard/src/components/chats/ChatComposer.test.tsindex 5de7ab66d..fab4fbd5f 100644--- a/dashboard/src/components/chats/ChatComposer.test.ts+++ b/dashboard/src/components/chats/ChatComposer.test.ts@@ -1,7 +1,7 @@// A picked file is staged only once FileReader has read it. Sending text in that window must not throw the// file away: the operator saw no banner, so nothing would tell them it was dropped.import '../../test-helpers/register-hooks.ts';-import { test, before, after } from 'node:test';+import { test, before, after, afterEach } from 'node:test';import assert from 'node:assert/strict';import { createElement, useState, type ReactNode } from 'react';import type { Chat } from '../../services/api.ts';@@ -21,6 +21,9 @@ class HeldFileReader {}}+// Request paths the composer sent, in order.+const sentPaths: string[] = [];+const chat: Chat = {id: '[email protected]',name: 'Alice',@@ -37,10 +40,12 @@ before(async () => {const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');await installJsdomGlobals();(globalThis as Record<string, unknown>).FileReader = HeldFileReader;- globalThis.fetch = (() =>- Promise.resolve(+ globalThis.fetch = ((input: RequestInfo | URL) => {+ sentPaths.push(new URL(typeof input === 'string' ? input : input.toString(), 'http://localhost').pathname);+ return Promise.resolve(new Response(JSON.stringify({ messageId: 'sent-1' }), { headers: { 'Content-Type': 'application/json' } }),- )) as typeof fetch;+ );+ }) as typeof fetch;const { i18nReady } = await import('../../i18n/index.ts');await i18nReady;rtl = await import('@testing-library/react');@@ -67,6 +72,12 @@ before(async () => {({ default: ChatComposer } = await import('./ChatComposer.tsx'));});+afterEach(() => {+ rtl.cleanup();+ heldReads.length = 0;+ sentPaths.length = 0;+});+after(() => rtl.cleanup());function Harness() {@@ -109,3 +120,29 @@ test('sending text while a picked file is still being read keeps the file', asyn'the picked file was dropped',);});++test('sending while a replacement pick is still being read sends neither file and stages the new one', async () => {+ const { screen, fireEvent, act, waitFor } = rtl;+ const { container } = rtl.render(wrap(createElement(Harness)));+ const fileInput = container.querySelector('input[type="file"]')!;++ const first = new window.File(['%PDF-1.4 old'], 'old.pdf', { type: 'application/pdf' });+ fireEvent.change(fileInput, { target: { files: [first] } });+ await act(async () => heldReads[0]());+ assert.equal(container.querySelector('.preview-filename')?.textContent, 'old.pdf');++ // The replacement is still being read when the operator sends: the file it replaces must not go out.+ const second = new window.File(['%PDF-1.4 new'], 'new.pdf', { type: 'application/pdf' });+ fireEvent.change(fileInput, { target: { files: [second] } });+ fireEvent.change(container.querySelector('.message-text-input')!, { target: { value: 'see attached' } });+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));+ await waitFor(() => assert.equal(sentPaths.length, 1));+ assert.deepEqual(sentPaths, ['/api/sessions/sess-1/messages/send-text'], 'the replaced file was sent');+ await act(async () => heldReads[1]());++ assert.equal(+ container.querySelector('.preview-filename')?.textContent,+ 'new.pdf',+ 'the replacement pick was dropped',+ );+});diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsxindex 3938fef1a..45bf4039a 100644--- a/dashboard/src/components/chats/ChatComposer.tsx+++ b/dashboard/src/components/chats/ChatComposer.tsx@@ -158,6 +158,9 @@ function ChatComposer({return;}+ // The pick replaces any staged file now, so a send while it is read cannot send the one it replaced.+ // Not handleRemoveAttachment: bumping the read sequence would discard this read too.+ setAttachment(null);if (file.type.startsWith('image/')) {setPreviewUrl(URL.createObjectURL(file));} else {