Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): reseed a cleared override only for its own session

Yudhi Armyndharis606449f344ce
diff --git a/dashboard/src/pages/Plugins.test.ts b/dashboard/src/pages/Plugins.test.ts
index 411c482ef..d25d71904 100644
--- a/dashboard/src/pages/Plugins.test.ts
+++ b/dashboard/src/pages/Plugins.test.ts
@@ -29,6 +29,7 @@ const PLUGIN = {
};
const SESSION = { id: 'sess-1', name: 'Main', status: 'ready', createdAt: '2026-01-01T00:00:00.000Z' };
+const SESSION_2 = { id: 'sess-2', name: 'Second', status: 'ready', createdAt: '2026-01-01T00:00:00.000Z' };
const REJECTION = 'Cannot tell which entry was removed; reload and try again';
@@ -36,6 +37,8 @@ const REJECTION = 'Cannot tell which entry was removed; reload and try again';
let sessionConfig: Record<string, Record<string, unknown>> = {};
let putReply: { success: boolean; message?: string } = { success: false, message: REJECTION };
let putBodies: unknown[] = [];
+// When set, a PUT waits for it before answering, so a test can act while the request is in flight.
+let putGate: Promise<void> | undefined;
function installFetchStub(): void {
globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
@@ -45,17 +48,20 @@ function installFetchStub(): void {
if (method === 'GET' && path === '/api/plugins') {
return Promise.resolve(jsonResponse([{ ...PLUGIN, sessionConfig }]));
}
- if (method === 'GET' && path === '/api/sessions') return Promise.resolve(jsonResponse([SESSION]));
- if (method === 'PUT' && path === `/api/plugins/${PLUGIN.id}/config/${SESSION.id}`) {
+ if (method === 'GET' && path === '/api/sessions') return Promise.resolve(jsonResponse([SESSION, SESSION_2]));
+ const put = method === 'PUT' ? path.match(new RegExp(`^/api/plugins/${PLUGIN.id}/config/([^/]+)$`)) : null;
+ if (put) {
+ const sid = put[1];
const body = JSON.parse(String(init?.body)) as { config: Record<string, unknown> };
putBodies.push(body);
if (putReply.success) {
// The server drops an empty override, so the session inherits Global again.
sessionConfig = { ...sessionConfig };
- if (Object.keys(body.config).length === 0) delete sessionConfig[SESSION.id];
- else sessionConfig[SESSION.id] = body.config;
+ if (Object.keys(body.config).length === 0) delete sessionConfig[sid];
+ else sessionConfig[sid] = body.config;
}
- return Promise.resolve(jsonResponse(putReply));
+ const reply = putReply;
+ return (putGate ?? Promise.resolve()).then(() => jsonResponse(reply));
}
return Promise.resolve(jsonResponse([]));
}) as typeof fetch;
@@ -92,6 +98,7 @@ afterEach(() => {
sessionConfig = {};
putReply = { success: false, message: REJECTION };
putBodies = [];
+ putGate = undefined;
rtl.cleanup();
queryClient?.clear();
queryClient = undefined;
@@ -143,3 +150,23 @@ test('clearing an override shows the Global values, so the next save does not pi
await waitFor(() => assert.equal(putBodies.length, 2));
assert.deepEqual(putBodies, [{ config: {} }, { config: {} }]);
});
+
+test('a clear that answers after the operator switched sessions leaves the new session on its own values', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ sessionConfig = { [SESSION.id]: { greeting: 'hi' }, [SESSION_2.id]: { greeting: 'hey' } };
+ putReply = { success: true };
+ let releasePut!: () => void;
+ putGate = new Promise(resolve => (releasePut = resolve));
+ await openSessionOverride('Main ●');
+ const field = await screen.findByLabelText<HTMLInputElement>('Greeting');
+ assert.equal(field.value, 'hi');
+
+ fireEvent.click(screen.getByRole('button', { name: 'Clear override' }));
+ await waitFor(() => assert.equal(putBodies.length, 1));
+ fireEvent.change(screen.getByRole('combobox', { name: 'Select a session…' }), { target: { value: SESSION_2.id } });
+ await waitFor(() => assert.equal(screen.getByLabelText<HTMLInputElement>('Greeting').value, 'hey'));
+
+ releasePut();
+ await screen.findByText('Configuration Saved');
+ assert.equal(screen.getByLabelText<HTMLInputElement>('Greeting').value, 'hey');
+});
diff --git a/dashboard/src/pages/Plugins.tsx b/dashboard/src/pages/Plugins.tsx
index 6434cd9e6..0a440a871 100644
--- a/dashboard/src/pages/Plugins.tsx
+++ b/dashboard/src/pages/Plugins.tsx
@@ -409,6 +409,11 @@ function SessionsTab({ plugin }: { plugin: Plugin }) {
const [overrideCfg, setOverrideCfg] = useState<Record<string, unknown>>({});
const [savingOverride, setSavingOverride] = useState(false);
const overrideFormRef = useRef<HTMLFormElement>(null);
+ // The session selected now, read by a request that resolves after the operator may have switched.
+ const selSessionRef = useRef(selSession);
+ useEffect(() => {
+ selSessionRef.current = selSession;
+ }, [selSession]);
// Seed the override form from the resolved slice (the session's override value where set, else base).
// Keyed on selSession + plugin.id (NOT the plugin object): `configPlugin` is derived from the live
@@ -452,15 +457,17 @@ function SessionsTab({ plugin }: { plugin: Plugin }) {
};
const clearOverride = async () => {
- if (!selSession) return;
+ const sid = selSession;
+ if (!sid) return;
setSavingOverride(true);
try {
- const res = await pluginsApi.updateSessionConfig(plugin.id, selSession, {});
+ const res = await pluginsApi.updateSessionConfig(plugin.id, sid, {});
if (!res.success) throw new Error(res.message);
// The seed effect does not re-run on the refetch, so reseed from Global here (the seed with an empty
// override). Left as it was, the form keeps the cleared values and the next save pins them back.
+ // Skipped when the operator has since picked another session: the form now holds that one's values.
const props = plugin.configSchema?.properties;
- if (props) {
+ if (props && selSessionRef.current === sid) {
setOverrideCfg(
Object.fromEntries(
Object.entries(props).map(([key, field]) => [key, plugin.config[key] ?? emptyForField(field)]),