feat(provider): load live settings when editing active provider
- Add live settings fetching when editing the current active provider - Use useMemo to prioritize live settings over SSOT configuration - Implement graceful fallback to SSOT if live settings fetch fails - Prevent unnecessary API calls with condition checks (open state and current provider)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useCallback } from "react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Save } from "lucide-react";
|
||||
import {
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
ProviderForm,
|
||||
type ProviderFormValues,
|
||||
} from "@/components/providers/forms/ProviderForm";
|
||||
import type { AppId } from "@/lib/api";
|
||||
import { providersApi, vscodeApi, type AppId } from "@/lib/api";
|
||||
|
||||
interface EditProviderDialogProps {
|
||||
open: boolean;
|
||||
@@ -34,6 +34,45 @@ export function EditProviderDialog({
|
||||
}: EditProviderDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 默认使用传入的 provider.settingsConfig,若当前编辑对象是“当前生效供应商”,则尝试读取实时配置替换初始值
|
||||
const [liveSettings, setLiveSettings] = useState<Record<string, unknown> | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
const load = async () => {
|
||||
if (!open || !provider) {
|
||||
setLiveSettings(null);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const currentId = await providersApi.getCurrent(appId);
|
||||
if (currentId && provider.id === currentId) {
|
||||
try {
|
||||
const live = (await vscodeApi.getLiveProviderSettings(appId)) as Record<string, unknown>;
|
||||
if (!cancelled && live && typeof live === "object") {
|
||||
setLiveSettings(live);
|
||||
}
|
||||
} catch {
|
||||
// 读取实时配置失败则回退到 SSOT(不打断编辑流程)
|
||||
if (!cancelled) setLiveSettings(null);
|
||||
}
|
||||
} else {
|
||||
if (!cancelled) setLiveSettings(null);
|
||||
}
|
||||
} finally {
|
||||
// no-op
|
||||
}
|
||||
};
|
||||
void load();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [open, provider, appId]);
|
||||
|
||||
const initialSettingsConfig = useMemo(() => {
|
||||
return (liveSettings ?? provider?.settingsConfig ?? {}) as Record<string, unknown>;
|
||||
}, [liveSettings, provider]);
|
||||
|
||||
const handleSubmit = useCallback(
|
||||
async (values: ProviderFormValues) => {
|
||||
if (!provider) return;
|
||||
@@ -82,7 +121,8 @@ export function EditProviderDialog({
|
||||
initialData={{
|
||||
name: provider.name,
|
||||
websiteUrl: provider.websiteUrl,
|
||||
settingsConfig: provider.settingsConfig,
|
||||
// 若读取到实时配置则优先使用
|
||||
settingsConfig: initialSettingsConfig,
|
||||
category: provider.category,
|
||||
meta: provider.meta,
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user