import React, { useState, useEffect } from "react"; import { X, Save } from "lucide-react"; import { extractBaseUrlFromToml } from "../../utils/providerConfigUtils"; interface CodexConfigEditorProps { authValue: string; configValue: string; onAuthChange: (value: string) => void; onConfigChange: (value: string) => void; onAuthBlur?: () => void; useCommonConfig: boolean; onCommonConfigToggle: (checked: boolean) => void; commonConfigSnippet: string; onCommonConfigSnippetChange: (value: string) => void; commonConfigError: string; authError: string; } const CodexConfigEditor: React.FC = ({ authValue, configValue, onAuthChange, onConfigChange, onAuthBlur, useCommonConfig, onCommonConfigToggle, commonConfigSnippet, onCommonConfigSnippetChange, commonConfigError, authError, }) => { const [isCommonConfigModalOpen, setIsCommonConfigModalOpen] = useState(false); const [isWritingVscode, setIsWritingVscode] = useState(false); const [vscodeError, setVscodeError] = useState(""); const [vscodeSuccess, setVscodeSuccess] = useState(""); useEffect(() => { if (commonConfigError && !isCommonConfigModalOpen) { setIsCommonConfigModalOpen(true); } }, [commonConfigError, isCommonConfigModalOpen]); useEffect(() => { if (!vscodeSuccess) return; const timer = window.setTimeout(() => { setVscodeSuccess(""); }, 3000); return () => window.clearTimeout(timer); }, [vscodeSuccess]); // 支持按下 ESC 关闭弹窗 useEffect(() => { if (!isCommonConfigModalOpen) return; const onKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); closeModal(); } }; window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }, [isCommonConfigModalOpen]); const closeModal = () => { setIsCommonConfigModalOpen(false); }; const handleAuthChange = (value: string) => { onAuthChange(value); }; const handleConfigChange = (value: string) => { onConfigChange(value); }; const handleCommonConfigSnippetChange = (value: string) => { onCommonConfigSnippetChange(value); }; const handleWriteVscodeConfig = async () => { setVscodeError(""); setVscodeSuccess(""); if (typeof window === "undefined" || !window.api?.writeVscodeSettings) { setVscodeError("当前环境暂不支持写入 VS Code 配置"); return; } const trimmed = configValue.trim(); if (!trimmed) { setVscodeError("请先填写 config.toml,再写入 VS Code 配置"); return; } const baseUrl = extractBaseUrlFromToml(trimmed); if (!baseUrl) { setVscodeError("未在 config.toml 中找到 base_url 字段"); return; } setIsWritingVscode(true); try { const success = await window.api.writeVscodeSettings(baseUrl); if (success) { setVscodeSuccess("已写入 VS Code 配置"); } else { setVscodeError("写入 VS Code 配置失败,请稍后重试"); } } catch (error) { setVscodeError(`写入 VS Code 配置失败: ${String(error)}`); } finally { setIsWritingVscode(false); } }; return (