import { useEffect, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/Button'; import { HeaderInputList } from '@/components/ui/HeaderInputList'; import { Input } from '@/components/ui/Input'; import { Modal } from '@/components/ui/Modal'; import type { ProviderKeyConfig } from '@/types'; import { headersToEntries } from '@/utils/headers'; import { modelsToEntries } from '@/components/ui/modelInputListUtils'; import { excludedModelsToText } from '../utils'; import type { ProviderFormState, ProviderModalProps } from '../types'; interface CodexModalProps extends ProviderModalProps { isSaving: boolean; } const buildEmptyForm = (): ProviderFormState => ({ apiKey: '', prefix: '', baseUrl: '', proxyUrl: '', headers: [], models: [], excludedModels: [], modelEntries: [{ name: '', alias: '' }], excludedText: '', }); export function CodexModal({ isOpen, editIndex, initialData, onClose, onSave, isSaving, }: CodexModalProps) { const { t } = useTranslation(); const [form, setForm] = useState(buildEmptyForm); useEffect(() => { if (!isOpen) return; if (initialData) { // eslint-disable-next-line react-hooks/set-state-in-effect setForm({ ...initialData, headers: headersToEntries(initialData.headers), modelEntries: modelsToEntries(initialData.models), excludedText: excludedModelsToText(initialData.excludedModels), }); return; } setForm(buildEmptyForm()); }, [initialData, isOpen]); return ( } > setForm((prev) => ({ ...prev, apiKey: e.target.value }))} /> setForm((prev) => ({ ...prev, prefix: e.target.value }))} hint={t('ai_providers.prefix_hint')} /> setForm((prev) => ({ ...prev, baseUrl: e.target.value }))} /> setForm((prev) => ({ ...prev, proxyUrl: e.target.value }))} /> setForm((prev) => ({ ...prev, headers: entries }))} addLabel={t('common.custom_headers_add')} keyPlaceholder={t('common.custom_headers_key_placeholder')} valuePlaceholder={t('common.custom_headers_value_placeholder')} />