2025-08-06 21:00:49 +08:00
|
|
|
|
import React, { useState } from "react";
|
|
|
|
|
|
import { Provider } from "../../shared/types";
|
|
|
|
|
|
import { inferWebsiteUrl } from "../../shared/utils";
|
|
|
|
|
|
import "./AddProviderModal.css";
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
|
|
|
|
|
interface AddProviderModalProps {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
onAdd: (provider: Omit<Provider, "id">) => void;
|
|
|
|
|
|
onClose: () => void;
|
2025-08-04 22:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 21:00:49 +08:00
|
|
|
|
const AddProviderModal: React.FC<AddProviderModalProps> = ({
|
|
|
|
|
|
onAdd,
|
|
|
|
|
|
onClose,
|
|
|
|
|
|
}) => {
|
2025-08-04 22:16:26 +08:00
|
|
|
|
const [formData, setFormData] = useState({
|
2025-08-06 21:00:49 +08:00
|
|
|
|
name: "",
|
|
|
|
|
|
apiUrl: "",
|
|
|
|
|
|
apiKey: "",
|
|
|
|
|
|
websiteUrl: "",
|
|
|
|
|
|
});
|
|
|
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
|
const [error, setError] = useState("");
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
setError("");
|
|
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
if (!formData.name || !formData.apiUrl || !formData.apiKey) {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
setError("请填写所有必填字段");
|
|
|
|
|
|
return;
|
2025-08-04 22:16:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-06 21:00:49 +08:00
|
|
|
|
onAdd(formData);
|
|
|
|
|
|
};
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-08-06 21:00:49 +08:00
|
|
|
|
const handleChange = (
|
|
|
|
|
|
e: React.ChangeEvent<HTMLInputElement | HTMLSelectElement>
|
|
|
|
|
|
) => {
|
|
|
|
|
|
const { name, value } = e.target;
|
2025-08-06 10:09:58 +08:00
|
|
|
|
const newFormData = {
|
2025-08-04 22:16:26 +08:00
|
|
|
|
...formData,
|
2025-08-06 21:00:49 +08:00
|
|
|
|
[name]: value,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-06 10:09:58 +08:00
|
|
|
|
// 如果修改的是API地址,自动推测网站地址
|
2025-08-06 21:00:49 +08:00
|
|
|
|
if (name === "apiUrl") {
|
|
|
|
|
|
newFormData.websiteUrl = inferWebsiteUrl(value);
|
2025-08-06 10:09:58 +08:00
|
|
|
|
}
|
2025-08-06 21:00:49 +08:00
|
|
|
|
|
|
|
|
|
|
setFormData(newFormData);
|
|
|
|
|
|
};
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-08-06 11:42:58 +08:00
|
|
|
|
const handleApiUrlBlur = (e: React.FocusEvent<HTMLInputElement>) => {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
const apiUrl = e.target.value.trim();
|
2025-08-06 11:42:58 +08:00
|
|
|
|
if (apiUrl) {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
let normalizedApiUrl = apiUrl;
|
|
|
|
|
|
|
2025-08-06 11:42:58 +08:00
|
|
|
|
// 如果没有协议,添加 https://
|
|
|
|
|
|
if (!normalizedApiUrl.match(/^https?:\/\//)) {
|
2025-08-06 21:00:49 +08:00
|
|
|
|
normalizedApiUrl = "https://" + normalizedApiUrl;
|
2025-08-06 11:42:58 +08:00
|
|
|
|
}
|
2025-08-06 21:00:49 +08:00
|
|
|
|
|
|
|
|
|
|
setFormData((prev) => ({
|
2025-08-06 11:42:58 +08:00
|
|
|
|
...prev,
|
|
|
|
|
|
apiUrl: normalizedApiUrl,
|
2025-08-06 21:00:49 +08:00
|
|
|
|
websiteUrl: inferWebsiteUrl(normalizedApiUrl),
|
|
|
|
|
|
}));
|
2025-08-06 11:42:58 +08:00
|
|
|
|
}
|
2025-08-06 21:00:49 +08:00
|
|
|
|
};
|
2025-08-06 11:42:58 +08:00
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
// 预设的供应商配置
|
|
|
|
|
|
const presets = [
|
|
|
|
|
|
{
|
2025-08-06 21:00:49 +08:00
|
|
|
|
name: "Anthropic 官方",
|
|
|
|
|
|
apiUrl: "https://api.anthropic.com",
|
2025-08-04 22:16:26 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
2025-08-06 21:00:49 +08:00
|
|
|
|
name: "PackyCode",
|
|
|
|
|
|
apiUrl: "https://api.packycode.com",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "YesCode",
|
|
|
|
|
|
apiUrl: "https://co.yes.vg",
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
name: "AnyRouter",
|
|
|
|
|
|
apiUrl: "https://anyrouter.top",
|
|
|
|
|
|
},
|
|
|
|
|
|
];
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-08-06 21:00:49 +08:00
|
|
|
|
const applyPreset = (preset: (typeof presets)[0]) => {
|
2025-08-06 10:09:58 +08:00
|
|
|
|
const newFormData = {
|
2025-08-04 22:16:26 +08:00
|
|
|
|
...formData,
|
|
|
|
|
|
name: preset.name,
|
2025-08-06 21:00:49 +08:00
|
|
|
|
apiUrl: preset.apiUrl,
|
|
|
|
|
|
};
|
2025-08-06 10:09:58 +08:00
|
|
|
|
// 应用预设时也自动推测网站地址
|
2025-08-06 21:00:49 +08:00
|
|
|
|
newFormData.websiteUrl = inferWebsiteUrl(preset.apiUrl);
|
|
|
|
|
|
setFormData(newFormData);
|
|
|
|
|
|
};
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
2025-08-06 21:00:49 +08:00
|
|
|
|
<div className="modal-overlay">
|
|
|
|
|
|
<div className="modal-content">
|
2025-08-04 22:16:26 +08:00
|
|
|
|
<h2>添加新供应商</h2>
|
2025-08-06 21:00:49 +08:00
|
|
|
|
|
|
|
|
|
|
{error && <div className="error-message">{error}</div>}
|
|
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
<div className="presets">
|
|
|
|
|
|
<label>快速选择:</label>
|
|
|
|
|
|
<div className="preset-buttons">
|
|
|
|
|
|
{presets.map((preset, index) => (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={index}
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="preset-btn"
|
|
|
|
|
|
onClick={() => applyPreset(preset)}
|
|
|
|
|
|
>
|
|
|
|
|
|
{preset.name}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="name">供应商名称 *</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
id="name"
|
|
|
|
|
|
name="name"
|
|
|
|
|
|
value={formData.name}
|
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
|
placeholder="例如:官方 Anthropic"
|
|
|
|
|
|
required
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="apiUrl">API 地址 *</label>
|
|
|
|
|
|
<input
|
|
|
|
|
|
type="url"
|
|
|
|
|
|
id="apiUrl"
|
|
|
|
|
|
name="apiUrl"
|
|
|
|
|
|
value={formData.apiUrl}
|
|
|
|
|
|
onChange={handleChange}
|
2025-08-06 11:42:58 +08:00
|
|
|
|
onBlur={handleApiUrlBlur}
|
2025-08-04 22:16:26 +08:00
|
|
|
|
placeholder="https://api.anthropic.com"
|
|
|
|
|
|
required
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-08-06 10:09:58 +08:00
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="websiteUrl">网站地址</label>
|
|
|
|
|
|
<input
|
2025-08-06 11:42:58 +08:00
|
|
|
|
type="text"
|
2025-08-06 10:09:58 +08:00
|
|
|
|
id="websiteUrl"
|
|
|
|
|
|
name="websiteUrl"
|
|
|
|
|
|
value={formData.websiteUrl}
|
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
|
placeholder="https://example.com(可选)"
|
|
|
|
|
|
/>
|
2025-08-06 21:00:49 +08:00
|
|
|
|
<small className="field-hint">
|
|
|
|
|
|
用于在面板中显示可访问的网站链接,留空则显示API地址
|
|
|
|
|
|
</small>
|
2025-08-06 10:09:58 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-08-04 22:16:26 +08:00
|
|
|
|
<div className="form-group">
|
|
|
|
|
|
<label htmlFor="apiKey">API Key *</label>
|
2025-08-05 09:51:41 +08:00
|
|
|
|
<div className="password-input-wrapper">
|
|
|
|
|
|
<input
|
|
|
|
|
|
type={showPassword ? "text" : "password"}
|
|
|
|
|
|
id="apiKey"
|
|
|
|
|
|
name="apiKey"
|
|
|
|
|
|
value={formData.apiKey}
|
|
|
|
|
|
onChange={handleChange}
|
2025-08-06 21:00:49 +08:00
|
|
|
|
placeholder={formData.name === "YesCode" ? "cr_..." : "sk-..."}
|
2025-08-05 09:51:41 +08:00
|
|
|
|
required
|
|
|
|
|
|
/>
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
className="password-toggle"
|
|
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
|
|
|
|
tabIndex={-1}
|
|
|
|
|
|
title={showPassword ? "隐藏密码" : "显示密码"}
|
|
|
|
|
|
>
|
|
|
|
|
|
{showPassword ? (
|
2025-08-06 21:00:49 +08:00
|
|
|
|
<svg
|
|
|
|
|
|
width="20"
|
|
|
|
|
|
height="20"
|
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
|
fill="none"
|
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
|
>
|
2025-08-05 09:51:41 +08:00
|
|
|
|
<path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" />
|
|
|
|
|
|
<circle cx="12" cy="12" r="3" />
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
) : (
|
2025-08-06 21:00:49 +08:00
|
|
|
|
<svg
|
|
|
|
|
|
width="20"
|
|
|
|
|
|
height="20"
|
|
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
|
|
fill="none"
|
|
|
|
|
|
stroke="currentColor"
|
|
|
|
|
|
>
|
2025-08-05 09:51:41 +08:00
|
|
|
|
<path d="M17.94 17.94A10.07 10.07 0 0 1 12 20c-7 0-11-8-11-8a18.45 18.45 0 0 1 5.06-5.94M9.9 4.24A9.12 9.12 0 0 1 12 4c7 0 11 8 11 8a18.5 18.5 0 0 1-2.16 3.19m-6.72-1.07a3 3 0 1 1-4.24-4.24" />
|
|
|
|
|
|
<line x1="1" y1="1" x2="23" y2="23" />
|
|
|
|
|
|
</svg>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
2025-08-04 22:16:26 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="form-actions">
|
|
|
|
|
|
<button type="button" className="cancel-btn" onClick={onClose}>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<button type="submit" className="submit-btn">
|
|
|
|
|
|
添加
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-08-06 21:00:49 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|
2025-08-04 22:16:26 +08:00
|
|
|
|
|
2025-08-06 21:00:49 +08:00
|
|
|
|
export default AddProviderModal;
|