Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): re-read sessions when the feed connects after a failed load

Yudhi Armyndharis023e4056dc98
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.ts
index a3431c0e3..8af0acc7d 100644
--- a/dashboard/src/pages/Sessions.test.ts
+++ b/dashboard/src/pages/Sessions.test.ts
@@ -543,6 +543,29 @@ test('Refresh on a feed that never connected re-reads the list once the socket i
assert.equal(screen.queryByRole('alert') === null, true, 'the feed banner is still shown');
});
+test('a failed mount read is retried when the feed first connects after its own retries', async () => {
+ const { screen, waitFor, act } = rtl;
+ resetFetchCalls();
+ sessionListFailures = 1;
+ window.sessionStorage.setItem('openwa_api_key', 'test-key');
+ holdConnect();
+ renderSessions();
+
+ await screen.findByText('gateway unavailable');
+ const listReads = (): number => fetchCalls.filter(c => c.method === 'GET' && c.path === '/api/sessions').length;
+ assert.equal(listReads(), 1);
+
+ // socket.io's manager retried the handshake on its own and it went through: this is the socket's
+ // FIRST connect, so the feed reports no reconnect and nothing else re-reads the list.
+ const socket = lastSocket();
+ assert.ok(socket, 'expected the page to have opened a socket');
+ act(() => socket.receive('connect'));
+
+ await waitFor(() => assert.equal(listReads(), 2));
+ await screen.findByText('new-device');
+ assert.equal(screen.queryByText('gateway unavailable') === null, true, 'the failed read error is still shown');
+});
+
// ── Auto-reject toggle ───────────────────────────────────────────────────────
async function openDetailFor(name: string): Promise<HTMLInputElement> {
diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsx
index 5914f3149..fbca7cb05 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -99,7 +99,12 @@ export function Sessions() {
// proxy, and the credentials with it, that the operator never got to see.
const [proxyLoadFailed, setProxyLoadFailed] = useState(false);
+ // Set while the last list read failed. Cleared as a read starts, so a connect that already triggered
+ // a reload (onReconnect) is not followed by a second read from the recovery effect below.
+ const listReadFailed = useRef(false);
+
const fetchSessions = useCallback(async (): Promise<Session[]> => {
+ listReadFailed.current = false;
try {
// Background refetches — a websocket push, a mutation reloading the list — would otherwise
// replace the whole page with a spinner for the length of a round-trip, so a restriction
@@ -120,6 +125,7 @@ export function Sessions() {
void invalidateSessionQueries(queryClient, queryKeys.sessions);
return data;
} catch (err) {
+ listReadFailed.current = true;
setError(err instanceof Error ? err.message : t('sessions.create.errorDefault'));
return [];
} finally {
@@ -200,7 +206,7 @@ export function Sessions() {
void fetchSessions();
}, [fetchSessions]);
- const { connectionFailed, reconnect } = useSessionFeed({
+ const { isConnected, connectionFailed, reconnect } = useSessionFeed({
sessions,
sessionsRef,
onQRCode: applyQrPush,
@@ -257,6 +263,14 @@ export function Sessions() {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
+ // A connected feed means the gateway answers again, but the feed only reports a RECONNECT: a first
+ // connect that lands after socket.io's own retries (the gateway was restarting at mount) fires no
+ // onReconnect, and no status push re-reads the list, so the failed mount read would stay on screen
+ // with no cards. `error` is a dependency so a read that fails after the connect is retried once too.
+ useEffect(() => {
+ if (isConnected && listReadFailed.current) void fetchSessions();
+ }, [isConnected, error, fetchSessions]);
+
const handleDelete = async (id: string) => {
const session = sessions.find(s => s.id === id);
try {