From c9a4938866ce797557dd33a2446e3bdb40175b76 Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 10 Jan 2026 11:10:23 +0800 Subject: [PATCH] fix(provider-form): reset baseUrl and apiKey states when switching presets Fix state synchronization in useBaseUrlState and useApiKeyState hooks to properly clear values when config is reset. Previously, when switching from a preset to "custom", the baseUrl and apiKey states would retain their old values because the sync logic only updated when new values existed, not when they were cleared. Changes: - useBaseUrlState: Always sync baseUrl to config value (empty if undefined) - useApiKeyState: Remove hasApiKeyField check that prevented clearing --- src/components/providers/forms/hooks/useApiKeyState.ts | 6 +----- src/components/providers/forms/hooks/useBaseUrlState.ts | 5 +++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/components/providers/forms/hooks/useApiKeyState.ts b/src/components/providers/forms/hooks/useApiKeyState.ts index 9cd5fde39..9101d2e35 100644 --- a/src/components/providers/forms/hooks/useApiKeyState.ts +++ b/src/components/providers/forms/hooks/useApiKeyState.ts @@ -43,11 +43,7 @@ export function useApiKeyState({ return; } - // 仅当配置确实包含 API Key 字段时才同步(避免无意清空用户正在输入的 key) - if (!hasApiKeyField(initialConfig, appType)) { - return; - } - + // 从配置中提取 API Key(如果不存在则返回空字符串) const extracted = getApiKeyFromConfig(initialConfig, appType); if (extracted !== apiKey) { setApiKey(extracted); diff --git a/src/components/providers/forms/hooks/useBaseUrlState.ts b/src/components/providers/forms/hooks/useBaseUrlState.ts index 61b1ad997..2deffcbac 100644 --- a/src/components/providers/forms/hooks/useBaseUrlState.ts +++ b/src/components/providers/forms/hooks/useBaseUrlState.ts @@ -41,8 +41,9 @@ export function useBaseUrlState({ try { const config = JSON.parse(settingsConfig || "{}"); const envUrl: unknown = config?.env?.ANTHROPIC_BASE_URL; - if (typeof envUrl === "string" && envUrl && envUrl.trim() !== baseUrl) { - setBaseUrl(envUrl.trim()); + const nextUrl = typeof envUrl === "string" ? envUrl.trim() : ""; + if (nextUrl !== baseUrl) { + setBaseUrl(nextUrl); } } catch { // ignore