Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): open an uncached chat at its newest message

Yudhi Armyndharis3da240e4549e
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
new file mode 100644
index 000000000..2267b19bb
--- /dev/null
+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
@@ -0,0 +1,73 @@
+// The scroll-position hook mounted on a real container. jsdom has no layout and fires no scroll event
+// when a shrinking thread clamps scrollTop, so the container below models both: scrollTop is clamped
+// to the content height, and the test dispatches the scroll event a browser would.
+import '../test-helpers/register-hooks.ts';
+import { test, before, after } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+
+let rtl: typeof import('@testing-library/react');
+let useChatScrollPosition: (typeof import('./useChatScrollPosition.ts'))['useChatScrollPosition'];
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ rtl = await import('@testing-library/react');
+ ({ useChatScrollPosition } = await import('./useChatScrollPosition.ts'));
+});
+
+after(() => rtl.cleanup());
+
+const CLIENT_HEIGHT = 400;
+// A loaded thread is 100 rows; a chat still loading shows one spinner row.
+const LOADED_HEIGHT = 4600;
+const SPINNER_HEIGHT = 400;
+
+function Thread({ chatId, isLoaded }: { chatId: string; isLoaded: boolean }) {
+ const { containerRef } = useChatScrollPosition(chatId, isLoaded, false);
+ return createElement('div', { ref: containerRef, 'data-testid': 'thread' });
+}
+
+test('switching to a chat that is still loading does not save the clamped top as its position', async () => {
+ const { render, screen, fireEvent } = rtl;
+ let height = LOADED_HEIGHT;
+ let top = 0;
+ const maxTop = () => Math.max(0, height - CLIENT_HEIGHT);
+ // Defined before the first commit, so the restore effects already see this geometry.
+ const proto = window.HTMLDivElement.prototype;
+ const saved = Object.getOwnPropertyDescriptors(proto);
+ Object.defineProperties(proto, {
+ scrollHeight: { configurable: true, get: () => height },
+ clientHeight: { configurable: true, get: () => CLIENT_HEIGHT },
+ scrollTop: {
+ configurable: true,
+ get: () => Math.min(top, maxTop()),
+ set: (v: number) => {
+ top = Math.max(0, Math.min(v, maxTop()));
+ },
+ },
+ });
+ try {
+ const { rerender } = render(createElement(Thread, { chatId: 'A', isLoaded: true }));
+ const el = screen.getByTestId('thread');
+ assert.equal(el.scrollTop, maxTop(), 'chat A did not open at the bottom');
+ fireEvent.scroll(el); // the event a browser fires for that write
+ el.scrollTop = 3000; // the operator reads a little way up
+ fireEvent.scroll(el);
+
+ // Chat B is not cached: the same container swaps A's thread for the spinner, and the browser
+ // clamps scrollTop to 0 and reports it as a scroll on the next frame.
+ height = SPINNER_HEIGHT;
+ rerender(createElement(Thread, { chatId: 'B', isLoaded: false }));
+ fireEvent.scroll(el);
+
+ height = LOADED_HEIGHT;
+ rerender(createElement(Thread, { chatId: 'B', isLoaded: true }));
+ assert.equal(el.scrollTop, maxTop(), 'chat B opened at its oldest message instead of the newest');
+ } finally {
+ for (const key of ['scrollHeight', 'clientHeight', 'scrollTop'] as const) {
+ if (saved[key]) Object.defineProperty(proto, key, saved[key]);
+ else delete (proto as unknown as Record<string, unknown>)[key];
+ }
+ }
+});
diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.ts
index 2704f135c..7f9ce31f6 100644
--- a/dashboard/src/hooks/useChatScrollPosition.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.ts
@@ -137,6 +137,10 @@ export function useChatScrollPosition(
const scrollMap = useRef<Map<string, number>>(new Map());
const prevChatIdRef = useRef<string | null>(null);
const pinnedRef = useRef<boolean>(true);
+ // Whether the visible chat's messages have rendered. While a chat loads, the container shows only a
+ // spinner and the browser clamps scrollTop to it; that clamp arrives as a scroll event and is not a
+ // position to remember.
+ const loadedRef = useRef<boolean>(false);
// A 'saved' restore writes scrollTop BEFORE media decodes — the browser clamps the write to the
// still-short scrollHeight and the thread lands at the top. The saved value lives here and is
// re-applied on every media decode until the user scrolls (any genuine scroll cancels it).
@@ -188,7 +192,7 @@ export function useChatScrollPosition(
pendingRestoreRef.current = null;
pinnedRef.current = isNearBottom(el.scrollTop, el.scrollHeight, el.clientHeight);
const visibleChatId = prevChatIdRef.current;
- if (visibleChatId) scrollMap.current.set(visibleChatId, el.scrollTop);
+ if (visibleChatId && loadedRef.current) scrollMap.current.set(visibleChatId, el.scrollTop);
};
el.addEventListener('scroll', onScroll, { passive: true });
return () => el.removeEventListener('scroll', onScroll);
@@ -219,6 +223,7 @@ export function useChatScrollPosition(
}
prevChatIdRef.current = next;
+ loadedRef.current = isLoaded;
}, [activeChatId, isLoaded, pinToBottom, writeScrollTop]);
// Older messages are prepended ABOVE the viewport, so the thread grows upward and what the user