Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): hold a page out from the spot a restore put back

Yudhi Armyndharise9f44d362a99
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
index 18c165727..61fbe0c97 100644
--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
@@ -1410,6 +1410,48 @@ for (const scenario of [
});
}
+// A chat shown again with its page still out is held against where the restore put the reader, before the images of
+// the thread, mounted again, have decoded. One above the saved spot that decodes while the restore waits is absorbed
+// by the restore putting them back on it, so the page that lands after must not count it a second time.
+for (const scenario of [{ name: 'by the row they are on' }, { name: 'with no message row in view', noAnchors: true }]) {
+ test(`a page still out when the reader comes back lands after an image above their spot decoded ${scenario.name}`, async () => {
+ const { render, screen, fireEvent, cleanup } = rtl;
+ cleanup();
+ const plain = (rows: Row[]) => (scenario.noAnchors ? rows.map(r => ({ ...r, anchor: false as const })) : rows);
+ const t: Thread = { rows: plain(textRows('a', 46)), top: 0 };
+ t.rows[2] = { ...t.rows[2], img: 300 }; // decoded while the reader was here
+ const restore = installRows(t);
+ try {
+ const { rerender, container } = render(createElement(RowsProbe, { chatId: 'A', rows: t.rows }));
+ const el = screen.getByTestId('thread');
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 1000); // a7 at the top edge
+ hook().onOlderMessagesRequested();
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: true }));
+
+ const rowsOfA = t.rows;
+ t.rows = textRows('b', 30);
+ rerender(createElement(RowsProbe, { chatId: 'B', rows: t.rows }));
+ fireEvent.scroll(el);
+ t.rows = rowsOfA.map((r, i) => (i === 2 ? { ...r, img: 0 } : r)); // mounted again, not decoded yet
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: true }));
+ fireEvent.scroll(el);
+
+ t.rows[2] = { ...t.rows[2], img: 300 };
+ hook().onMediaLoad({ currentTarget: container.querySelector('img[data-img="a2"]') });
+ await nextFrame();
+ fireEvent.scroll(el);
+ assert.equal(el.scrollTop, 1000, 'the restore did not put the reader back on their spot');
+
+ t.rows = [...plain(textRows('p', 10)), ...t.rows]; // the page lands: 1000px above
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: false }));
+ assert.equal(el.scrollTop, 1000 + 1000, 'the image the restore absorbed was counted again by the page');
+ } finally {
+ restore();
+ }
+ });
+}
+
// A reader can stop where no row is in view (a tall element that is not a message): the landing then holds them by the
// row they were on before, not by every pixel the thread grew, growth below them included.
test('a reader who scrolls to where no message is in view while an older page is out is held by the row before', async () => {
diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.ts
index 53baf9060..b83884b33 100644
--- a/dashboard/src/hooks/useChatScrollPosition.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.ts
@@ -492,7 +492,10 @@ export function useChatScrollPosition(
if (pending !== null) {
requestAnimationFrame(() => {
const cur = containerRef.current;
- if (cur && onScreen() && pendingRestoreRef.current !== null) writeScrollTop(cur, savedSpotTop(cur, pending));
+ if (!cur || !onScreen() || pendingRestoreRef.current === null) return;
+ writeScrollTop(cur, savedSpotTop(cur, pending));
+ // What grew above the spot is absorbed by this write: a page still out is held from where it put the reader.
+ if (awaitingOlderPageRef.current) onOlderMessagesRequested();
});
return;
}
@@ -518,7 +521,7 @@ export function useChatScrollPosition(
else decoded.length = 0;
});
},
- [activeChatId, applyMediaGrowth, followBottom, writeScrollTop],
+ [activeChatId, applyMediaGrowth, followBottom, onOlderMessagesRequested, writeScrollTop],
);
return { containerRef, onMessageAppended, onMediaLoad, measureMedia, onOlderMessagesRequested };