Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): follow a message sent while an older page loads

Yudhi Armyndharise8984f689ba0
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
index cfd4295a8..338c68044 100644
--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts
@@ -913,3 +913,118 @@ test('a decode after an older page landed does not apply the saved spot from bef
restore();
}
});
+
+// ── A thread of message rows ─────────────────────────────────────────────────
+// Rows laid out top to bottom in the container, each an anchor; a row may hold an image, which the test grows as it
+// decodes. Positions are what a browser would report for them at the current scrollTop.
+
+type Row = { id: string; h: number; img?: number };
+type Thread = { rows: Row[]; top: number };
+
+const rowsHeight = (t: Thread) => t.rows.reduce((sum, r) => sum + r.h + (r.img ?? 0), 0);
+const rowsMax = (t: Thread) => Math.max(0, rowsHeight(t) - CLIENT_HEIGHT);
+const textRows = (prefix: string, n: number, h = 100): Row[] =>
+ Array.from({ length: n }, (_, i) => ({ id: `${prefix}${i}`, h }));
+
+function installRows(t: Thread): () => void {
+ const proto = window.HTMLDivElement.prototype;
+ const saved = Object.getOwnPropertyDescriptors(proto);
+ const rectSaved = window.Element.prototype.getBoundingClientRect;
+ Object.defineProperties(proto, {
+ scrollHeight: { configurable: true, get: () => rowsHeight(t) },
+ clientHeight: { configurable: true, get: () => CLIENT_HEIGHT },
+ scrollTop: {
+ configurable: true,
+ get: () => Math.min(t.top, rowsMax(t)),
+ set: (v: number) => {
+ t.top = Math.max(0, Math.min(v, rowsMax(t)));
+ },
+ },
+ });
+ const box = (y: number, h: number) =>
+ ({ top: y, bottom: y + h, left: 0, right: 0, width: 0, height: h, x: 0, y }) as DOMRect;
+ window.Element.prototype.getBoundingClientRect = function (this: Element) {
+ const id = this.getAttribute('data-id') ?? this.getAttribute('data-img');
+ let y = -Math.min(t.top, rowsMax(t));
+ for (const r of t.rows) {
+ if (r.id === id) return this.hasAttribute('data-img') ? box(y + r.h, r.img ?? 0) : box(y, r.h + (r.img ?? 0));
+ y += r.h + (r.img ?? 0);
+ }
+ return box(0, CLIENT_HEIGHT); // the container
+ };
+ return () => {
+ 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];
+ }
+ window.Element.prototype.getBoundingClientRect = rectSaved;
+ };
+}
+
+function RowsProbe({ chatId, rows, fetching = false }: { chatId: string; rows: Row[]; fetching?: boolean }) {
+ const result = useChatScrollPosition(chatId, true, fetching);
+ useEffect(() => {
+ hookRef.current = result;
+ });
+ return createElement(
+ 'div',
+ { ref: result.containerRef, 'data-testid': 'thread' },
+ rows.map(r =>
+ createElement(
+ 'div',
+ { key: r.id, 'data-id': r.id, 'data-scroll-anchor': '' },
+ r.img === undefined ? null : createElement('img', { ref: result.measureMedia, 'data-img': r.id }),
+ ),
+ ),
+ );
+}
+
+// The reader sends while an older page is out, and the page lands in the commit that adds their row: what they sent
+// is where they are taken, not the message the page holds them on.
+test('a message the reader sends while an older page is out is followed when the page lands with its row', async () => {
+ const { render, screen, fireEvent, cleanup } = rtl;
+ cleanup();
+ const t: Thread = { rows: textRows('a', 46), top: 0 };
+ const restore = installRows(t);
+ try {
+ const { rerender } = render(createElement(RowsProbe, { chatId: 'A', rows: t.rows }));
+ const el = screen.getByTestId('thread');
+ fireEvent.scroll(el);
+ readerScrollsTo(el, 50); // near the top: an older page is asked for
+ hook().onOlderMessagesRequested();
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: true }));
+
+ hook().onMessageAppended('outgoing');
+ t.rows = [...textRows('p', 10), ...t.rows, { id: 'own', h: 100 }];
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: false }));
+ assert.equal(el.scrollTop, rowsMax(t), 'the message the reader sent was left below the fold');
+ } finally {
+ restore();
+ }
+});
+
+// A follow the thread never consumed (the message was announced but added no row) is not the reader's: it must not
+// take them to the bottom when a page lands under them later, whichever way the message went.
+test('a message announced before the reader went back for history does not pin them when the page lands', async () => {
+ const { render, screen, fireEvent, cleanup } = rtl;
+ cleanup();
+ const t: Thread = { rows: textRows('a', 46), top: 0 };
+ const restore = installRows(t);
+ try {
+ const { rerender } = render(createElement(RowsProbe, { chatId: 'A', rows: t.rows }));
+ const el = screen.getByTestId('thread');
+ fireEvent.scroll(el);
+ hook().onMessageAppended('outgoing');
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows })); // no row was added
+ await nextFrame();
+
+ readerScrollsTo(el, 50);
+ hook().onOlderMessagesRequested();
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: true }));
+ t.rows = [...textRows('p', 10), ...t.rows];
+ rerender(createElement(RowsProbe, { chatId: 'A', rows: t.rows, fetching: false }));
+ assert.equal(el.scrollTop, 50 + 1000, 'the landing page took the reader to the bottom');
+ } finally {
+ restore();
+ }
+});
diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.ts
index e6ecf7c35..0b96cec7a 100644
--- a/dashboard/src/hooks/useChatScrollPosition.ts
+++ b/dashboard/src/hooks/useChatScrollPosition.ts
@@ -177,10 +177,13 @@ export function useChatScrollPosition(
// 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);
// A live message the thread is to follow, until the commit that adds its row applies it (see the layout effect
- // after the older-page landing).
- const pendingFollowRef = useRef<{ chat: string | null; direction: ScrollDirection; geometry: ScrollGeometry } | null>(
- null,
- );
+ // after the older-page landing). `duringPage`: announced while an older page was out.
+ const pendingFollowRef = useRef<{
+ chat: string | null;
+ direction: ScrollDirection;
+ geometry: ScrollGeometry;
+ duringPage: boolean;
+ } | null>(null);
// See the layout effect below.
const prevScrollHeightRef = useRef<number>(0);
const awaitingOlderPageRef = useRef<boolean>(false);
@@ -300,8 +303,10 @@ export function useChatScrollPosition(
awaitingOlderPageRef.current = false;
// A follow still armed belongs to a message announced before the reader went back for history; the page that
// lands is not its row, and consuming it here would take the reader from the message they were held on to the
- // bottom.
- pendingFollowRef.current = null;
+ // bottom. A message the reader sent while the page was out is the exception: its row can land in this same commit,
+ // and the follow below takes them to it.
+ const follow = pendingFollowRef.current;
+ if (!(follow?.direction === 'outgoing' && follow.duringPage)) pendingFollowRef.current = null;
// How far the element the reader was on moved down. Measured on the element, not as a change of the
// whole thread's height: a preview that swapped in below the reader, or a live message at the
// bottom, while the page was in flight is inside that height delta and moves nothing on screen, so
@@ -367,7 +372,12 @@ export function useChatScrollPosition(
if (direction === 'incoming' && pendingRestoreRef.current !== null) return;
const geometry = { scrollTop: el.scrollTop, scrollHeight: el.scrollHeight, clientHeight: el.clientHeight };
if (decideScroll(direction, geometry) === 'preserve') return;
- pendingFollowRef.current = { chat: prevChatIdRef.current, direction, geometry };
+ pendingFollowRef.current = {
+ chat: prevChatIdRef.current,
+ direction,
+ geometry,
+ duringPage: awaitingOlderPageRef.current,
+ };
}, []);
// Media has no layout box before it decodes. While pinned, each decode re-pins to the bottom;