Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): scroll to a sent message while a restore waits
Yudhi Armyndharisd2fd0f3ad3cc
diff --git a/dashboard/src/hooks/useChatScrollPosition.render.test.ts b/dashboard/src/hooks/useChatScrollPosition.render.test.tsindex 9468199c4..376724ae0 100644--- a/dashboard/src/hooks/useChatScrollPosition.render.test.ts+++ b/dashboard/src/hooks/useChatScrollPosition.render.test.ts@@ -726,6 +726,43 @@ test('a live message in the decode window of a restored chat does not erase the}});+// The reader's own send is an explicit act: it scrolls to the new row even in a chat that came back at its saved spot+// and is still waiting for its media. The row is where they are now, so the spot is not theirs to be put back on.+test('a message the reader sends in a chat reopened at its saved spot scrolls to it, and no decode takes them back', 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);+ rerender(createElement(Probe, { chatId: 'A' })); // back at the spot, the restore still pending+ fireEvent.scroll(el);+ assert.equal(el.scrollTop, 3000);++ await liveMessage(g, rerender, 'outgoing', 300);+ assert.equal(el.scrollTop, g.height - CLIENT_HEIGHT, 'the sent message was left below the fold');+ fireEvent.scroll(el);++ g.height += 400; // an image decodes+ hook().onMediaLoad();+ await nextFrame();+ assert.equal(el.scrollTop, g.height - CLIENT_HEIGHT, 'a decode took the reader back to the saved spot');+ fireEvent.scroll(el);++ rerender(createElement(Probe, { chatId: 'B' })); // the reader leaves A at its bottom+ fireEvent.scroll(el);+ rerender(createElement(Probe, { chatId: 'A' }));+ assert.equal(el.scrollTop, g.height - CLIENT_HEIGHT, 'A reopened at the old spot instead of at the bottom');+ } 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 b63db4062..47850475e 100644--- a/dashboard/src/hooks/useChatScrollPosition.ts+++ b/dashboard/src/hooks/useChatScrollPosition.ts@@ -206,10 +206,12 @@ export function useChatScrollPosition();// A pin that follows the thread (a live message, a decoded image) forgets the chat's remembered offset too: the- // reader is at the bottom again, and the offset from before the thread grew would bring them back above it.+ // reader is at the bottom again, and the offset from before the thread grew would bring them back above it. For+ // the same reason a saved spot still waiting for media is no longer theirs to be put back on.const followBottom = useCallback((el: HTMLDivElement) => {pinToBottom(el);+ pendingRestoreRef.current = null;if (prevChatIdRef.current !== null) scrollMap.current.delete(prevChatIdRef.current);},[pinToBottom],@@ -333,8 +335,12 @@ 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, or it is on its way to a saved spot.- if (follow.chat !== prevChatIdRef.current || pendingRestoreRef.current !== null) {+ // The chat the message was announced in is not the one on screen any more, or the message is not the reader's own+ // and the chat is on its way to a saved spot.+ if (+ follow.chat !== prevChatIdRef.current ||+ (follow.direction === 'incoming' && pendingRestoreRef.current !== null)+ ) {pendingFollowRef.current = null;return;}@@ -354,9 +360,11 @@ 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. 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;+ // that is still on its way to a saved spot, for a message that arrives: 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. A message the+ // reader sends themselves always scrolls, saved spot or not.+ if (!el || !loadedRef.current) return;+ 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 };diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 99806e185..0df2716c1 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -3479,6 +3479,36 @@ test('a message delivered twice leaves a reader just above the bottom where they}});+test('a message sent from the page scrolls a chat reopened at its saved spot to it', async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ const restoreGeometry = installRoomGeometry();+ try {+ const { container } = renderChats();+ const thread = await openPagedChat(container);+ thread.scrollTop = 900; // the reader's spot in Carol's thread+ fireEvent.scroll(thread);++ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-header') as HTMLElement).findByText('Alice');+ fireEvent.click(await screen.findByText('Carol'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('paged message 119');+ assert.equal(thread.scrollTop, 900, 'Carol did not reopen at the spot the reader left');++ fireEvent.change(screen.getByPlaceholderText('Type a message...'), { target: { value: 'sent at the saved spot' } });+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));+ await within(thread).findByText('sent at the saved spot');+ await waitFor(() =>+ assert.equal(+ thread.scrollTop,+ thread.scrollHeight - thread.clientHeight,+ 'the sent message was left below the fold',+ ),+ );+ } finally {+ restoreGeometry();+ }+});+// ── Live feed subscription ───────────────────────────────────────────────────const MESSAGE_FEED_EVENTS = [