Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep a picked file when text is sent during its read
Yudhi Armyndharis92c84eca3c52
diff --git a/dashboard/src/components/chats/ChatComposer.test.ts b/dashboard/src/components/chats/ChatComposer.test.tsnew file mode 100644index 000000000..f06223c93--- /dev/null+++ b/dashboard/src/components/chats/ChatComposer.test.ts@@ -0,0 +1,110 @@+// 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 assert from 'node:assert/strict';+import { createElement, useState, type ReactNode } from 'react';+import type { Chat } from '../../services/api.ts';+import type { StagedAttachment } from './ChatComposer.tsx';++let rtl: typeof import('@testing-library/react');+let ChatComposer: (typeof import('./ChatComposer.tsx'))['default'];+let wrap: (children: ReactNode) => ReactNode;++// Reads held until the test finishes them, so a send can land while one is still in flight.+const heldReads: Array<() => void> = [];+class HeldFileReader {+ onload: ((event: { target: { result: string } }) => void) | null = null;+ readAsDataURL(): void {+ heldReads.push(() => this.onload?.({ target: { result: 'data:application/pdf;base64,JVBERi0=' } }));+ }+}++const chat: Chat = {+ id: '[email protected]',+ name: 'Alice',+ isGroup: false,+ kind: 'individual',+ unreadCount: 0,+ timestamp: 0,+ archived: false,+ pinned: false,+ muted: false,+};++before(async () => {+ const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');+ await installJsdomGlobals();+ (globalThis as Record<string, unknown>).FileReader = HeldFileReader;+ globalThis.fetch = (() =>+ Promise.resolve(+ new Response(JSON.stringify({ messageId: 'sent-1' }), { headers: { 'Content-Type': 'application/json' } }),+ )) as typeof fetch;+ const { i18nReady } = await import('../../i18n/index.ts');+ await i18nReady;+ rtl = await import('@testing-library/react');+ const { QueryClient, QueryClientProvider } = await import('@tanstack/react-query');+ const { ToastProvider } = await import('../Toast.tsx');+ const { RoleContext } = await import('../../hooks/useRole.tsx');+ const queryClient = new QueryClient();+ const role = {+ role: 'admin' as const,+ setRole: () => undefined,+ isAdmin: true,+ isOperator: false,+ isViewer: false,+ canWrite: true,+ engineType: null,+ setEngineType: () => undefined,+ };+ wrap = children =>+ createElement(+ QueryClientProvider,+ { client: queryClient },+ createElement(RoleContext.Provider, { value: role }, createElement(ToastProvider, null, children)),+ );+ ({ default: ChatComposer } = await import('./ChatComposer.tsx'));+});++after(() => rtl.cleanup());++function Harness() {+ const [messageInput, setMessageInput] = useState('');+ const [attachment, setAttachment] = useState<StagedAttachment | null>(null);+ const [previewUrl, setPreviewUrl] = useState<string | null>(null);+ const [replyingTo, setReplyingTo] = useState<null>(null);+ return createElement(ChatComposer, {+ selectedSessionId: 'sess-1',+ activeChat: chat,+ replyingTo,+ setReplyingTo,+ onMessageAppended: () => undefined,+ onSent: () => undefined,+ messageInput,+ setMessageInput,+ attachment,+ setAttachment,+ previewUrl,+ setPreviewUrl,+ });+}++test('sending text while a picked file is still being read keeps the file', async () => {+ const { screen, fireEvent, act, waitFor } = rtl;+ const { container } = rtl.render(wrap(createElement(Harness)));++ fireEvent.change(screen.getByPlaceholderText('Type a message...'), { target: { value: 'see attached' } });+ const file = new window.File(['%PDF-1.4 stub'], 'contract.pdf', { type: 'application/pdf' });+ fireEvent.change(container.querySelector('input[type="file"]')!, { target: { files: [file] } });+ assert.equal(heldReads.length, 1);++ fireEvent.click(screen.getByRole('button', { name: 'Send' }));+ await waitFor(() => assert.equal((screen.getByPlaceholderText('Type a message...') as HTMLInputElement).value, ''));+ await act(async () => heldReads[0]());++ assert.equal(+ container.querySelector('.preview-filename')?.textContent,+ 'contract.pdf',+ 'the picked file was dropped',+ );+});diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsxindex 3cf5ba212..999fc19d1 100644--- a/dashboard/src/components/chats/ChatComposer.tsx+++ b/dashboard/src/components/chats/ChatComposer.tsx@@ -239,7 +239,9 @@ function ChatComposer({const currentAttachment = attachment;const currentReplyingTo = replyingTo;- handleRemoveAttachment();+ // Clear only a file that is being sent: a pick still being read stages once it lands, so it is not+ // silently dropped by text sent in the meantime.+ if (currentAttachment) handleRemoveAttachment();setReplyingTo(null);try {