Skip to content

Commit a497f7b

Browse files
fix(web): confirm before removing a provider instance's saved settings (pingdotgg#29480)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 84537c4 commit a497f7b

2 files changed

Lines changed: 130 additions & 4 deletions

File tree

‎apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const state = vi.hoisted(() => ({
3636
updateModelPreferences: vi.fn(),
3737
toggleFavorite: vi.fn(),
3838
mutateProviderInstance: vi.fn(),
39+
confirm: vi.fn<() => Promise<boolean>>(),
3940
}));
4041

4142
vi.mock(import("../../state/server"), async (original) => {
@@ -80,6 +81,14 @@ vi.mock(import("../../state/use-atom-command"), async (original) => ({
8081
"accept-url-auth": state.acceptUrlAuth,
8182
})[command.label ?? ""] ?? vi.fn()) as never,
8283
}));
84+
vi.mock(import("~/localApi"), async (original) => {
85+
const api = { dialogs: { confirm: state.confirm } };
86+
return {
87+
...(await original()),
88+
readLocalApi: (() => api) as never,
89+
ensureLocalApi: (() => api) as never,
90+
};
91+
});
8392
vi.mock(import("../../state/query"), async (original) => ({
8493
...(await original()),
8594
useEnvironmentQuery: ((query: unknown) => ({
@@ -231,6 +240,7 @@ async function setup() {
231240
state.updateModelPreferences.mockReset();
232241
state.toggleFavorite.mockReset();
233242
state.mutateProviderInstance.mockReset().mockResolvedValue(AsyncResult.success({}));
243+
state.confirm.mockReset().mockResolvedValue(false);
234244
state.refresh.mockReset().mockResolvedValue(AsyncResult.success(undefined));
235245
state.updateProvider.mockReset().mockResolvedValue(AsyncResult.success(undefined));
236246
state.uninstall.mockReset().mockResolvedValue(AsyncResult.success({}));
@@ -359,6 +369,90 @@ describe("EnvironmentProviderSettings routing", () => {
359369
});
360370
});
361371

372+
describe("EnvironmentProviderSettings instance removal", () => {
373+
it("resets a configured default instance only once the user confirms", async () => {
374+
await setup();
375+
state.settings = {
376+
...DEFAULT_UNIFIED_SETTINGS,
377+
providerInstances: {
378+
[codexId]: {
379+
driver: ProviderDriverKind.make("codex"),
380+
enabled: true,
381+
config: { binaryPath: "/opt/codex/bin/codex" },
382+
},
383+
},
384+
};
385+
await setProviders([provider()]);
386+
await using _screen = await render(panel({ targetInstanceId: codexId }));
387+
const reset = page.getByRole("button", { name: "Reset Codex provider settings to default" });
388+
await userEvent.click(reset);
389+
390+
expect(state.mutateProviderInstance).not.toHaveBeenCalled();
391+
expect(state.confirm).toHaveBeenCalledOnce();
392+
393+
state.confirm.mockResolvedValue(true);
394+
await userEvent.click(reset);
395+
await vi.waitFor(() =>
396+
expect(state.mutateProviderInstance).toHaveBeenCalledWith({
397+
operation: "remove",
398+
instanceId: codexId,
399+
}),
400+
);
401+
});
402+
403+
it("warns that resetting an enabled default-off provider turns it off", async () => {
404+
await setup();
405+
const antigravityId = ProviderInstanceId.make("antigravity");
406+
const antigravity = ProviderDriverKind.make("antigravity");
407+
state.settings = {
408+
...DEFAULT_UNIFIED_SETTINGS,
409+
providerInstances: {
410+
[antigravityId]: {
411+
driver: antigravity,
412+
enabled: true,
413+
config: { binaryPath: "/opt/antigravity/bin/agy" },
414+
},
415+
},
416+
};
417+
await setProviders([{ ...provider(), instanceId: antigravityId, driver: antigravity }]);
418+
await using _screen = await render(panel({ targetInstanceId: antigravityId }));
419+
await userEvent.click(page.getByRole("button", { name: /^Reset .* provider settings/ }));
420+
421+
expect(state.confirm).toHaveBeenCalledWith(
422+
expect.stringContaining("It will also be turned off."),
423+
{
424+
variant: "destructive",
425+
},
426+
);
427+
expect(state.mutateProviderInstance).not.toHaveBeenCalled();
428+
});
429+
430+
it("deletes a custom instance only once the user confirms", async () => {
431+
await setup();
432+
state.settings = {
433+
...DEFAULT_UNIFIED_SETTINGS,
434+
providerInstances: {
435+
[customId]: { driver: ProviderDriverKind.make("codex"), enabled: true },
436+
},
437+
};
438+
await using _screen = await render(panel({ targetInstanceId: customId }));
439+
const remove = page.getByRole("button", { name: `Delete instance ${customId}` });
440+
await userEvent.click(remove);
441+
442+
expect(state.mutateProviderInstance).not.toHaveBeenCalled();
443+
expect(state.confirm).toHaveBeenCalledOnce();
444+
445+
state.confirm.mockResolvedValue(true);
446+
await userEvent.click(remove);
447+
await vi.waitFor(() =>
448+
expect(state.mutateProviderInstance).toHaveBeenCalledWith({
449+
operation: "remove",
450+
instanceId: customId,
451+
}),
452+
);
453+
});
454+
});
455+
362456
describe("EnvironmentProviderSettings health check interval", () => {
363457
it("commits interval drafts and allows retrying rejected edits", async () => {
364458
await setup();

‎apps/web/src/components/settings/ProviderSettingsPanel.tsx‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { connectionStatusTitle } from "@t3tools/client-runtime/connection";
1111
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
1212
import {
1313
isAtomCommandInterrupted,
14+
settlePromise,
1415
squashAtomCommandFailure,
1516
} from "@t3tools/client-runtime/state/runtime";
1617
import {
@@ -47,6 +48,7 @@ import {
4748
useToggleEnvironmentModelFavorite,
4849
} from "../../hooks/useSettings";
4950
import { useStableCallback } from "../../hooks/useStableCallback";
51+
import { ensureLocalApi } from "../../localApi";
5052
import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
5153
import { cn } from "../../lib/utils";
5254
import { resolveAppModelSelectionState } from "../../modelSelection";
@@ -127,6 +129,25 @@ import {
127129
resolveSelectedProviderEnvironmentId,
128130
} from "./ProviderSettingsPanel.logic";
129131

132+
function providerInstanceName(instance: ProviderInstanceConfig): string {
133+
return (
134+
instance.displayName?.trim() ||
135+
providerClients.get(instance.driver)?.label ||
136+
String(instance.driver)
137+
);
138+
}
139+
140+
async function confirmProviderInstanceRemoval(
141+
question: string,
142+
consequence = "",
143+
): Promise<boolean> {
144+
const message = `${question}\nIts saved settings, including any custom binary path, will be removed.${consequence}`;
145+
const confirmed = await settlePromise(() =>
146+
ensureLocalApi().dialogs.confirm(message, { variant: "destructive" }),
147+
);
148+
return confirmed._tag === "Success" && confirmed.value;
149+
}
150+
130151
function modelListChanges(current: ReadonlyArray<string>, next: ReadonlyArray<string>) {
131152
const currentSet = new Set(current);
132153
const nextSet = new Set(next.map((model) => model.trim()).filter(Boolean));
@@ -830,6 +851,10 @@ export function EnvironmentProviderSettings({
830851

831852
const deleteInstance = useStableCallback(
832853
async (instanceId: ProviderInstanceId, instance: ProviderInstanceConfig) => {
854+
const confirmed = await confirmProviderInstanceRemoval(
855+
`Remove the ${providerInstanceName(instance)} provider instance?`,
856+
);
857+
if (!confirmed) return;
833858
const updateResult = await persistProviderInstance({
834859
operation: "remove",
835860
instanceId,
@@ -866,10 +891,17 @@ export function EnvironmentProviderSettings({
866891
);
867892

868893
const resetDefaultInstance = useStableCallback(async (driverKind: ProviderDriverKind) => {
869-
const result = await persistProviderInstance({
870-
operation: "remove",
871-
instanceId: defaultInstanceIdForDriver(driverKind),
872-
});
894+
const instanceId = defaultInstanceIdForDriver(driverKind);
895+
const instance = settings.providerInstances?.[instanceId] ?? { driver: driverKind };
896+
const turnsOff =
897+
resolveProviderInstanceEnabled(instance) &&
898+
!resolveProviderInstanceEnabled({ driver: driverKind });
899+
const confirmed = await confirmProviderInstanceRemoval(
900+
`Reset the ${providerInstanceName(instance)} provider to its defaults?`,
901+
turnsOff ? " It will also be turned off." : "",
902+
);
903+
if (!confirmed) return;
904+
const result = await persistProviderInstance({ operation: "remove", instanceId });
873905
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
874906
const error = squashAtomCommandFailure(result);
875907
toastManager.add({

0 commit comments

Comments
 (0)