Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): apply a session status to the list it arrives with

Yudhi Armyndharisb19b878e273b
diff --git a/dashboard/src/pages/Chats.features.test.ts b/dashboard/src/pages/Chats.features.test.ts
index 68852b1d7..13479b0ac 100644
--- a/dashboard/src/pages/Chats.features.test.ts
+++ b/dashboard/src/pages/Chats.features.test.ts
@@ -1112,6 +1112,28 @@ test('with every session down at load, the first one that connects is selected a
assert.equal(selectedSessionName(container), SESSION.name, 'the selection moved to the session that connected next');
});
+test('a status that lands with the session list, before the list has rendered, is applied to that list', async () => {
+ const { screen, waitFor, act } = rtl;
+ mainDown = true;
+ listOffline = true;
+ const release = hold('sessions');
+ const { container } = renderPage();
+ // The list answer and the status land in one turn, so the status is handled before React renders the list:
+ // it must still find Main listed, not take it for an unknown session (whose re-read, still down here, would
+ // leave nothing selected).
+ await act(async () => {
+ release();
+ await new Promise(resolve => setTimeout(resolve, 0));
+ lastSocket()?.receive('message', {
+ type: 'event',
+ timestamp: new Date(1_700_003_000_000).toISOString(),
+ payload: { event: 'session.status', sessionId: SESSION.id, data: { status: 'ready' } },
+ });
+ });
+ await waitFor(() => assert.equal(selectedSessionName(container), SESSION.name));
+ await screen.findByText('Alice');
+});
+
test('a session created after the page loaded joins the switcher, and can be picked once it connects', async () => {
const { screen, fireEvent, waitFor } = rtl;
const { container } = renderPage();
diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsx
index 4a4e29550..3fdbad2b0 100644
--- a/dashboard/src/pages/Chats.tsx
+++ b/dashboard/src/pages/Chats.tsx
@@ -176,6 +176,12 @@ export function Chats() {
// Every listed session, ready or not: the switcher shows the others with their state.
const [listedSessions, setListedSessions] = useState<Session[]>([]);
const [selectedSessionId, setSelectedSessionId] = useState<string>('');
+ // The list as last applied, for a status frame to look a session up in. readSessions sets it with the list, not
+ // after the render: a status in the same turn as the list's answer must already find its session there.
+ const listedSessionsRef = useRef(listedSessions);
+ useEffect(() => {
+ listedSessionsRef.current = listedSessions;
+ });
// The sessions that can be opened, and the open one while it is down, so the switcher still shows it. Derived
// rather than kept beside the list: a copy filtered through the selection's ref dropped a session selected in the
// same turn as a list read answered, before the ref caught up.
@@ -435,6 +441,7 @@ export function Chats() {
if (request < reads.applied) return reads.list;
reads.applied = request;
reads.list = list;
+ listedSessionsRef.current = list;
setSessionsFailure(null);
setListedSessions(list);
const firstReady = list.find(s => s.status === 'ready')?.id ?? '';
@@ -978,10 +985,6 @@ export function Chats() {
// follow the live session feed the Sessions page uses (the '*' room, or one room per listed session for
// a session-scoped key) instead of the read at mount alone. The open session stays selectable when it
// drops, so the page does not switch away under the operator.
- const listedSessionsRef = useRef(listedSessions);
- useEffect(() => {
- listedSessionsRef.current = listedSessions;
- });
const sessionFeedRef = useRef(createSessionFeedState());
const [sessionFeedError, setSessionFeedError] = useState<string | null>(null);
// A session created after the list was read reports a status the list has no row for. The list is read again,