Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): follow own-account messages in the open chat

Yudhi Armyndharis0aaee6e51532
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index cb349704b..427a1e222 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -3305,3 +3305,84 @@ test('a chat opened in another session starts at its newest message, not at the
twoSessions = false;
}
});
+
+// A live message in the open chat follows a reader who is at the bottom, whoever sent it: the contact, or the
+// account itself (the phone, an API client, an automation).
+for (const fromMe of [false, true]) {
+ test(`a live message ${fromMe ? 'from the account itself' : 'from the contact'} scrolls a reader at the bottom to it`, async () => {
+ const { within, waitFor } = rtl;
+ const restoreGeometry = installRoomGeometry();
+ try {
+ const { container } = renderChats();
+ const thread = await openPagedChat(container);
+ const newest = (): number => thread.scrollHeight - thread.clientHeight;
+ assert.equal(thread.scrollTop, newest(), 'a first visit did not open at the end');
+
+ receiveEvent(fromMe ? 'message.sent' : 'message.received', {
+ id: 'wamid.live.follow',
+ chatId: CHAT_2.id,
+ from: fromMe ? 'me' : CHAT_2.id,
+ to: fromMe ? CHAT_2.id : 'me',
+ body: 'live follow',
+ type: 'text',
+ fromMe,
+ timestamp: 1_700_001_500,
+ });
+ await within(thread).findByText('live follow');
+ await waitFor(() => assert.equal(thread.scrollTop, newest(), 'the new message was left below the fold'));
+ } finally {
+ restoreGeometry();
+ }
+ });
+}
+
+test('a live message from the account itself leaves a reader of older messages where they are', async () => {
+ const { fireEvent, within } = rtl;
+ const restoreGeometry = installRoomGeometry();
+ try {
+ const { container } = renderChats();
+ const thread = await openPagedChat(container);
+ thread.scrollTop = 900;
+ fireEvent.scroll(thread);
+
+ receiveEvent('message.sent', {
+ id: 'wamid.live.reading',
+ chatId: CHAT_2.id,
+ from: 'me',
+ to: CHAT_2.id,
+ body: 'live while reading',
+ type: 'text',
+ fromMe: true,
+ timestamp: 1_700_001_500,
+ });
+ await within(thread).findByText('live while reading');
+ await flush();
+ assert.equal(thread.scrollTop, 900);
+ } finally {
+ restoreGeometry();
+ }
+});
+
+test('a message sent from the page scrolls the thread to it, whatever the reader was reading', async () => {
+ const { screen, fireEvent, within, waitFor } = rtl;
+ const restoreGeometry = installRoomGeometry();
+ try {
+ const { container } = renderChats();
+ const thread = await openPagedChat(container);
+ thread.scrollTop = 900;
+ fireEvent.scroll(thread);
+
+ fireEvent.change(screen.getByPlaceholderText('Type a message...'), { target: { value: 'sent from the page' } });
+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));
+ await within(thread).findByText('sent from the page');
+ await waitFor(() =>
+ assert.equal(
+ thread.scrollTop,
+ thread.scrollHeight - thread.clientHeight,
+ 'the thread stayed where the reader was',
+ ),
+ );
+ } finally {
+ restoreGeometry();
+ }
+});
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index d61132c02..204e59092 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -759,10 +759,12 @@ export function Chats() {
// up to date so re-opening them shows fresh data without a refetch.
appendMessage(event.sessionId, newMsg.chatId, mappedMessage);
- // If the message belongs to the currently visible chat, mark-as-read and run the scroll heuristic.
+ // If the message belongs to the currently visible chat, mark-as-read and run the scroll heuristic. Own-account
+ // messages (the phone, an API client, an automation) follow a reader at the bottom like any other; a send from
+ // this page scrolled through the composer, and its echo finds the reader there already.
if (activeChat && newMsg.chatId === activeChat.id) {
markChatRead(activeChat.id);
- if (!newMsg.fromMe) onMessageAppended('incoming');
+ onMessageAppended('incoming');
}
// Update sidebar chat list. Whether the chat is missing is decided against the list on screen,