fix(webui): save settings when editing completes (NAN-323) (#6384)

* fix(webui): save settings when editing completes (NAN-323)

* fix(webui): close settings completion and verification gaps

* refactor(webui): simplify settings edit ownership
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-22 13:39:46 +08:00
1 parent 26e1f41a00
commit c6babb20e8
34 files changed
+1363 -388

No files matched your search

@@ -107,7 +107,7 @@ describe("Linear channel UI", () => {
mockFeature(linearFeature);
renderSettingsView({ initialSection: "channels" });
fireEvent.click(await screen.findByRole("button", { name: "View Linear settings" }));
const createApp = await screen.findByRole("button", { name: "Create Linear app" });
const createApp = await screen.findByRole("button", { name: "Create Linear app" }, { timeout: 5000 });
expect(createApp).toBeEnabled();
fireEvent.click(createApp);
const prompt = await screen.findByRole("alertdialog", { name: "Public HTTPS URL" });
@@ -151,7 +151,7 @@ describe("Linear channel UI", () => {
expect(screen.queryByRole("dialog", { name: "Linear" })).not.toBeInTheDocument();
});
it("automatically saves a public URL before OAuth credentials and reveals the app button", async () => {
it("commits a public URL on blur before OAuth credentials and reveals the app button", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async (input: RequestInfo | URL) => {
@@ -197,6 +197,7 @@ describe("Linear channel UI", () => {
fireEvent.change(await screen.findByPlaceholderText("https://nanobot.example.com"), {
target: { value: "https://nanobot.example.com" },
});
fireEvent.blur(screen.getByPlaceholderText("https://nanobot.example.com"));
expect(screen.queryByRole("button", { name: /^Save/ })).not.toBeInTheDocument();
await waitFor(() =>
@@ -242,6 +243,7 @@ describe("Linear channel UI", () => {
target: { value: "3980" },
});
expect(screen.getByRole("button", { name: "Connect Linear" })).toBeDisabled();
fireEvent.blur(screen.getByRole("spinbutton", { name: "Listen port" }));
await waitFor(() => expect(screen.getByRole("button", { name: "Connect Linear" })).toBeEnabled());
const params = actionCalls()[0][1] as { values: Record<string, unknown> };
expect(params.values["channels.linear.port"]).toBe("3980");
@@ -450,7 +452,7 @@ describe("Linear channel UI", () => {
});
const dialog = screen.getByRole("dialog");
fireEvent.click(within(dialog).getByRole("button", { name: "Close" }));
expect(actionCalls()).toHaveLength(1);
await waitFor(() => expect(actionCalls()).toHaveLength(1));
expect(dialog).toBeVisible();
fireEvent.click(within(dialog).getByRole("button", { name: "Close" }));
expect(actionCalls()).toHaveLength(1);
+23 -25
View File
@@ -5,7 +5,7 @@ import { channelTranslator } from "@/channel-plugins/i18n";
import type { ChannelPluginPanelProps } from "@/channel-plugins/types";
import { channelValuesForSubmit, defaultChannelFieldValues } from "@/components/settings/channels/CredentialForm";
import { channelSetup } from "@/components/settings/channels/ChannelIdentity";
import { useAutoSave } from "@/components/settings/shared/useAutoSave";
import { useEditCompletion } from "@/components/settings/shared/useEditCompletion";
import { configureChannel, disableNanobotFeature, fetchNanobotFeatures } from "@/lib/api";
import { useClient } from "@/providers/ClientProvider";
@@ -71,17 +71,21 @@ export function LinearPanel({
setTouchedFields(new Set());
setVisibleSecrets({});
setClearedSecrets(new Set());
}, [savedValuesKey, feature.name]);
}, [feature.name]);
useEffect(() => {
const savedValues = defaultChannelFieldValues(editableFields, feature.config_values);
setFieldValues((current) => ({
...savedValues,
...Object.fromEntries(Object.entries(current).filter(([key]) => touchedFields.has(key))),
}));
}, [savedValuesKey]);
const busy = saving || resetting || Boolean(actionKey);
const savedValues = defaultChannelFieldValues(editableFields, feature.config_values);
const dirty = clearedSecrets.size > 0 || editableFields.some(
(field) => fieldValues[field.key] !== savedValues[field.key],
);
const secretFields = editableFields.filter((field) => field.secret);
const touchedSecret = secretFields.some((field) =>
touchedFields.has(field.key) && Boolean(fieldValues[field.key]?.trim()),
);
const memberConfigScope = JSON.stringify([
feature.config_values?.["channels.linear.clientId"],
feature.config_values?.["channels.linear.allowFrom"],
@@ -155,8 +159,7 @@ export function LinearPanel({
const saveSettings = useCallback(async (): Promise<boolean> => {
if (savePromiseRef.current) return savePromiseRef.current;
if (!dirty) return true;
if (busy || connecting) return false;
if (resetting || actionKey || connecting) return false;
const save = (async () => {
setSaving(true);
setSaved(false);
@@ -167,10 +170,10 @@ export function LinearPanel({
feature.name,
channelValuesForSubmit(editableFields, fieldValues, touchedFields, clearedSecrets),
);
setTouchedFields(new Set());
setClearedSecrets(new Set());
setTouchedFields((current) => current === touchedFields ? new Set() : current);
setClearedSecrets((current) => current === clearedSecrets ? new Set() : current);
setVisibleSecrets({});
setFieldValues((current) => Object.fromEntries(
setFieldValues((current) => current !== fieldValues ? current : Object.fromEntries(
editableFields.map((field) => [field.key, field.secret ? "" : current[field.key] ?? ""]),
));
if (payload.nanobot_features) onFeaturesUpdate(payload.nanobot_features);
@@ -187,15 +190,11 @@ export function LinearPanel({
const result = await save;
if (savePromiseRef.current === save) savePromiseRef.current = null;
return result;
}, [busy, connecting, dirty, client, feature.name, editableFields, fieldValues,
}, [resetting, actionKey, connecting, client, feature.name, editableFields, fieldValues,
touchedFields, clearedSecrets, onFeaturesUpdate]);
useAutoSave(
{ fieldValues, clearedSecrets: [...clearedSecrets] },
dirty,
busy || connecting,
() => void saveSettings(),
!touchedSecret && !notice,
const edits = useEditCompletion(
{ fieldValues, clearedSecrets: [...clearedSecrets] }, dirty, saveSettings, true, feature.name,
);
useEffect(() => {
@@ -203,10 +202,10 @@ export function LinearPanel({
if (secretSavePromiseRef.current) {
try { await secretSavePromiseRef.current; } catch { return false; }
}
return saveSettings();
return edits.commit();
} : null);
return () => onBeforeCloseChange?.(null);
}, [dirty, saving, saveSettings, onBeforeCloseChange]);
}, [dirty, saving, edits.commit, onBeforeCloseChange]);
const saveSecret = async (key: string, value: string) => {
if (busy || dirty || connecting || savePromiseRef.current || secretSavePromiseRef.current) {
@@ -235,20 +234,19 @@ export function LinearPanel({
visibleSecrets,
clearedSecrets,
onChange: setFieldValue,
onFieldBlur: () => { void saveSettings(); },
onToggleSecret: (key: string) => {
setVisibleSecrets((current) => ({ ...current, [key]: !current[key] }));
},
disabled: busy || connecting,
disabled: resetting || Boolean(actionKey) || connecting,
compact: true,
};
return <LinearPanelView feature={feature} showBrandLogos={showBrandLogos} fields={fields}
return <div onChangeCapture={edits.onChangeCapture} onFocusCapture={edits.onFocusCapture} onBlurCapture={edits.onBlurCapture}><LinearPanelView feature={feature} showBrandLogos={showBrandLogos} fields={fields}
advancedFields={advancedFields} formProps={formProps}
state={{ busy, dirty, saving, saved, connecting, credentialsSaved, connectionGeneration, notice }}
workspaces={{ installations: cachedInstallations, logos, loading: loadingInstallations, inspectionError,
disconnectingId, notice: workspaceNotice, error: workspaceError }}
onSave={saveSettings} onSaveSecret={saveSecret} onRefreshWorkspaces={loadInstallations}
onSave={edits.commit} onSaveSecret={saveSecret} onRefreshWorkspaces={loadInstallations}
onDisconnectWorkspace={disconnectWorkspace} onClearWorkspaceError={() => setWorkspaceError(null)}
onRequestConnection={() => setConnectRequestId(id => id + 1)} onConfigureMcp={onConfigureMcp}
renderMemberAccess={(organizationId, disabled) => <LinearMemberAccess organizationId={organizationId}
@@ -268,5 +266,5 @@ export function LinearPanel({
idleLabel={tx("custom.connect", "Connect Linear")} onFeaturesUpdate={(payload) => {
onFeaturesUpdate(payload);
void workspaceStore.load(true);
}} onActiveChange={setConnecting} renderActions={renderActions} />} />;
}} onActiveChange={setConnecting} renderActions={renderActions} />} /></div>;
}
@@ -120,7 +120,10 @@ export function LinearPanelView({ feature, showBrandLogos, fields, advancedField
<button type="button"
className="inline-flex h-8 shrink-0 items-center gap-1.5 rounded px-1 text-[12px] leading-none text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-offset-2"
aria-expanded={advancedOpen} aria-controls={advancedPanelId}
onClick={() => setAdvancedOpen((current) => !current)}>
onClick={async () => {
if (advancedOpen && dirty && !(await saveSettings())) return;
setAdvancedOpen((current) => !current);
}}>
{t("settings.channels.advanced", { defaultValue: "Advanced" })}
<ChevronDown className={cn(
"h-3.5 w-3.5 transition-transform motion-reduce:transition-none",