fix(provider-list): Modify the keyField function to support index parameters and ensure uniqueness

fix(ai-providers): Optimize configuration synchronization logic in OpenAI editing layout
This commit is contained in:
Supra4E8C
2026-02-12 16:36:44 +08:00
parent e40c3488fe
commit 2d841c0a2f
3 changed files with 16 additions and 10 deletions

View File

@@ -87,7 +87,7 @@ export function OpenAISection({
<ProviderList<OpenAIProviderConfig> <ProviderList<OpenAIProviderConfig>
items={configs} items={configs}
loading={loading} loading={loading}
keyField={(item) => item.name} keyField={(_, index) => `openai-provider-${index}`}
emptyTitle={t('ai_providers.openai_empty_title')} emptyTitle={t('ai_providers.openai_empty_title')}
emptyDescription={t('ai_providers.openai_empty_desc')} emptyDescription={t('ai_providers.openai_empty_desc')}
onEdit={onEdit} onEdit={onEdit}

View File

@@ -6,7 +6,7 @@ import { EmptyState } from '@/components/ui/EmptyState';
interface ProviderListProps<T> { interface ProviderListProps<T> {
items: T[]; items: T[];
loading: boolean; loading: boolean;
keyField: (item: T) => string; keyField: (item: T, index: number) => string;
renderContent: (item: T, index: number) => ReactNode; renderContent: (item: T, index: number) => ReactNode;
onEdit: (index: number) => void; onEdit: (index: number) => void;
onDelete: (index: number) => void; onDelete: (index: number) => void;
@@ -48,7 +48,7 @@ export function ProviderList<T>({
const rowDisabled = getRowDisabled ? getRowDisabled(item, index) : false; const rowDisabled = getRowDisabled ? getRowDisabled(item, index) : false;
return ( return (
<div <div
key={keyField(item)} key={keyField(item, index)}
className="item-row" className="item-row"
style={rowDisabled ? { opacity: 0.6 } : undefined} style={rowDisabled ? { opacity: 0.6 } : undefined}
> >

View File

@@ -77,8 +77,6 @@ export function AiProvidersOpenAIEditLayout() {
const config = useConfigStore((state) => state.config); const config = useConfigStore((state) => state.config);
const fetchConfig = useConfigStore((state) => state.fetchConfig); const fetchConfig = useConfigStore((state) => state.fetchConfig);
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
const clearCache = useConfigStore((state) => state.clearCache);
const isCacheValid = useConfigStore((state) => state.isCacheValid); const isCacheValid = useConfigStore((state) => state.isCacheValid);
const [providers, setProviders] = useState<OpenAIProviderConfig[]>( const [providers, setProviders] = useState<OpenAIProviderConfig[]>(
@@ -335,9 +333,18 @@ export function AiProvidersOpenAIEditLayout() {
: [...providers, payload]; : [...providers, payload];
await providersApi.saveOpenAIProviders(nextList); await providersApi.saveOpenAIProviders(nextList);
setProviders(nextList);
updateConfigValue('openai-compatibility', nextList); let syncedProviders = nextList;
clearCache('openai-compatibility'); try {
const latest = await fetchConfig('openai-compatibility', true);
if (Array.isArray(latest)) {
syncedProviders = latest as OpenAIProviderConfig[];
}
} catch {
// 保存成功后刷新失败时,回退到本地计算结果,避免页面数据为空或回退
}
setProviders(syncedProviders);
showNotification( showNotification(
editIndex !== null editIndex !== null
? t('notification.openai_provider_updated') ? t('notification.openai_provider_updated')
@@ -351,15 +358,14 @@ export function AiProvidersOpenAIEditLayout() {
setSaving(false); setSaving(false);
} }
}, [ }, [
clearCache,
editIndex, editIndex,
fetchConfig,
form, form,
handleBack, handleBack,
providers, providers,
testModel, testModel,
showNotification, showNotification,
t, t,
updateConfigValue,
]); ]);
const resolvedLoading = !draft?.initialized; const resolvedLoading = !draft?.initialized;