import React, { useState } from "react"; import { Provider } from "../../shared/types"; import { inferWebsiteUrl } from "../../shared/utils"; import "./AddProviderModal.css"; interface AddProviderModalProps { onAdd: (provider: Omit) => void; onClose: () => void; } const AddProviderModal: React.FC = ({ onAdd, onClose, }) => { const [formData, setFormData] = useState({ name: "", apiUrl: "", apiKey: "", websiteUrl: "", }); const [showPassword, setShowPassword] = useState(false); const [error, setError] = useState(""); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); setError(""); if (!formData.name || !formData.apiUrl || !formData.apiKey) { setError("请填写所有必填字段"); return; } onAdd(formData); }; const handleChange = ( e: React.ChangeEvent ) => { const { name, value } = e.target; const newFormData = { ...formData, [name]: value, }; // 如果修改的是API地址,自动推测网站地址 if (name === "apiUrl") { newFormData.websiteUrl = inferWebsiteUrl(value); } setFormData(newFormData); }; const handleApiUrlBlur = (e: React.FocusEvent) => { const apiUrl = e.target.value.trim(); if (apiUrl) { let normalizedApiUrl = apiUrl; // 如果没有协议,添加 https:// if (!normalizedApiUrl.match(/^https?:\/\//)) { normalizedApiUrl = "https://" + normalizedApiUrl; } setFormData((prev) => ({ ...prev, apiUrl: normalizedApiUrl, websiteUrl: inferWebsiteUrl(normalizedApiUrl), })); } }; // 预设的供应商配置 const presets = [ { name: "Anthropic 官方", apiUrl: "https://api.anthropic.com", }, { name: "PackyCode", apiUrl: "https://api.packycode.com", }, { name: "YesCode", apiUrl: "https://co.yes.vg", }, { name: "AnyRouter", apiUrl: "https://anyrouter.top", }, ]; const applyPreset = (preset: (typeof presets)[0]) => { const newFormData = { ...formData, name: preset.name, apiUrl: preset.apiUrl, }; // 应用预设时也自动推测网站地址 newFormData.websiteUrl = inferWebsiteUrl(preset.apiUrl); setFormData(newFormData); }; return (

添加新供应商

{error &&
{error}
}
{presets.map((preset, index) => ( ))}
用于在面板中显示可访问的网站链接,留空则显示API地址
); }; export default AddProviderModal;