import React, { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Provider } from "../types"; import { Play, Edit3, Trash2, CheckCircle2, Users, Check } from "lucide-react"; import { buttonStyles, cardStyles, badgeStyles, cn } from "../lib/styles"; import { AppType } from "../lib/tauri-api"; // 不再在列表中显示分类徽章,避免造成困惑 interface ProviderListProps { providers: Record; currentProviderId: string; onSwitch: (id: string) => void; onDelete: (id: string) => void; onEdit: (id: string) => void; appType?: AppType; onNotify?: ( message: string, type: "success" | "error", duration?: number ) => void; } const ProviderList: React.FC = ({ providers, currentProviderId, onSwitch, onDelete, onEdit, appType, onNotify, }) => { const { t } = useTranslation(); // 提取API地址(兼容不同供应商配置:Claude env / Codex TOML) const getApiUrl = (provider: Provider): string => { try { const cfg = provider.settingsConfig; // Claude/Anthropic: 从 env 中读取 if (cfg?.env?.ANTHROPIC_BASE_URL) { return cfg.env.ANTHROPIC_BASE_URL; } // Codex: 从 TOML 配置中解析 base_url if (typeof cfg?.config === "string" && cfg.config.includes("base_url")) { // 支持单/双引号 const match = cfg.config.match(/base_url\s*=\s*(['"])([^'\"]+)\1/); if (match && match[2]) return match[2]; } return t("provider.notConfigured"); } catch { return t("provider.configError"); } }; const handleUrlClick = async (url: string) => { try { await window.api.openExternal(url); } catch (error) { console.error(t("console.openLinkFailed"), error); } }; const [claudeApplied, setClaudeApplied] = useState(false); // 检查 Claude 插件配置是否已应用 useEffect(() => { const checkClaude = async () => { if (appType !== "claude" || !currentProviderId) { setClaudeApplied(false); return; } try { const applied = await window.api.isClaudePluginApplied(); setClaudeApplied(applied); } catch (error) { console.error(t("console.setupListenerFailed"), error); setClaudeApplied(false); } }; checkClaude(); }, [appType, currentProviderId, providers, t]); const handleApplyToClaudePlugin = async () => { try { await window.api.applyClaudePluginConfig({ official: false }); onNotify?.(t("notifications.appliedToClaudePlugin"), "success", 3000); setClaudeApplied(true); } catch (error: any) { console.error(error); const msg = error && error.message ? error.message : t("notifications.syncClaudePluginFailed"); onNotify?.(msg, "error", 5000); } }; const handleRemoveFromClaudePlugin = async () => { try { await window.api.applyClaudePluginConfig({ official: true }); onNotify?.(t("notifications.removedFromClaudePlugin"), "success", 3000); setClaudeApplied(false); } catch (error: any) { console.error(error); const msg = error && error.message ? error.message : t("notifications.syncClaudePluginFailed"); onNotify?.(msg, "error", 5000); } }; // 对供应商列表进行排序 const sortedProviders = Object.values(providers).sort((a, b) => { // 按添加时间排序 // 没有时间戳的视为最早添加的(排在最前面) // 有时间戳的按时间升序排列 const timeA = a.createdAt || 0; const timeB = b.createdAt || 0; // 如果都没有时间戳,按名称排序 if (timeA === 0 && timeB === 0) { return a.name.localeCompare(b.name, "zh-CN"); } // 如果只有一个没有时间戳,没有时间戳的排在前面 if (timeA === 0) return -1; if (timeB === 0) return 1; // 都有时间戳,按时间升序 return timeA - timeB; }); return (
{sortedProviders.length === 0 ? (

{t("provider.noProviders")}

{t("provider.noProvidersDescription")}

) : (
{sortedProviders.map((provider) => { const isCurrent = provider.id === currentProviderId; const apiUrl = getApiUrl(provider); return (

{provider.name}

{/* 分类徽章已移除 */}
{t("provider.currentlyUsing")}
{provider.websiteUrl ? ( ) : ( {apiUrl} )}
{appType === "claude" ? (
{provider.category !== "official" && isCurrent && ( )}
) : null}
); })}
)}
); }; export default ProviderList;