Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): keep the reader's own scroll when an older page lands
Yudhi Armyndharis0b5fc79430dc
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.tsindex d6ea4c8f5..f1b2b8f91 100644--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts@@ -141,3 +141,75 @@ test('an older page holds the reader by their message, not by every pixel the thwindow.Element.prototype.getBoundingClientRect = rectSaved;}});++// The reader's own scrolling while a page is in flight is not the content moving: it must survive the+// landing, and a reader who jumped to the bottom must still be at the bottom, now of the taller thread.+for (const scenario of [+ { name: 'scrolls down 300px', scrollTo: 700, expected: 700 + 2000 },+ { name: 'is carried to the top by momentum', scrollTo: 0, expected: 0 + 2000 },+ { name: 'jumps to the bottom', scrollTo: 900, expected: 900 + 2000 },+]) {+ test(`an older page keeps a position the reader moved to while it was in flight: ${scenario.name}`, async () => {+ const { render, screen, cleanup } = rtl;+ cleanup();+ const top = { value: 0 };+ const messageTops = new Map<string, number>([+ ['m1', 100],+ ['m2', 500],+ ['m3', 900],+ ]);+ let height = 1300;+ const proto = window.HTMLDivElement.prototype;+ const saved = Object.getOwnPropertyDescriptors(proto);+ const rectSaved = window.Element.prototype.getBoundingClientRect;+ Object.defineProperties(proto, {+ scrollHeight: { configurable: true, get: () => height },+ clientHeight: { configurable: true, get: () => CLIENT_HEIGHT },+ scrollTop: {+ configurable: true,+ get: () => top.value,+ set: (v: number) => {+ top.value = Math.max(0, Math.min(v, height - CLIENT_HEIGHT));+ },+ },+ });+ window.Element.prototype.getBoundingClientRect = function (this: Element) {+ const content = messageTops.get(this.getAttribute('data-id') ?? '');+ const y = content === undefined ? 0 : content - top.value;+ return { top: y, bottom: y + 300, left: 0, right: 0, width: 0, height: 300, x: 0, y } as DOMRect;+ };+ const request: MutableRefObject<(() => void) | null> = { current: null };+ function Older({ fetching }: { fetching: boolean }) {+ const { containerRef, onOlderMessagesRequested } = useChatScrollPosition('A', true, fetching);+ useEffect(() => {+ request.current = onOlderMessagesRequested;+ });+ return createElement(+ 'div',+ { ref: containerRef, 'data-testid': 'thread' },+ ['m1', 'm2', 'm3'].map(id => createElement('div', { key: id, 'data-id': id, 'data-scroll-anchor': '' })),+ );+ }+ try {+ const { rerender } = render(createElement(Older, { fetching: false }));+ const el = screen.getByTestId('thread');+ top.value = 80; // near the top, where an older page is requested+ request.current?.();+ rerender(createElement(Older, { fetching: true }));++ // The reader moves while the page is out, then it lands with 2000px of history above.+ top.value = scenario.scrollTo;+ for (const [id, y] of [...messageTops]) messageTops.set(id, y + 2000);+ height += 2000;+ rerender(createElement(Older, { fetching: false }));++ assert.equal(el.scrollTop, scenario.expected, `the reader ended at ${el.scrollTop}`);+ } 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];+ }+ window.Element.prototype.getBoundingClientRect = rectSaved;+ }+ });+}diff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.tsindex df452a3a1..31aa9406e 100644--- a/dashboard/src/hooks/useChatScrollPosition.ts+++ b/dashboard/src/hooks/useChatScrollPosition.ts@@ -123,15 +123,18 @@ export function correctionForMediaGrowth(export const SCROLL_ANCHOR_ATTRIBUTE = 'data-scroll-anchor';/**- * The first anchor element the reader can see, with its offset from the container's top edge: the- * thing an older page landing above must hold still, whatever grew elsewhere in the thread meanwhile.+ * The first anchor element the reader can see, with its offset inside the thread's content (from the+ * top of what scrolls, not from the visible top edge): the thing an older page landing above must hold+ * still, whatever grew elsewhere in the thread meanwhile. In content coordinates the element's offset+ * changes only when something above it grew, so a reader who scrolls while the page is in flight is not+ * counted as the content moving.*/function firstVisibleAnchor(container: HTMLElement): { node: Element; top: number } | null {const containerTop = container.getBoundingClientRect().top;for (const child of Array.from(container.children)) {if (!child.hasAttribute(SCROLL_ANCHOR_ATTRIBUTE)) continue;const rect = child.getBoundingClientRect();- if (rect.bottom > containerTop) return { node: child, top: rect.top - containerTop };+ if (rect.bottom > containerTop) return { node: child, top: rect.top - containerTop + container.scrollTop };}return null;}@@ -273,7 +276,7 @@ export function useChatScrollPosition(olderAnchorRef.current = null;const grew =anchor && el.contains(anchor.node)- ? anchor.node.getBoundingClientRect().top - el.getBoundingClientRect().top - anchor.top+ ? anchor.node.getBoundingClientRect().top - el.getBoundingClientRect().top + el.scrollTop - anchor.top: el.scrollHeight - prevScrollHeightRef.current;if (grew > 0) {const corrected = el.scrollTop + grew;