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
This commit is contained in:
Jason
2026-01-10 11:10:23 +08:00
parent 95ed6d6903
commit c9a4938866
2 changed files with 4 additions and 7 deletions

View File

@@ -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);

View File

@@ -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