Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): report a failed sessions or groups read as an error
Yudhi Armyndharisfe25e0ea7e02
diff --git a/dashboard/src/components/GroupPicker.tsx b/dashboard/src/components/GroupPicker.tsxindex 7acd3f920..7f8d5dbee 100644--- a/dashboard/src/components/GroupPicker.tsx+++ b/dashboard/src/components/GroupPicker.tsx@@ -14,6 +14,8 @@ interface GroupPickerProps {selectedIds: string[];onChange: (ids: string[]) => void;loading: boolean;+ /** The groups read failed: say so instead of "no groups found" when there is nothing to list. */+ loadFailed?: boolean;limit: number;labelledBy: string;disabled?: boolean;@@ -24,6 +26,7 @@ export function GroupPicker({selectedIds,onChange,loading,+ loadFailed = false,limit,labelledBy,disabled = false,@@ -82,6 +85,10 @@ export function GroupPicker({<Loader2 className="animate-spin" size={16} />{t('messageTester.loadingGroups')}</p>+ ) : noGroups && loadFailed ? (+ <p className="group-picker-empty" role="alert">+ {t('dashboard.loadError')}+ </p>) : noGroups ? (<p className="group-picker-empty">{t('messageTester.noGroupsFound')}</p>) : visibleRows.length === 0 ? (diff --git a/dashboard/src/pages/MessageTester.test.ts b/dashboard/src/pages/MessageTester.test.tsindex 16192e1cf..fa9333812 100644--- a/dashboard/src/pages/MessageTester.test.ts+++ b/dashboard/src/pages/MessageTester.test.ts@@ -537,3 +537,40 @@ test('a media file that cannot be read is reported and not attached', async () =globalThis.FileReader.prototype.readAsDataURL = readAsDataURL;}});++function renderTesterAsWriter(): void {+ window.sessionStorage.setItem('openwa_user_role', 'admin');+ const client = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 1_000 } } });+ rtl.render(+ createElement(QueryClientProvider, { client }, createElement(RoleProvider, null, createElement(MessageTester))),+ );+}++test('a failed sessions read is reported, not shown as "no ready sessions"', async () => {+ globalThis.fetch = ((): Promise<Response> =>+ Promise.resolve(jsonResponse({ message: 'gateway restarting' }, 502))) as typeof fetch;+ renderTesterAsWriter();++ const alert = await rtl.screen.findByRole('alert');+ rtl.within(alert).getByText(/Failed to load data/);+ rtl.within(alert).getByText(/gateway restarting/);+ assert.equal(rtl.screen.queryByRole('option', { name: 'No ready sessions' }), null);+});++test('a failed groups read is reported, not shown as "no groups found"', async () => {+ globalThis.fetch = ((input: RequestInfo | URL): Promise<Response> => {+ const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;+ if (url.endsWith('/sessions')) {+ return Promise.resolve(jsonResponse([{ id: 's1', name: 'Main', status: 'ready', phone: '15550000000' }]));+ }+ if (url.endsWith('/sessions/s1/groups')) return Promise.resolve(jsonResponse({ message: 'engine busy' }, 500));+ return Promise.resolve(jsonResponse([]));+ }) as typeof fetch;+ renderTesterAsWriter();+ await rtl.screen.findByRole('option', { name: /Main/ });+ rtl.fireEvent.click(rtl.screen.getByRole('button', { name: 'Group' }));++ const alert = await rtl.screen.findByRole('alert');+ rtl.within(alert).getByText(/Failed to load data/);+ assert.equal(rtl.screen.queryByText('No groups found'), null);+});diff --git a/dashboard/src/pages/MessageTester.tsx b/dashboard/src/pages/MessageTester.tsxindex bdcc0a08c..4b804d144 100644--- a/dashboard/src/pages/MessageTester.tsx+++ b/dashboard/src/pages/MessageTester.tsx@@ -1,6 +1,6 @@import { useState, useEffect, useRef, type ChangeEvent } from 'react';import { useTranslation } from 'react-i18next';-import { Send, CheckCircle, XCircle, Loader2, Upload, X, Plus } from 'lucide-react';+import { Send, CheckCircle, XCircle, Loader2, Upload, X, Plus, AlertCircle } from 'lucide-react';import {messageApi,contactApi,@@ -117,7 +117,9 @@ export function MessageTester() {const { t } = useTranslation();useDocumentTitle(t('messageTester.title'));const { canWrite } = useRole();- const { data: allSessions = [], isLoading: loadingSessions } = useSessionsQuery();+ const { data: allSessions = [], isLoading: loadingSessions, error: sessionsError } = useSessionsQuery();+ // A read that never produced a list is not "no ready sessions"; a failed refetch keeps the cached one.+ const sessionsFailed = !!sessionsError && allSessions.length === 0;const sessions = allSessions.filter(s => s.status === 'ready');const [session, setSession] = useState('');const [recipient, setRecipient] = useState('');@@ -176,7 +178,11 @@ export function MessageTester() {// poll/cancel must keep addressing the session the batch was created on.const batchSessionRef = useRef('');- const { data: groups = [], isLoading: loadingGroups } = useSessionGroupsQuery(session, recipientType === 'group');+ const {+ data: groups = [],+ isLoading: loadingGroups,+ isError: groupsFailed,+ } = useSessionGroupsQuery(session, recipientType === 'group');// Also re-picks when the chosen session leaves the ready list on a refetch: the select would show// the first option while every send still went to the dropped one.@@ -639,6 +645,15 @@ export function MessageTester() {<div className="message-tester"><PageHeader title={t('messageTester.title')} subtitle={t('messageTester.subtitle')} />+ {sessionsFailed && (+ <div className="error-banner" role="alert">+ <AlertCircle size={20} />+ <span className="error-banner-text">+ {t('dashboard.loadError')}: {sessionsError.message}+ </span>+ </div>+ )}+<div className="tester-panels"><div className="compose-panel"><h2 className="eyebrow">{t('messageTester.compose')}</h2>@@ -646,7 +661,9 @@ export function MessageTester() {<div className="form-group"><label htmlFor="mt-1">{t('messageTester.session')}</label><select id="mt-1" value={session} onChange={e => setSession(e.target.value)} disabled={isGroupSending}>- {sessions.length === 0 && <option value="">{t('messageTester.noReadySessions')}</option>}+ {sessions.length === 0 && (+ <option value="">{t(sessionsFailed ? 'dashboard.loadError' : 'messageTester.noReadySessions')}</option>+ )}{sessions.map(s => (<option key={s.id} value={s.id}>{s.name} ({s.phone || t('messageTester.sessionOptionPhoneNone')})@@ -697,6 +714,7 @@ export function MessageTester() {selectedIds={selectedGroups}onChange={setSelectedGroups}loading={loadingGroups}+ loadFailed={groupsFailed}limit={BULK_MAX_RECIPIENTS}labelledBy="group-picker-label"disabled={isGroupSending}diff --git a/dashboard/src/pages/Templates.test.ts b/dashboard/src/pages/Templates.test.tsindex c0b78da67..c4f9b2d18 100644--- a/dashboard/src/pages/Templates.test.ts+++ b/dashboard/src/pages/Templates.test.ts@@ -8,6 +8,7 @@ import { createElement } from 'react';import { QueryClient, QueryClientProvider } from '@tanstack/react-query';let templatesStatus = 200;+let sessionsStatus = 200;let templates: Array<{ id: string; name: string; body: string }> = [];const deleted: string[] = [];@@ -20,6 +21,7 @@ function installFetchStub(): void {const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;const path = url.replace(/^https?:\/\/[^/]+/, '');if (path === '/api/sessions') {+ if (sessionsStatus !== 200) return Promise.resolve(jsonResponse({ message: 'gateway restarting' }, 502));return Promise.resolve(jsonResponse([{@@ -73,6 +75,7 @@ afterEach(() => {rtl.cleanup();queryClient?.clear();queryClient = undefined;+ sessionsStatus = 200;});function renderTemplates(): void {@@ -139,3 +142,13 @@ test('a successful empty read still shows the empty state', async () => {renderTemplates();await rtl.screen.findByText('No templates saved');});++test('a failed sessions read shows the error, not "no sessions available"', async () => {+ sessionsStatus = 502;+ renderTemplates();+ const alert = await rtl.screen.findByRole('alert');+ rtl.within(alert).getByText('Failed to load data');+ rtl.within(alert).getByText('gateway restarting');+ assert.equal(rtl.screen.queryByText('No sessions available'), null);+ assert.equal(rtl.screen.queryByRole('option', { name: 'No sessions' }), null);+});diff --git a/dashboard/src/pages/Templates.tsx b/dashboard/src/pages/Templates.tsxindex 851174972..7b9906632 100644--- a/dashboard/src/pages/Templates.tsx+++ b/dashboard/src/pages/Templates.tsx@@ -56,7 +56,10 @@ export function Templates() {const { t } = useTranslation();useDocumentTitle(t('templates.title'));const { canWrite } = useRole();- const { data: sessions = [], isLoading: loadingSessions } = useSessionsQuery();+ const { data: sessions = [], isLoading: loadingSessions, error: sessionsError } = useSessionsQuery();+ // A failed read is not "no sessions": the gateway may simply be restarting. A failed background refetch keeps+ // the cached list, so only a read that never produced one counts.+ const sessionsFailed = !!sessionsError && sessions.length === 0;const [selectedSessionId, setSelectedSessionId] = useState('');const [form, setForm] = useState<TemplateForm>(emptyForm);const [editingTemplate, setEditingTemplate] = useState<MessageTemplate | null>(null);@@ -194,7 +197,9 @@ export function Templates() {resetForm();}}>- {sessions.length === 0 && <option value="">{t('templates.noSessions')}</option>}+ {sessions.length === 0 && (+ <option value="">{t(sessionsFailed ? 'dashboard.loadError' : 'templates.noSessions')}</option>+ )}{sessions.map(session => (<option key={session.id} value={session.id}>{session.name}@@ -204,7 +209,13 @@ export function Templates() {}/>- {sessions.length === 0 ? (+ {sessionsFailed ? (+ <div className="templates-empty-page" role="alert">+ <AlertCircle size={48} strokeWidth={1} />+ <h3>{t('dashboard.loadError')}</h3>+ <p>{sessionsError.message}</p>+ </div>+ ) : sessions.length === 0 ? (<div className="templates-empty-page"><FileText size={48} strokeWidth={1} /><h3>{t('templates.empty.noSessionsTitle')}</h3>