Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): keep the cached webhook count when a refetch fails

Yudhi Armyndharis517fddcbf662
diff --git a/dashboard/src/pages/Dashboard.test.ts b/dashboard/src/pages/Dashboard.test.ts
index 93677b3c0..390c2df14 100644
--- a/dashboard/src/pages/Dashboard.test.ts
+++ b/dashboard/src/pages/Dashboard.test.ts
@@ -9,6 +9,7 @@ import { MemoryRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
let webhooksStatus = 403;
+let webhookList: unknown[] = [];
function jsonResponse(data: unknown, status = 200): Response {
return new Response(JSON.stringify(data), { status, headers: { 'Content-Type': 'application/json' } });
@@ -21,7 +22,7 @@ function installFetchStub(): void {
if (path === '/api/sessions') return Promise.resolve(jsonResponse([]));
if (path === '/api/webhooks') {
return webhooksStatus === 200
- ? Promise.resolve(jsonResponse([]))
+ ? Promise.resolve(jsonResponse(webhookList))
: Promise.resolve(jsonResponse({ message: 'Insufficient permissions. Required: operator' }, webhooksStatus));
}
// Everything else, the admin-only overview included, is refused.
@@ -53,6 +54,7 @@ afterEach(() => {
rtl.cleanup();
queryClient?.clear();
queryClient = undefined;
+ webhookList = [];
});
function renderDashboard(): void {
@@ -80,6 +82,19 @@ test('a refused webhook read shows the unavailable placeholder, not zero webhook
assert.notEqual(statValue('Webhooks Configured'), '0');
});
+test('a failed background refetch keeps counting the cached webhooks', async () => {
+ webhooksStatus = 200;
+ webhookList = [{ id: 'w1', url: 'https://example.test/hook', events: [] }];
+ renderDashboard();
+ await rtl.screen.findByText('Webhooks Configured');
+ await rtl.waitFor(() => assert.equal(statValue('Webhooks Configured'), '1'));
+
+ webhooksStatus = 502;
+ await rtl.act(() => queryClient!.refetchQueries({ queryKey: ['webhooks'] }));
+ await rtl.waitFor(() => assert.equal(queryClient!.getQueryState(['webhooks'])?.status, 'error'));
+ assert.equal(statValue('Webhooks Configured'), '1');
+});
+
test('a successful empty webhook read still counts zero', async () => {
webhooksStatus = 200;
renderDashboard();
diff --git a/dashboard/src/pages/Dashboard.tsx b/dashboard/src/pages/Dashboard.tsx
index 71d8fb12f..c248e2809 100644
--- a/dashboard/src/pages/Dashboard.tsx
+++ b/dashboard/src/pages/Dashboard.tsx
@@ -24,7 +24,7 @@ export function Dashboard() {
const navigate = useNavigate();
const { data: sessions = [], isLoading: loadingSessions, error: sessionsError } = useSessionsQuery();
const { data: stats } = useSessionStatsQuery();
- const { data: webhooks = [], isError: webhooksFailed } = useWebhooksQuery();
+ const { data: webhooks, isError: webhooksFailed } = useWebhooksQuery();
// /stats/overview is ADMIN-only; for a non-admin key it 403s → overview stays undefined and the
// message cards fall back to '—' without breaking the (un-gated) session cards.
const { data: overview } = useStatsOverviewQuery();
@@ -36,7 +36,8 @@ export function Dashboard() {
const error =
sessionsError instanceof Error ? sessionsError.message : sessionsError ? t('dashboard.loadError') : null;
// GET /webhooks is OPERATOR-only, so a viewer key always fails it: a failed read is not zero webhooks.
- const webhookCount = webhooksFailed ? unavailable : webhooks.length;
+ // A failed background refetch keeps the cached list, which still counts.
+ const webhookCount = webhooksFailed && !webhooks ? unavailable : (webhooks ?? []).length;
const handleDisconnect = async (id: string) => {
try {