Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): tell the reader's scrolls from the thread's own

Yudhi Armyndharis387d34e66c73
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
index 98f6aca2c..4ee1fa12c 100644
--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
@@ -383,3 +383,92 @@ test('a chat left mid-thread comes back where the reader left it', async () => {
restore();
}
});
+
+// ── Which scroll events are the reader's ─────────────────────────────────────
+// A browser reports at most one scroll event per frame, however many writes the frame held, and reports the
+// clamp a content swap causes as one too. None of that is the reader moving the thread.
+
+test('a restore keeps its pending position through the scroll event the content swap raised', async () => {
+ const { render, screen, fireEvent, cleanup } = rtl;
+ cleanup();
+ const g: Geometry = { height: 4600, top: 0 };
+ const restore = installGeometry(g);
+ try {
+ const { rerender } = render(createElement(Probe, { chatId: 'A' }));
+ const el = screen.getByTestId('thread');
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 3000); // the reader's spot in A
+ rerender(createElement(Probe, { chatId: 'B' }));
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 4200); // B is left at its bottom
+
+ // Back to A, whose images have not decoded: the cached thread is short, so the browser clamps the offset B
+ // left and reports it. The restore write finds the thread already there and moves nothing.
+ g.height = 2000;
+ rerender(createElement(Probe, { chatId: 'A' }));
+ fireEvent.scroll(el);
+
+ g.height = 4600; // the images decode
+ hook().onMediaLoad();
+ await nextFrame();
+ assert.equal(el.scrollTop, 3000, 'the saved spot was lost to the clamp');
+ } finally {
+ restore();
+ }
+});
+
+test('decodes that share a frame do not turn their one scroll event into the reader scrolling', async () => {
+ const { render, screen, fireEvent, cleanup } = rtl;
+ cleanup();
+ const g: Geometry = { height: 4600, top: 0 };
+ const restore = installGeometry(g);
+ try {
+ const { rerender } = render(createElement(Probe, { chatId: 'A' }));
+ const el = screen.getByTestId('thread');
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 3000);
+ rerender(createElement(Probe, { chatId: 'B' }));
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 300);
+
+ g.height = 2000; // A again, its images not decoded
+ rerender(createElement(Probe, { chatId: 'A' }));
+ fireEvent.scroll(el);
+
+ // Two images decode in one frame. The first write moves the thread; the second finds it where the first
+ // left it. The browser reports the frame as a single event.
+ g.height = 3200;
+ hook().onMediaLoad();
+ hook().onMediaLoad();
+ await nextFrame();
+ assert.equal(el.scrollTop, 2800);
+ fireEvent.scroll(el);
+
+ g.height = 4600; // the rest decode
+ hook().onMediaLoad();
+ await nextFrame();
+ assert.equal(el.scrollTop, 3000, "the saved spot was lost to the frame's scroll event");
+ } finally {
+ restore();
+ }
+});
+
+test('a reader scrolling back to where a pin left the thread is still the reader', async () => {
+ const { render, screen, cleanup } = rtl;
+ cleanup();
+ const g: Geometry = { height: 4600, top: 4200 }; // already at the bottom, so the opening pin moves nothing
+ const restore = installGeometry(g);
+ try {
+ render(createElement(Probe, { chatId: 'A' }));
+ const el = screen.getByTestId('thread');
+ readerScrollsTo(el, 3000);
+ readerScrollsTo(el, 4200); // exactly where the pin left it: the reader's scroll, which pins the thread again
+
+ g.height = 4900; // an image decodes below
+ hook().onMediaLoad();
+ await nextFrame();
+ assert.equal(el.scrollTop, 4500, 'a reader at the bottom was not kept there');
+ } finally {
+ restore();
+ }
+});
diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.ts
index 256f281e8..b3a80c344 100644
--- a/dashboard/src/hooks/useChatScrollPosition.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.ts
@@ -167,9 +167,12 @@ export function useChatScrollPosition(
// 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).
const pendingRestoreRef = useRef<number | null>(null);
- // Marks our own writes so the scroll listener can skip them (a genuine user scroll both updates
- // the pin state / position map AND cancels pendingRestore; our writes must do neither).
- const programmaticWriteRef = useRef<boolean>(false);
+ // Where our own last write left the thread, so the scroll listener can skip the event it raised (a genuine
+ // user scroll both updates the pin state / position map AND cancels pendingRestore; our writes must do
+ // neither). That event reports the position by value, and so does the clamp event a content swap queued
+ // before a write that then moved nothing. A browser raises one event per frame however many writes the
+ // frame held, so a flag cleared by a write that moved nothing handed the frame's one event to the reader.
+ const programmaticTopRef = useRef<number | null>(null);
// See the layout effect below.
const prevScrollHeightRef = useRef<number>(0);
const awaitingOlderPageRef = useRef<boolean>(false);
@@ -181,12 +184,9 @@ export function useChatScrollPosition(
const mediaHeights = useRef(new WeakMap<Element, number>());
const writeScrollTop = useCallback((el: HTMLDivElement, top: number) => {
- const before = el.scrollTop;
- programmaticWriteRef.current = true;
el.scrollTop = top;
- // No scroll event fires when the value doesn't change (or clamps to the same value) — don't
- // leave the flag set to swallow the next genuine user scroll.
- if (el.scrollTop === before) programmaticWriteRef.current = false;
+ // The read-back, not `top`: the browser clamps a write to the content.
+ programmaticTopRef.current = el.scrollTop;
}, []);
const pinToBottom = useCallback(
@@ -218,10 +218,10 @@ export function useChatScrollPosition(
const el = containerRef.current;
if (!el) return undefined;
const onScroll = () => {
- if (programmaticWriteRef.current) {
- programmaticWriteRef.current = false;
- return;
- }
+ // Any event ends the marker's use: a reader who lands on the same offset later is the reader.
+ const ours = el.scrollTop === programmaticTopRef.current;
+ programmaticTopRef.current = null;
+ if (ours) return;
// A genuine user scroll: cancels any pending restore, then updates pin + position map.
pendingRestoreRef.current = null;
const atBottom = isNearBottom(el.scrollTop, el.scrollHeight, el.clientHeight);