Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not follow a live message while a restore is pending
Yudhi Armyndharis414d4c112c0e
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.tsindex 989f587bd..775c2815d 100644--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts@@ -687,6 +687,45 @@ test('a thread pinned to the bottom stays there as media decodes, until the read}});+// A chat comes back at its saved spot while its media has not decoded: the short thread clamps the write, so it reads+// as at the bottom. A live message announced in that window is not the reader following the conversation.+test('a live message in the decode window of a restored chat does not erase the saved spot', 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);++ g.height = 2000; // back to A before its media has decoded: the write clamps+ rerender(createElement(Probe, { chatId: 'A' }));+ fireEvent.scroll(el);+ assert.equal(el.scrollTop, 1600);++ hook().onMessageAppended('incoming');+ g.height = 2070;+ rerender(createElement(Probe, { chatId: 'A' })); // its row commits++ g.height = 4670; // the media decodes and the saved spot is reached+ hook().onMediaLoad();+ await nextFrame();+ assert.equal(el.scrollTop, 3000, 'the decode did not return the reader to the saved spot');+ fireEvent.scroll(el);++ rerender(createElement(Probe, { chatId: 'B' })); // the reader leaves again without scrolling+ fireEvent.scroll(el);+ rerender(createElement(Probe, { chatId: 'A' }));+ assert.equal(el.scrollTop, 3000, `A reopened at ${el.scrollTop}, not at the spot the reader was reading`);+ } finally {+ restore();+ }+});+// ── Older pages landing under a position the memory holds ────────────────────// The echo of a send from the page can reach it after the composer reconciled its placeholder: the cache writediff --git a/dashboard/src/hooks/useChatScrollPosition.ts b/dashboard/src/hooks/useChatScrollPosition.tsindex f5d18efea..f46d97668 100644--- a/dashboard/src/hooks/useChatScrollPosition.ts+++ b/dashboard/src/hooks/useChatScrollPosition.ts@@ -328,8 +328,8 @@ export function useChatScrollPosition(const follow = pendingFollowRef.current;const el = containerRef.current;if (!follow || !el) return;- // The chat the message was announced in is not the one on screen any more.- if (follow.chat !== prevChatIdRef.current) {+ // The chat the message was announced in is not the one on screen any more, or it is on its way to a saved spot.+ if (follow.chat !== prevChatIdRef.current || pendingRestoreRef.current !== null) {pendingFollowRef.current = null;return;}@@ -348,8 +348,10 @@ export function useChatScrollPosition(const onMessageAppended = useCallback((direction: ScrollDirection) => {const el = containerRef.current;- // A chat that has not rendered its thread has nothing to follow: the restore decides where it opens.- if (!el || !loadedRef.current) return;+ // A chat that has not rendered its thread has nothing to follow: the restore decides where it opens. Nor has one+ // that is still on its way to a saved spot: the short, undecoded thread reads as at the bottom, and following+ // it would drop the saved spot the next decode puts the reader back on.+ if (!el || !loadedRef.current || 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 };