Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): refetch the saved config after an infrastructure save

Yudhi Armyndharisa2391f2fc717
diff --git a/dashboard/src/pages/Infrastructure.test.ts b/dashboard/src/pages/Infrastructure.test.ts
index fe504a28b..ade142d62 100644
--- a/dashboard/src/pages/Infrastructure.test.ts
+++ b/dashboard/src/pages/Infrastructure.test.ts
@@ -590,6 +590,49 @@ test('the pending-restart note survives a successful save', async () => {
assert.ok(screen.queryByText(PENDING_RESTART_NOTE), 'the pending-restart note must not vanish once a save succeeds');
});
+test('a change saved and left for a later restart shows the pending-restart note', async () => {
+ const { screen, waitFor, fireEvent, within } = rtl;
+ resetFetchCalls();
+ // Running and saved agree, so no note yet: the drift comes only from this save.
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitConfigHydrated(container);
+ assert.ok(!screen.queryByText(PENDING_RESTART_NOTE), 'no note before anything is saved');
+
+ fireEvent.click(container.querySelector<HTMLInputElement>('input[name="dbType"]')!);
+ // From here on the gateway reports what the save just wrote.
+ overrides = { saved: { ...SAVED_CONFIG, database: { ...SAVED_CONFIG.database, type: 'sqlite' } } };
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+ const dialog = await screen.findByRole('dialog');
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Restart Later' }));
+
+ await waitFor(() => assert.ok(screen.queryByText(PENDING_RESTART_NOTE), 'expected the pending-restart note'));
+});
+
+test('a second save before the restart still warns of the database switch the first one saved', async () => {
+ const { screen, waitFor, fireEvent, within } = rtl;
+ resetFetchCalls();
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitConfigHydrated(container);
+ fireEvent.change(fieldInput(container, 'Host'), { target: { value: 'new-db-host' } });
+ overrides = { saved: { ...SAVED_CONFIG, database: { ...SAVED_CONFIG.database, host: 'new-db-host' } } };
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+ let dialog = await screen.findByRole('dialog');
+ within(dialog).getByText(DB_SWITCH_WARNING, { exact: false });
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Restart Later' }));
+ // The refetched saved config now names the new host; the running database is still the old one.
+ await waitFor(() =>
+ assert.equal(fetchCalls.filter(c => c.method === 'GET' && c.path === '/api/infra/config').length, 2),
+ );
+
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+ dialog = await screen.findByRole('dialog');
+ within(dialog).getByText(DB_SWITCH_WARNING, { exact: false });
+});
+
/** The pin note rendered inside a text field's form group, or null. */
function fieldPinNote(container: HTMLElement, labelText: string): string | null {
return fieldInput(container, labelText).closest('.form-group')?.querySelector('.env-pin-note')?.textContent ?? null;
diff --git a/dashboard/src/pages/Infrastructure.tsx b/dashboard/src/pages/Infrastructure.tsx
index 6c41a0d4d..393538dd0 100644
--- a/dashboard/src/pages/Infrastructure.tsx
+++ b/dashboard/src/pages/Infrastructure.tsx
@@ -1,4 +1,5 @@
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useRef } from 'react';
+import { useQueryClient } from '@tanstack/react-query';
import { Trans, useTranslation } from 'react-i18next';
import {
Database,
@@ -13,7 +14,7 @@ import {
Download,
Upload,
} from 'lucide-react';
-import { API_BASE_URL } from '../services/api';
+import { API_BASE_URL, type SavedConfig } from '../services/api';
import { copyToClipboard } from '../utils/clipboard';
import { useDocumentTitle } from '../hooks/useDocumentTitle';
import { useInfraStatusQuery, useInfraConfigQuery, useEnginesQuery, useCurrentEngineQuery } from '../hooks/queries';
@@ -48,6 +49,13 @@ export function Infrastructure() {
const currentEngine = currentEngineData?.engineType ?? '';
const configForm = useInfraConfigForm(infraStatus, savedConfig);
+ const queryClient = useQueryClient();
+ // The saved config as the page first read it. A save refetches the saved config (so the pending-restart
+ // notes see it), but until a restart the database still running is the one read here.
+ const loadedConfig = useRef<SavedConfig | undefined>(undefined);
+ useEffect(() => {
+ if (savedConfig && !loadedConfig.current) loadedConfig.current = savedConfig;
+ }, [savedConfig]);
const restartFlow = useRestartFlow();
const dataBackup = useDataBackup();
@@ -61,15 +69,16 @@ export function Infrastructure() {
// empty-database / orphaned-media data move before it happens. A switch is: changing type;
// flipping built-in↔external (different physical backend); OR retargeting an external Postgres
// to a different host/port/database (also a different, empty DB). Host/port/db aren't all in
- // /status, so compare the edited form against the still-cached saved config. A key with nothing
- // saved reads '', and runs on the same fallback the form was seeded with.
+ // /status, so compare the edited form against the saved config as the page loaded it. A key with
+ // nothing saved reads '', and runs on the same fallback the form was seeded with.
+ const loaded = loadedConfig.current;
const dbExternalRetarget =
configForm.dbConfig.type === 'postgres' &&
!configForm.dbConfig.builtIn &&
- !!savedConfig &&
- (configForm.dbConfig.host !== (savedConfig.database.host || infraStatus?.database.host || 'localhost') ||
- configForm.dbConfig.port !== (savedConfig.database.port || '5432') ||
- configForm.dbConfig.database !== (savedConfig.database.database || 'openwa'));
+ !!loaded &&
+ (configForm.dbConfig.host !== (loaded.database.host || infraStatus?.database.host || 'localhost') ||
+ configForm.dbConfig.port !== (loaded.database.port || '5432') ||
+ configForm.dbConfig.database !== (loaded.database.database || 'openwa'));
const dbSwitch =
!!infraStatus &&
(configForm.dbConfig.type !== infraStatus.database.type ||
@@ -90,6 +99,8 @@ export function Infrastructure() {
infraStatus?.storage.type === 's3' && infraStatus.storage.builtIn && 'minio',
].filter((p): p is string => typeof p === 'string');
restartFlow.open({ profiles, running, dbSwitch, storageSwitch });
+ // The form is seeded once, so the refetch keeps any edit in progress.
+ void queryClient.invalidateQueries({ queryKey: ['infra', 'config'] });
},
});