Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep the cached overview when a sessions refetch fails

Yudhi Armyndharis9925dbaeff18
diff --git a/dashboard/src/pages/Dashboard.test.ts b/dashboard/src/pages/Dashboard.test.ts
index b95e81c78..eb14f0273 100644
--- a/dashboard/src/pages/Dashboard.test.ts
+++ b/dashboard/src/pages/Dashboard.test.ts
@@ -13,6 +13,7 @@ let webhooksStatus = 403;
let webhookList: unknown[] = [];
let sessionList: unknown[] = [];
let stopStatus = 200;
+let sessionsStatus = 200;
function jsonResponse(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });
@@ -31,7 +32,11 @@ function installFetchStub(): void {
globalThis.fetch = ((input: RequestInfo | URL): Promise<Response> => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
const path = url.replace(/^https?:\/\/[^/]+/, '');
- if (path === '/api/sessions') return Promise.resolve(jsonResponse(sessionList));
+ if (path === '/api/sessions') {
+ return Promise.resolve(
+ sessionsStatus === 200 ? jsonResponse(sessionList) : jsonResponse({ message: 'Bad Gateway' }, sessionsStatus),
+ );
+ }
if (path === `/api/sessions/${READY_SESSION.id}/stop`) {
return Promise.resolve(
stopStatus === 200
@@ -81,6 +86,7 @@ afterEach(() => {
webhookList = [];
sessionList = [];
stopStatus = 200;
+ sessionsStatus = 200;
window.localStorage.setItem('openwa_user_role', 'viewer');
});
@@ -159,3 +165,22 @@ test('a failed stop is reported, not swallowed', async () => {
assert.ok(!rtl.screen.queryByRole('button', { name: 'Disconnect' }), 'the session list was not re-read'),
);
});
+
+test('a failed background refetch of the sessions keeps the cached page', async () => {
+ webhooksStatus = 200;
+ sessionList = [READY_SESSION];
+ renderDashboard();
+ await rtl.screen.findByText('Main');
+
+ sessionsStatus = 502;
+ await rtl.act(() => queryClient!.refetchQueries({ queryKey: ['sessions'] }));
+ await rtl.waitFor(() => assert.equal(queryClient!.getQueryState(['sessions'])?.status, 'error'));
+ assert.ok(rtl.screen.queryByText('Main'), 'a failed refetch replaced the cached sessions with an error');
+});
+
+test('a failed first read of the sessions still shows the error', async () => {
+ webhooksStatus = 200;
+ sessionsStatus = 502;
+ renderDashboard();
+ await rtl.screen.findByText(/Bad Gateway/);
+});
diff --git a/dashboard/src/pages/Dashboard.tsx b/dashboard/src/pages/Dashboard.tsx
index 283c1ceb2..15cbf8219 100644
--- a/dashboard/src/pages/Dashboard.tsx
+++ b/dashboard/src/pages/Dashboard.tsx
@@ -26,7 +26,12 @@ export function Dashboard() {
const navigate = useNavigate();
const { canWrite } = useRole();
const toast = useToast();
- const { data: sessions = [], isLoading: loadingSessions, error: sessionsError } = useSessionsQuery();
+ const {
+ data: sessions = [],
+ isLoading: loadingSessions,
+ error: sessionsError,
+ isLoadingError: sessionsNeverLoaded,
+ } = useSessionsQuery();
const { data: stats } = useSessionStatsQuery();
const { data: webhooks, isError: webhooksFailed } = useWebhooksQuery();
// /stats/overview is ADMIN-only; for a non-admin key it 403s → overview stays undefined and the
@@ -37,8 +42,13 @@ export function Dashboard() {
const messagesToday = overview ? overview.messages.today.sent + overview.messages.today.received : unavailable;
const totalMessages = overview ? overview.messages.sent + overview.messages.received : unavailable;
const loading = loadingSessions;
- const error =
- sessionsError instanceof Error ? sessionsError.message : sessionsError ? t('dashboard.loadError') : null;
+ // Only a read that never succeeded replaces the page: a failed background refetch keeps its cached
+ // data (as the webhook card below does), so the last good view stays on screen.
+ const error = sessionsNeverLoaded
+ ? sessionsError instanceof Error
+ ? sessionsError.message
+ : t('dashboard.loadError')
+ : null;
// GET /webhooks is OPERATOR-only, so a viewer key always fails it: a failed read is not zero webhooks.
// A failed background refetch keeps the cached list, which still counts.
const webhookCount = webhooksFailed && !webhooks ? unavailable : (webhooks ?? []).length;