Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): load archived media ahead of the scroll position

Yudhi Armyndharis5ea078477f87
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts b/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
index 5823771c4..7e38f461b 100644
--- a/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
+++ b/dashboard/src/components/chats/ArchivedMediaPreview.test.ts
@@ -133,3 +133,35 @@ test('unmount aborts an archived media request still waiting for its bytes', asy
assert.equal(signal!.aborted, true);
assert.deepEqual(created, []);
});
+
+test('the lazy-load observer measures against the thread scroll container, so its lead is not clipped', async () => {
+ const seen: Array<{ root: unknown; rootMargin: string | undefined }> = [];
+ class FakeObserver {
+ constructor(_callback: IntersectionObserverCallback, options?: IntersectionObserverInit) {
+ seen.push({ root: options?.root, rootMargin: options?.rootMargin });
+ }
+ observe(): void {}
+ unobserve(): void {}
+ disconnect(): void {}
+ takeRecords(): IntersectionObserverEntry[] {
+ return [];
+ }
+ }
+ const scroller = document.createElement('div');
+ const target = globalThis as { IntersectionObserver?: unknown };
+ target.IntersectionObserver = FakeObserver;
+ try {
+ rtl.render(
+ createElement(ArchivedMediaPreview, {
+ kind: 'image',
+ fallback,
+ load: () => new Promise<Blob>(() => undefined),
+ scrollRootRef: { current: scroller },
+ }),
+ );
+ // Without the scroller as root, the implicit viewport root clips the margin to the scroller's box.
+ assert.deepEqual(seen, [{ root: scroller, rootMargin: '600px 0px' }]);
+ } finally {
+ delete target.IntersectionObserver;
+ }
+});
diff --git a/dashboard/src/components/chats/ArchivedMediaPreview.tsx b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
index a58410cbd..2d5caa187 100644
--- a/dashboard/src/components/chats/ArchivedMediaPreview.tsx
+++ b/dashboard/src/components/chats/ArchivedMediaPreview.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useRef, useState, type ReactNode } from 'react';
+import { useEffect, useRef, useState, type ReactNode, type RefObject } from 'react';
import { Loader2 } from 'lucide-react';
import MediaLightbox from './MediaLightbox';
@@ -9,6 +9,11 @@ interface ArchivedMediaPreviewProps {
/** Fetches the archived bytes (the per-message media route). Called at most once per mount. */
load: (signal: AbortSignal) => Promise<Blob>;
alt?: string;
+ /**
+ * The thread's scroll container. The lazy-load observer measures against it: with the implicit viewport
+ * root the margin below is clipped to the container's own box and the lead never happens.
+ */
+ scrollRootRef?: RefObject<Element | null>;
/** Rendered instead of the preview when the fetch fails, the download button, so the bytes stay reachable. */
fallback: ReactNode;
measureMedia?: (el: Element | null) => void;
@@ -28,7 +33,15 @@ type PreviewState = { phase: 'waiting' | 'loading' | 'failed' } | { phase: 'read
// 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
// the parent's media cap unmounts older previews as the thread grows.
-function ArchivedMediaPreview({ kind, load, alt, fallback, measureMedia, onMediaLoad }: ArchivedMediaPreviewProps) {
+function ArchivedMediaPreview({
+ kind,
+ load,
+ alt,
+ scrollRootRef,
+ fallback,
+ measureMedia,
+ onMediaLoad,
+}: ArchivedMediaPreviewProps) {
const [state, setState] = useState<PreviewState>({ phase: 'waiting' });
const [imageOpen, setImageOpen] = useState(false);
const anchorRef = useRef<HTMLDivElement | null>(null);
@@ -65,11 +78,11 @@ function ArchivedMediaPreview({ kind, load, alt, fallback, measureMedia, onMedia
}
},
// A screen's worth of lead so the bytes are usually there by the time the bubble scrolls in.
- { rootMargin: '600px 0px' },
+ { root: scrollRootRef?.current ?? null, rootMargin: '600px 0px' },
);
observer.observe(el);
return () => observer.disconnect();
- }, [state.phase]);
+ }, [state.phase, scrollRootRef]);
// Phase 2: fetch once. A response landing after unmount is dropped rather than turned into a URL
// nothing would ever revoke.
diff --git a/dashboard/src/components/chats/ChatThread.tsx b/dashboard/src/components/chats/ChatThread.tsx
index 87bfab15e..4ffcce9a8 100644
--- a/dashboard/src/components/chats/ChatThread.tsx
+++ b/dashboard/src/components/chats/ChatThread.tsx
@@ -354,6 +354,7 @@ function ChatThread({
kind={previewKind}
load={signal => sessionApi.getMessageMediaBlob(sessionId, activeChat.id, waMessageId, signal)}
alt={mediaInfo.filename || t('chats.media.image')}
+ scrollRootRef={messagesContainerRef}
fallback={downloadButton}
measureMedia={measureMedia}
onMediaLoad={onMediaLoad}