Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): cap archived media previews

Yudhi Armyndharisa1492d09234d
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.tsx b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
index c98c382b1..ec735d1a3 100644
--- a/dashboard/src/components/chats/ArchivedMediaPreview.tsx
+++ b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
@@ -9,7 +9,7 @@ interface ArchivedMediaPreviewProps {
/** Fetches the archived bytes (the per-message media route). Called at most once per mount. */
load: () => Promise<Blob>;
alt?: string;
- /** Rendered instead of the preview when the fetch fails , the download button, so the bytes stay reachable. */
+ /** Rendered instead of the preview when the fetch fails, the download button, so the bytes stay reachable. */
fallback: ReactNode;
measureMedia?: (el: Element | null) => void;
onMediaLoad?: (event?: { currentTarget: Element | null }) => void;
@@ -17,7 +17,7 @@ interface ArchivedMediaPreviewProps {
type PreviewState = { phase: 'waiting' | 'loading' | 'failed' } | { phase: 'ready'; url: string };
-// An inline preview for media whose list payload is the `{ omitted: true, archived: true }` marker ,
+// An inline preview for media whose list payload is the `{ omitted: true, archived: true }` marker,
// what MESSAGE_INLINE_MEDIA=archive leaves once the bytes are verified on the archive store. The
// archive is the only copy then, so without this an image would read as a bare "Media" button where
// it used to render. A plain over-budget marker (no `archived`) keeps the click-to-download button:
@@ -27,7 +27,7 @@ type PreviewState = { phase: 'waiting' | 'loading' | 'failed' } | { phase: 'read
// Fetched lazily, once the bubble nears the viewport, so opening a media-heavy chat does not fire one
// request per archived message at once. Where IntersectionObserver is unavailable (old browsers, the
// jsdom test runtime) it loads immediately rather than never. The object URL is revoked on unmount so
-// scrolling through a long thread does not accumulate blobs.
+// the parent's media cap unmounts older previews as the thread grows.
function ArchivedMediaPreview({ kind, load, alt, fallback, measureMedia, onMediaLoad }: ArchivedMediaPreviewProps) {
const [state, setState] = useState<PreviewState>({ phase: 'waiting' });
const [imageOpen, setImageOpen] = useState(false);
@@ -38,7 +38,7 @@ function ArchivedMediaPreview({ kind, load, alt, fallback, measureMedia, onMedia
useEffect(() => {
loadRef.current = load;
});
- // The object URL the ready render points at. Revoked on unmount only , the phase never leaves
+ // The object URL the ready render points at. Revoked on unmount only, the phase never leaves
// `ready`, so the URL stays valid for as long as the element showing it exists.
const urlRef = useRef<string | null>(null);
useEffect(
diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsx
index 0aa9c0f0f..3ed89fb79 100644
--- a/dashboard/src/components/chats/ChatThread.tsx
+++ b/dashboard/src/components/chats/ChatThread.tsx
@@ -336,7 +336,7 @@ function ChatThread({
// `archived` marks a copy MESSAGE_INLINE_MEDIA=archive moved off the row once the archive
// store held it: the bubble rendered inline before, so it previews inline still, fetched
// lazily from the same route. Documents keep the button (there is nothing to preview),
- // and so does a plain over-budget marker , see ArchivedMediaPreview for why.
+ // and so does a plain over-budget marker, see ArchivedMediaPreview for why.
const previewKind: ArchivedMediaKind | null =
msg.type === 'image' || msg.type === 'sticker'
? 'image'
@@ -349,6 +349,7 @@ function ChatThread({
if (mediaInfo.archived && previewKind && sessionId && waMessageId) {
return (
<ArchivedMediaPreview
+ key={`${sessionId}:${activeChat.id}:${waMessageId}`}
kind={previewKind}
load={() => sessionApi.getMessageMediaBlob(sessionId, activeChat.id, waMessageId)}
alt={mediaInfo.filename || t('chats.media.image')}
diff --git a/dashboard/src/utils/chatMessages.test.ts b/dashboard/src/utils/chatMessages.test.ts
index bcbb3aaa0..35d1a9708 100644
--- a/dashboard/src/utils/chatMessages.test.ts
+++ b/dashboard/src/utils/chatMessages.test.ts
@@ -358,6 +358,16 @@ import { capMediaPayloads, MEDIA_PAYLOAD_CACHE_LIMIT } from './chatMessages.ts';
const mediaMsg = (id: string, data?: string): ChatMessageView =>
msg({ id, type: 'image', metadata: { media: { mimetype: 'image/jpeg', filename: `${id}.jpg`, data } } });
+test('capMediaPayloads bounds archived previews together with inline media', () => {
+ const archive = mediaMsg('old-archive');
+ archive.metadata!.media = { mimetype: 'image/jpeg', omitted: true, archived: true };
+ const list = [archive, ...Array.from({ length: MEDIA_PAYLOAD_CACHE_LIMIT }, (_, i) => mediaMsg(`m-${i}`, 'X'))];
+ const capped = capMediaPayloads(list);
+ assert.equal(capped[0].metadata?.media?.archived, undefined);
+ assert.equal(capped[0].metadata?.media?.omitted, true);
+ assert.equal(list[0].metadata?.media?.archived, true, 'the source marker remains unchanged');
+});
+
test('capMediaPayloads: under the limit the list is returned untouched (stable reference)', () => {
const list = [mediaMsg('m-1', 'AAA'), mediaMsg('m-2', 'BBB'), msg({ id: 'm-3' })];
assert.equal(capMediaPayloads(list), list);
diff --git a/dashboard/src/utils/chatMessages.ts b/dashboard/src/utils/chatMessages.ts
index 588f1addf..25026a9be 100644
--- a/dashboard/src/utils/chatMessages.ts
+++ b/dashboard/src/utils/chatMessages.ts
@@ -67,7 +67,7 @@ export function mergeChatMessages(db: ChatMessage[], history: ChatMessage[]): Ch
* React Query cache with staleTime: Infinity, so every image/video/voice note that arrives while
* the chat is open would otherwise pin its full base64 string in heap for the whole session β€”
* scrolling through a media-rich chat grows the tab unboundedly. Past the cap the OLDEST
- * payloads are stripped to the omitted marker ({data: undefined, omitted: true}), which renders
+ * payloads and archived previews are replaced by the omitted marker, which renders
* the same πŸ“Ž placeholder as a history row fetched without media; the newest `keep` stay
* renderable (thread + lightbox). Count-based (not byte-based): payloads are bounded upstream
* by the backend's media size cap.
@@ -87,17 +87,23 @@ export const MEDIA_PAYLOAD_CACHE_LIMIT = 100;
*/
export function capMediaPayloads(list: ChatMessageView[], keep = MEDIA_PAYLOAD_CACHE_LIMIT): ChatMessageView[] {
let payloadCount = 0;
- for (const m of list) if (m.metadata?.media?.data) payloadCount++;
+ const retainsMedia = (message: ChatMessageView): boolean => {
+ const media = message.metadata?.media;
+ return Boolean(
+ media?.data || (media?.archived && HISTORY_MEDIA_TYPES.has(message.type) && message.type !== 'document'),
+ );
+ };
+ for (const m of list) if (retainsMedia(m)) payloadCount++;
if (payloadCount <= keep) return list;
const next = list.slice();
let toStrip = payloadCount - keep;
for (let i = 0; i < next.length && toStrip > 0; i++) {
const media = next[i].metadata?.media;
- if (!media?.data) continue;
+ if (!media || !retainsMedia(next[i])) continue;
next[i] = {
...next[i],
- metadata: { ...next[i].metadata, media: { ...media, data: undefined, omitted: true } },
+ metadata: { ...next[i].metadata, media: { ...media, data: undefined, archived: undefined, omitted: true } },
};
toStrip--;
}
@@ -218,7 +224,7 @@ export type MessageMedia = {
sizeBytes?: number;
/**
* Set alongside `omitted` when MESSAGE_INLINE_MEDIA=archive dropped the inline copy because the
- * chat-media archive holds the bytes , the media route serves them, so the thread previews inline.
+ * chat-media archive holds the bytes, the media route serves them, so the thread previews inline.
*/
archived?: boolean;
};