Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): reselect a templates session deleted elsewhere

Yudhi Armyndharis75c5d2036e6b
diff --git a/dashboard/src/pages/Templates.test.ts b/dashboard/src/pages/Templates.test.ts
index 129b92c96..beed87b01 100644
--- a/dashboard/src/pages/Templates.test.ts
+++ b/dashboard/src/pages/Templates.test.ts
@@ -12,6 +12,9 @@ let templatesStatus = 200;
let sessionsStatus = 200;
// When set, GET /api/sessions answers only once this settles, holding the page on its first load.
let sessionsGate: Promise<void> | null = null;
+// Sessions listed after the default one, which is dropped while `firstSessionGone` is set.
+let extraSessions: Array<{ id: string; name: string }> = [];
+let firstSessionGone = false;
let templates: Array<{ id: string; name: string; body: string }> = [];
const deleted: string[] = [];
@@ -26,18 +29,23 @@ function installFetchStub(): void {
if (path === '/api/sessions') {
if (sessionsGate) return sessionsGate.then(() => jsonResponse([]));
if (sessionsStatus !== 200) return Promise.resolve(jsonResponse({ message: 'gateway restarting' }, 502));
+ const rows = [{ id: 'sess-1', name: 'billing-bot' }, ...extraSessions].filter(
+ row => !firstSessionGone || row.id !== 'sess-1',
+ );
return Promise.resolve(
- jsonResponse([
- {
- id: 'sess-1',
- name: 'billing-bot',
+ jsonResponse(
+ rows.map(row => ({
+ ...row,
status: 'ready',
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
- },
- ]),
+ })),
+ ),
);
}
+ if (path === '/api/sessions/sess-2/templates') {
+ return Promise.resolve(jsonResponse([{ id: 'tpl-2', name: 'support-greeting', body: 'Hello' }]));
+ }
if (path === '/api/sessions/sess-1/templates') {
if (templatesStatus === 403) {
return Promise.resolve(jsonResponse({ message: 'Insufficient permissions. Required: operator' }, 403));
@@ -80,6 +88,8 @@ afterEach(() => {
queryClient?.clear();
queryClient = undefined;
sessionsStatus = 200;
+ extraSessions = [];
+ firstSessionGone = false;
});
function renderTemplates(): void {
@@ -179,3 +189,27 @@ test('the first-load spinner is centered in a 400px block', async () => {
style.remove();
}
});
+
+// A session deleted elsewhere drops out of the list on the next read. Kept selected, it matches no
+// option, so the select shows another session while the list and every write still target the gone one.
+test('a selected session that disappears from the list is replaced by the first remaining one', async () => {
+ const { screen, act } = rtl;
+ templatesStatus = 200;
+ templates = [{ id: 'tpl-1', name: 'invoice-reminder', body: 'Hi {{name}}' }];
+ extraSessions = [{ id: 'sess-2', name: 'support-bot' }];
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
+ renderTemplates();
+
+ await screen.findByText('invoice-reminder');
+ const select = screen.getByLabelText<HTMLSelectElement>('Session');
+ assert.equal(select.value, 'sess-1');
+
+ firstSessionGone = true;
+ await act(() => queryClient!.refetchQueries({ queryKey: ['sessions'], exact: true }));
+
+ await screen.findByText('support-greeting');
+ assert.equal(select.value, 'sess-2');
+ assert.equal(screen.queryByText('invoice-reminder'), null);
+ screen.getByText('Saved under support-bot');
+ window.sessionStorage.setItem('openwa_user_role', 'viewer');
+});
diff --git a/dashboard/src/pages/Templates.tsx b/dashboard/src/pages/Templates.tsx
index 7b9906632..34546f38a 100644
--- a/dashboard/src/pages/Templates.tsx
+++ b/dashboard/src/pages/Templates.tsx
@@ -91,10 +91,17 @@ export function Templates() {
}, [searchTerm, templates]);
const isSaving = createMutation.isPending || updateMutation.isPending;
+ // Select the first session, and again once the selected one is gone (deleted elsewhere): a stale id
+ // matches no option, so the select would show another session while every read and write still
+ // went to the deleted one.
useEffect(() => {
- if (!selectedSessionId && sessions.length > 0) {
- setSelectedSessionId(sessions[0].id);
- }
+ if (sessions.some(session => session.id === selectedSessionId)) return;
+ const next = sessions[0]?.id ?? '';
+ if (next === selectedSessionId) return;
+ setSelectedSessionId(next);
+ setForm(emptyForm);
+ setEditingTemplate(null);
+ setPreviewValues({});
}, [selectedSessionId, sessions]);
useEffect(() => {