Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): drop the earlier status image on an oversized pick

Yudhi Armyndharis005773fa3b59
diff --git a/dashboard/src/components/chats/StatusComposeModal.test.ts b/dashboard/src/components/chats/StatusComposeModal.test.ts
index 21adcae41..a007a84db 100644
--- a/dashboard/src/components/chats/StatusComposeModal.test.ts
+++ b/dashboard/src/components/chats/StatusComposeModal.test.ts
@@ -1,7 +1,7 @@
// An image status is posted as base64 JSON, so a file past the upload cap is refused only after the whole
// inflated body went up. The picker rejects it before reading, like the chat composer does.
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 } from 'react';
@@ -10,17 +10,28 @@ let render: () => ReturnType<typeof import('@testing-library/react').render>;
let queryClient: import('@tanstack/react-query').QueryClient;
let reads = 0;
+// Reads count, then finish on the next microtask with a data URL carrying the file's type.
class CountingFileReader {
- onload: (() => void) | null = null;
- readAsDataURL(): void {
+ onload: ((event: { target: { result: string } }) => void) | null = null;
+ readAsDataURL(file: Blob): void {
reads += 1;
+ queueMicrotask(() => this.onload?.({ target: { result: `data:${file.type};base64,eA==` } }));
}
}
+// Bodies the modal posted, in order.
+const posted: unknown[] = [];
+
before(async () => {
const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');
await installJsdomGlobals();
(globalThis as Record<string, unknown>).FileReader = CountingFileReader;
+ globalThis.fetch = ((_input: RequestInfo | URL, init?: RequestInit) => {
+ posted.push(JSON.parse(String(init?.body)));
+ return Promise.resolve(
+ new Response(JSON.stringify({ id: 'status-1' }), { headers: { 'Content-Type': 'application/json' } }),
+ );
+ }) as typeof fetch;
const { i18nReady } = await import('../../i18n/index.ts');
await i18nReady;
rtl = await import('@testing-library/react');
@@ -57,6 +68,12 @@ before(async () => {
);
});
+afterEach(() => {
+ rtl.cleanup();
+ reads = 0;
+ posted.length = 0;
+});
+
after(() => {
rtl.cleanup();
// Drops the unmounted query's five-minute garbage-collection timer, which would hold the runner open.
@@ -74,3 +91,20 @@ test('an image past the upload cap is refused before it is read', async () => {
await screen.findByText('File is too large (max 18 MB)');
assert.equal(reads, 0, 'the oversized file was read');
});
+
+test('an oversized pick also drops the earlier pick it was meant to replace', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ render();
+ fireEvent.click(screen.getByRole('button', { name: 'Image' }));
+ const input = screen.getByLabelText('Status image');
+ fireEvent.change(input, { target: { files: [new window.File(['x'], 'a.jpg', { type: 'image/jpeg' })] } });
+ const post = screen.getByRole('button', { name: 'Post' }) as HTMLButtonElement;
+ await waitFor(() => assert.equal(post.disabled, false));
+
+ const huge = new window.File(['x'], 'huge.jpg', { type: 'image/jpeg' });
+ Object.defineProperty(huge, 'size', { value: 18 * 1024 * 1024 + 1 });
+ fireEvent.change(input, { target: { files: [huge] } });
+ await screen.findByText('File is too large (max 18 MB)');
+ // The input now reads "No file chosen", so nothing on screen shows the earlier image any more.
+ assert.equal(post.disabled, true, 'the hidden earlier pick can still be posted');
+});
diff --git a/dashboard/src/components/chats/StatusComposeModal.tsx b/dashboard/src/components/chats/StatusComposeModal.tsx
index c0c652d76..48391a3b9 100644
--- a/dashboard/src/components/chats/StatusComposeModal.tsx
+++ b/dashboard/src/components/chats/StatusComposeModal.tsx
@@ -105,9 +105,12 @@ function StatusComposeModal({ sessionId, onClose, onPosted }: Props) {
const file = e.target.files?.[0];
if (!file) return;
// Posted as base64 JSON, so an oversized image would only be refused after the whole inflated
- // body went up. Rejected before anything is cleared, so an image URL or earlier pick is kept.
+ // body went up. Rejected before it is read; an image URL stays in its own field, but an earlier pick
+ // is dropped (and a read of it cancelled), since the cleared input no longer shows it.
if (file.size > MEDIA_UPLOAD_MAX_BYTES) {
showErrorToast(t('chats.errors.fileTooLarge'));
+ composeImageReadSeq.current += 1;
+ setComposeImageBase64(null);
e.target.value = '';
return;
}