Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): do not call a failed session read empty
Yudhi Armyndharisd619be8d07d3
diff --git a/dashboard/src/pages/Chats.css b/dashboard/src/pages/Chats.cssindex 0d382e5d2..6d2bf1111 100644--- a/dashboard/src/pages/Chats.css+++ b/dashboard/src/pages/Chats.css@@ -897,6 +897,10 @@color: var(--text-muted);}+.chats-page .chats-error-state button {+ margin-top: 1.5rem;+}+.chats-page .text-warn {color: var(--warning-text);}diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.tsindex f01433bcf..b45151af7 100644--- a/dashboard/src/pages/Chats.test.ts+++ b/dashboard/src/pages/Chats.test.ts@@ -43,6 +43,9 @@ let twoSessions = false;const SESSION_3: Session = { ...SESSION, id: 'session-3', name: 'Third', phone: '15550004444' };let thirdSessionStatus: Session['status'] | null = null;+// When set, the session list answers this status with a gateway-style error body until a test clears it.+let sessionListStatus: number | null = null;+// Hits the global search answers with.let searchHits: SearchHit[] = [];@@ -285,6 +288,7 @@ function installFetchStub(): void {fetchCalls.push({ method, path, body });if (method === 'GET' && path === '/api/sessions') {+ if (sessionListStatus) return Promise.resolve(jsonResponse({ message: 'Bad Gateway' }, sessionListStatus));const listed = twoSessions ? [SESSION, SESSION_2] : [SESSION];if (thirdSessionStatus) listed.push({ ...SESSION_3, status: thirdSessionStatus });return Promise.resolve(jsonResponse(listed));@@ -439,6 +443,7 @@ afterEach(async () => {olderPageFails = false;chatsResponder = null;thirdSessionStatus = null;+ sessionListStatus = null;searchHits = [];engineType = 'baileys';channels = [CHANNEL];@@ -3630,3 +3635,24 @@ test("the page joins the open session's message events once connected, and movestwoSessions = false;}});++// A failed read of the sessions is not "no connected sessions", which tells the operator to go connect one.+test('a failed session list says so and offers the read again, not the no-sessions guidance', async () => {+ const { screen, fireEvent, within } = rtl;+ sessionListStatus = 502;+ renderChats();++ const alert = await screen.findByRole('alert');+ assert.match(alert.textContent ?? '', /Failed to load sessions/);+ assert.match(alert.textContent ?? '', /Bad Gateway/);+ assert.equal(+ screen.queryByText('No connected sessions') === null,+ true,+ 'a failed read claimed there are no sessions',+ );++ sessionListStatus = null;+ fireEvent.click(within(alert).getByRole('button', { name: 'Retry' }));+ await screen.findByText('Alice');+ assert.equal(screen.queryByRole('alert') === null, true);+});diff --git a/dashboard/src/pages/Chats.tsx b/dashboard/src/pages/Chats.tsxindex 960d9a4cf..e031ab106 100644--- a/dashboard/src/pages/Chats.tsx+++ b/dashboard/src/pages/Chats.tsx@@ -372,25 +372,28 @@ export function Chats() {current.showErrorToast(current.t(key), err instanceof Error ? err.message : undefined);}, []);- // 1. Fetch available connected sessions on mount- useEffect(() => {- const loadSessions = async () => {- try {- setLoadingSessions(true);- const list = await sessionApi.list();- const readySessions = list.filter(s => s.status === 'ready');- setSessions(readySessions);- if (readySessions.length > 0) {- setSelectedSessionId(readySessions[0].id);- }- } catch (err) {- showLoadError('chats.errors.loadSessions', err);- } finally {- setLoadingSessions(false);+ // 1. Fetch available connected sessions on mount. A failed read is not "no connected sessions": the gateway may+ // simply be restarting, so the page says it could not read them and offers the read again.+ const [sessionsFailure, setSessionsFailure] = useState<string | null>(null);+ const loadSessions = useCallback(async () => {+ try {+ setLoadingSessions(true);+ setSessionsFailure(null);+ const list = await sessionApi.list();+ const readySessions = list.filter(s => s.status === 'ready');+ setSessions(readySessions);+ if (readySessions.length > 0) {+ setSelectedSessionId(readySessions[0].id);}- };+ } catch (err) {+ setSessionsFailure(err instanceof Error ? err.message : '');+ } finally {+ setLoadingSessions(false);+ }+ }, []);+ useEffect(() => {void loadSessions();- }, [showLoadError]);+ }, [loadSessions]);// 2. Fetch chats when active session changes. A session switch does not cancel the list still// loading for the session left behind, so an answer for any session but the latest call's is@@ -1675,6 +1678,15 @@ export function Chats() {<Loader2 className="animate-spin" size={32} /><p>{t('common.loading')}</p></div>+ ) : sessionsFailure !== null ? (+ <div className="chats-error-state" role="alert">+ <AlertCircle size={48} className="text-warn" />+ <h3>{t('chats.errors.loadSessions')}</h3>+ {sessionsFailure && <p>{sessionsFailure}</p>}+ <button className="btn-secondary" onClick={() => void loadSessions()}>+ {t('common.retry')}+ </button>+ </div>) : sessions.length === 0 ? (<div className="chats-error-state"><AlertCircle size={48} className="text-warn" />