Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): save a lightbox image under its file name
Yudhi Armyndharisa902764f7b6f
diff --git a/dashboard/src/components/chats/MediaLightbox.test.ts b/dashboard/src/components/chats/MediaLightbox.test.tsnew file mode 100644index 000000000..884f2e6af--- /dev/null+++ b/dashboard/src/components/chats/MediaLightbox.test.ts@@ -0,0 +1,58 @@+// Render test for the lightbox download name. The caption is message text, not a file name, and an+// empty name makes the library open the image in a new tab instead of saving it.+import '../../test-helpers/register-hooks.ts';+import { test, before, afterEach } from 'node:test';+import assert from 'node:assert/strict';+import { createElement } from 'react';+import type { LightboxItem } from './MediaLightbox.tsx';++let rtl: typeof import('@testing-library/react');+let MediaLightbox: (typeof import('./MediaLightbox.tsx'))['default'];++before(async () => {+ const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');+ await installJsdomGlobals();+ rtl = await import('@testing-library/react');+ ({ default: MediaLightbox } = await import('./MediaLightbox.tsx'));+});++afterEach(() => {+ rtl.cleanup();+});++/** Press Download and return the name the library gave the link it clicked. */+function downloadName(item: LightboxItem): string {+ const noop = () => {};+ rtl.render(createElement(MediaLightbox, { items: [item], index: 0, onClose: noop, onNavigate: noop }));+ const button = rtl.screen.getByRole('button', { name: 'Download' });+ const links: HTMLAnchorElement[] = [];+ const createElementOriginal = document.createElement;+ document.createElement = ((tag: string, options?: ElementCreationOptions) => {+ const element = createElementOriginal.call(document, tag, options);+ if (tag === 'a') {+ // Stop the synthetic click from navigating jsdom; only the name matters here.+ element.dispatchEvent = () => true;+ links.push(element as HTMLAnchorElement);+ }+ return element;+ }) as typeof document.createElement;+ try {+ rtl.fireEvent.click(button);+ } finally {+ document.createElement = createElementOriginal;+ }+ assert.equal(links.length, 1, 'expected one download link');+ return links[0].download;+}++const IMAGE = { id: 'm1', url: 'http://localhost/media/m1' };++test('a captioned image downloads under its file name, not its caption', () => {+ assert.equal(downloadName({ ...IMAGE, alt: 'Look at this!', filename: 'photo.png' }), 'photo.png');+});++test('an image with no file name falls back to a generated name, captioned or not', () => {+ assert.equal(downloadName({ ...IMAGE, alt: 'Look at this!' }), 'image-m1.jpg');+ rtl.cleanup();+ assert.equal(downloadName({ ...IMAGE, alt: '' }), 'image-m1.jpg');+});diff --git a/dashboard/src/components/chats/MediaLightbox.tsx b/dashboard/src/components/chats/MediaLightbox.tsxindex 9aa2f6270..f611e0636 100644--- a/dashboard/src/components/chats/MediaLightbox.tsx+++ b/dashboard/src/components/chats/MediaLightbox.tsx@@ -11,6 +11,8 @@ export interface LightboxItem {id: string;url: string;alt?: string;+ /** The media's own file name; the caption in `alt` is message text, never a download name. */+ filename?: string;senderName?: string;timestamp?: string;}@@ -36,7 +38,8 @@ export default function MediaLightbox({ items, index, onClose, onNavigate }: Proalt: m.alt ?? '',title: m.senderName,description: m.timestamp,- download: { url: m.url, filename: m.alt ?? `image-${m.id}.jpg` },+ // Never empty: the library opens the image in a new tab instead of saving it on an empty name.+ download: { url: m.url, filename: m.filename || `image-${m.id}.jpg` },}))}plugins={[Zoom, Download, Counter, Captions]}zoom={{diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 4ede921c2..fe3020f33 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -863,6 +863,7 @@ export function Chats() {id: m.id,url: getMediaSrc(m.metadata?.media),alt: m.body || m.metadata?.media?.filename || '',+ filename: m.metadata?.media?.filename,senderName: undefined,timestamp: formatChatTime(m.timestamp || Math.floor(new Date(m.createdAt).getTime() / 1000)),})),