Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): promote a sent chat only in its own session
Yudhi Armyndharis06f7d298ffe2
diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsxindex a20b30af4..da7782028 100644--- a/dashboard/src/components/chats/ChatComposer.tsx+++ b/dashboard/src/components/chats/ChatComposer.tsx@@ -4,7 +4,6 @@ import { useTranslation } from 'react-i18next';import { Loader2, Paperclip, Send, Smile, X } from 'lucide-react';import { messageApi, type Chat, type MessageType } from '../../services/api';import { type ChatMessageView } from '../../utils/chatMessages';-import { promoteChatWithSnippet } from '../../utils/chatList';import { buildMediaSendPayload, buildOptimisticMetadata, quotedIdOf } from '../../utils/composerSend';import { messagesQueryKey, useChatMessagesActions, upsertCachedMessage } from '../../hooks/useChatMessages';import { useRole } from '../../hooks/useRole';@@ -39,7 +38,8 @@ interface ChatComposerProps {replyingTo: ChatMessageView | null;setReplyingTo: Dispatch<SetStateAction<ChatMessageView | null>>;onMessageAppended: (direction: ScrollDirection) => void;- setChats: Dispatch<SetStateAction<Chat[]>>;+ /** Moves the chat to the top of the sidebar, if `sessionId` is still the session on screen. */+ onSent: (sessionId: string, chatId: string, snippet: string, sentAt: number) => void;messageInput: string;setMessageInput: Dispatch<SetStateAction<string>>;attachment: StagedAttachment | null;@@ -58,7 +58,7 @@ function ChatComposer({replyingTo,setReplyingTo,onMessageAppended,- setChats,+ onSent,messageInput,setMessageInput,attachment,@@ -285,7 +285,7 @@ function ChatComposer({// Named by the type the bubble uses, not the MIME major type: a PDF is a document, not "application".const snippet = currentAttachment ? typeLabel(messageTypeFromMime(currentAttachment.mimetype)) : textToSend;const sentAt = Math.floor(Date.now() / 1000);- setChats(prevChats => promoteChatWithSnippet(prevChats, activeChat.id, snippet, sentAt));+ onSent(selectedSessionId, activeChat.id, snippet, sentAt);} catch (err) {showErrorToast(t('chats.errors.send'), err instanceof Error ? err.message : undefined);updateMessage(selectedSessionId, activeChat.id, tempId, { status: 'failed' });diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex 2908e68b0..5cc7f7c1d 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -978,6 +978,42 @@ test('a staged reply is dropped when another session is opened', async () => {}});+test("a send that resolves after a session switch does not promote the other session's chat", async () => {+ const { screen, fireEvent, within, waitFor } = rtl;+ twoSessions = true;+ const releaseSend = holdSend();+ // Session 2 lists a chat with Alice's id too (a contact both accounts share), below Carol.+ chatsResponder = sessionId =>+ Promise.resolve(+ jsonResponse(sessionId === SESSION.id ? [CHAT, CHAT_2] : [CHAT_2, { ...CHAT, lastMessage: 'alice on two' }]),+ );+ try {+ resetFetchCalls();+ const { container } = renderChats();+ await screen.findByText('Main (15551234567)');+ fireEvent.click(await screen.findByText('Alice'));+ await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');+ fireEvent.change(screen.getByPlaceholderText('Type a message...'), { target: { value: 'from session one' } });+ fireEvent.click(screen.getByRole('button', { name: 'Send' }));+ await waitFor(() => assert.ok(findFetchCall('POST', `/api/sessions/${SESSION.id}/messages/send-text`)));++ fireEvent.change(container.querySelector('select.session-selector') as HTMLSelectElement, {+ target: { value: SESSION_2.id },+ });+ await screen.findByText('alice on two');+ releaseSend();+ await flush();+ await flush();++ const rows = [...container.querySelectorAll('.chat-item-card')];+ assert.equal(rows[0]?.textContent?.includes('Carol'), true, "the other session's Alice row was moved to the top");+ const alice = rows.find(row => row.textContent?.includes('Alice'));+ assert.equal(alice?.querySelector('.chat-item-snippet')?.textContent ?? null, 'alice on two');+ } finally {+ twoSessions = false;+ }+});+test('a chat list that answers after the user switched sessions does not replace the new one', async () => {const { screen, fireEvent, waitFor } = rtl;twoSessions = true;diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex a4d08484d..562a35976 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -2,7 +2,7 @@ import { useState, useEffect, useCallback, useRef, useMemo, useLayoutEffect } frimport { useQuery, useQueryClient } from '@tanstack/react-query';import { Trans, useTranslation } from 'react-i18next';import { nextReconnectState } from '../utils/reconnectState';-import { applyIncomingToChatList } from '../utils/chatList';+import { applyIncomingToChatList, promoteChatWithSnippet } from '../utils/chatList';import { filterChats, filterChannels, groupStatusesByContact } from '../utils/chatFilters';import { ArrowLeft, Loader2, Megaphone, CircleDashed, AlertCircle, MessageSquare } from 'lucide-react';import { useProfilePicture } from '../hooks/useProfilePicture';@@ -343,6 +343,14 @@ export function Chats() {[showLoadError],);+ // A send resolves after the await, possibly once another session's list is on screen. That list can+ // hold a chat with the same id (a group or contact both accounts share), so the promote only applies+ // to the session the send started in.+ const promoteSentChat = useCallback((sessionId: string, chatId: string, snippet: string, sentAt: number) => {+ if (sessionId !== chatsSessionRef.current) return;+ setChats(prev => promoteChatWithSnippet(prev, chatId, snippet, sentAt));+ }, []);+useEffect(() => {if (selectedSessionId) {void loadChats(selectedSessionId);@@ -1026,7 +1034,7 @@ export function Chats() {replyingTo={replyingTo}setReplyingTo={setReplyingTo}onMessageAppended={onMessageAppended}- setChats={setChats}+ onSent={promoteSentChat}messageInput={messageInput}setMessageInput={setMessageInput}attachment={attachment}