2025-10-16 21:40:42 +08:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
import { FormLabel } from "@/components/ui/form";
|
2025-11-12 22:41:26 +08:00
|
|
|
|
import { ClaudeIcon, CodexIcon, GeminiIcon } from "@/components/BrandIcons";
|
2025-11-06 15:22:38 +08:00
|
|
|
|
import { Zap, Star } from "lucide-react";
|
2025-11-02 21:05:48 +08:00
|
|
|
|
import type { ProviderPreset } from "@/config/claudeProviderPresets";
|
2025-10-16 21:40:42 +08:00
|
|
|
|
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
|
2025-11-12 22:41:26 +08:00
|
|
|
|
import type { GeminiProviderPreset } from "@/config/geminiProviderPresets";
|
2025-10-19 12:24:47 +08:00
|
|
|
|
import type { ProviderCategory } from "@/types";
|
2025-11-02 22:22:45 +08:00
|
|
|
|
import type { AppId } from "@/lib/api";
|
2025-10-16 21:40:42 +08:00
|
|
|
|
|
|
|
|
|
|
type PresetEntry = {
|
|
|
|
|
|
id: string;
|
2025-11-12 22:41:26 +08:00
|
|
|
|
preset: ProviderPreset | CodexProviderPreset | GeminiProviderPreset;
|
2025-10-16 21:40:42 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
interface ProviderPresetSelectorProps {
|
|
|
|
|
|
selectedPresetId: string | null;
|
|
|
|
|
|
groupedPresets: Record<string, PresetEntry[]>;
|
|
|
|
|
|
categoryKeys: string[];
|
|
|
|
|
|
presetCategoryLabels: Record<string, string>;
|
|
|
|
|
|
onPresetChange: (value: string) => void;
|
2025-10-19 12:24:47 +08:00
|
|
|
|
category?: ProviderCategory; // 新增:当前选中的分类
|
2025-11-02 22:22:45 +08:00
|
|
|
|
appId?: AppId;
|
|
|
|
|
|
onOpenWizard?: () => void; // Codex 专用:打开配置向导
|
2025-10-16 21:40:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function ProviderPresetSelector({
|
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
|
groupedPresets,
|
|
|
|
|
|
categoryKeys,
|
|
|
|
|
|
presetCategoryLabels,
|
|
|
|
|
|
onPresetChange,
|
2025-10-19 12:24:47 +08:00
|
|
|
|
category,
|
2025-11-02 22:22:45 +08:00
|
|
|
|
appId,
|
|
|
|
|
|
onOpenWizard,
|
2025-10-16 21:40:42 +08:00
|
|
|
|
}: ProviderPresetSelectorProps) {
|
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
2025-10-19 12:24:47 +08:00
|
|
|
|
// 根据分类获取提示文字
|
2025-11-02 22:22:45 +08:00
|
|
|
|
const getCategoryHint = (): React.ReactNode => {
|
2025-10-19 12:24:47 +08:00
|
|
|
|
switch (category) {
|
|
|
|
|
|
case "official":
|
|
|
|
|
|
return t("providerForm.officialHint", {
|
|
|
|
|
|
defaultValue: "💡 官方供应商使用浏览器登录,无需配置 API Key",
|
|
|
|
|
|
});
|
|
|
|
|
|
case "cn_official":
|
|
|
|
|
|
return t("providerForm.cnOfficialApiKeyHint", {
|
|
|
|
|
|
defaultValue: "💡 国产官方供应商只需填写 API Key,请求地址已预设",
|
|
|
|
|
|
});
|
|
|
|
|
|
case "aggregator":
|
|
|
|
|
|
return t("providerForm.aggregatorApiKeyHint", {
|
|
|
|
|
|
defaultValue: "💡 聚合服务供应商只需填写 API Key 即可使用",
|
|
|
|
|
|
});
|
|
|
|
|
|
case "third_party":
|
|
|
|
|
|
return t("providerForm.thirdPartyApiKeyHint", {
|
|
|
|
|
|
defaultValue: "💡 第三方供应商需要填写 API Key 和请求地址",
|
|
|
|
|
|
});
|
|
|
|
|
|
case "custom":
|
2025-11-02 22:22:45 +08:00
|
|
|
|
// Codex 自定义:在此位置显示"手动配置…或者 使用配置向导"
|
|
|
|
|
|
if (appId === "codex" && onOpenWizard) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<>
|
|
|
|
|
|
{t("providerForm.manualConfig")}
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={onOpenWizard}
|
|
|
|
|
|
className="ml-1 text-blue-500 dark:text-blue-400 hover:text-blue-600 dark:hover:text-blue-300 underline-offset-2 hover:underline"
|
|
|
|
|
|
aria-label={t("providerForm.openConfigWizard")}
|
|
|
|
|
|
>
|
|
|
|
|
|
{t("providerForm.useConfigWizard")}
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 其他情况沿用原提示
|
2025-10-19 12:24:47 +08:00
|
|
|
|
return t("providerForm.customApiKeyHint", {
|
|
|
|
|
|
defaultValue: "💡 自定义配置需手动填写所有必要字段",
|
|
|
|
|
|
});
|
|
|
|
|
|
default:
|
|
|
|
|
|
return t("providerPreset.hint", {
|
|
|
|
|
|
defaultValue: "选择预设后可继续调整下方字段。",
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-19 23:11:48 +08:00
|
|
|
|
// 渲染预设按钮的图标
|
2025-11-12 22:41:26 +08:00
|
|
|
|
const renderPresetIcon = (
|
|
|
|
|
|
preset: ProviderPreset | CodexProviderPreset | GeminiProviderPreset,
|
|
|
|
|
|
) => {
|
2025-10-19 23:11:48 +08:00
|
|
|
|
const iconType = preset.theme?.icon;
|
|
|
|
|
|
if (!iconType) return null;
|
|
|
|
|
|
|
|
|
|
|
|
switch (iconType) {
|
|
|
|
|
|
case "claude":
|
|
|
|
|
|
return <ClaudeIcon size={14} />;
|
|
|
|
|
|
case "codex":
|
|
|
|
|
|
return <CodexIcon size={14} />;
|
2025-11-12 22:41:26 +08:00
|
|
|
|
case "gemini":
|
|
|
|
|
|
return <GeminiIcon size={14} />;
|
2025-10-19 23:11:48 +08:00
|
|
|
|
case "generic":
|
|
|
|
|
|
return <Zap size={14} />;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取预设按钮的样式类名
|
|
|
|
|
|
const getPresetButtonClass = (
|
|
|
|
|
|
isSelected: boolean,
|
2025-11-12 22:41:26 +08:00
|
|
|
|
preset: ProviderPreset | CodexProviderPreset | GeminiProviderPreset,
|
2025-10-19 23:11:48 +08:00
|
|
|
|
) => {
|
|
|
|
|
|
const baseClass =
|
|
|
|
|
|
"inline-flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors";
|
|
|
|
|
|
|
|
|
|
|
|
if (isSelected) {
|
|
|
|
|
|
// 如果有自定义主题,使用自定义颜色
|
|
|
|
|
|
if (preset.theme?.backgroundColor) {
|
|
|
|
|
|
return `${baseClass} text-white`;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 默认使用主题蓝色
|
|
|
|
|
|
return `${baseClass} bg-blue-500 text-white dark:bg-blue-600`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return `${baseClass} bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700`;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取预设按钮的内联样式(用于自定义背景色)
|
|
|
|
|
|
const getPresetButtonStyle = (
|
|
|
|
|
|
isSelected: boolean,
|
2025-11-12 22:41:26 +08:00
|
|
|
|
preset: ProviderPreset | CodexProviderPreset | GeminiProviderPreset,
|
2025-10-19 23:11:48 +08:00
|
|
|
|
) => {
|
|
|
|
|
|
if (!isSelected || !preset.theme?.backgroundColor) {
|
|
|
|
|
|
return undefined;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
backgroundColor: preset.theme.backgroundColor,
|
|
|
|
|
|
color: preset.theme.textColor || "#FFFFFF",
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-16 21:40:42 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="space-y-3">
|
2025-10-24 13:02:35 +08:00
|
|
|
|
<FormLabel>{t("providerPreset.label")}</FormLabel>
|
2025-10-16 21:40:42 +08:00
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
|
|
|
|
{/* 自定义按钮 */}
|
|
|
|
|
|
<button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => onPresetChange("custom")}
|
|
|
|
|
|
className={`inline-flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
|
|
|
|
|
|
selectedPresetId === "custom"
|
2025-10-19 23:11:48 +08:00
|
|
|
|
? "bg-blue-500 text-white dark:bg-blue-600"
|
2025-10-16 21:40:42 +08:00
|
|
|
|
: "bg-gray-100 dark:bg-gray-800 text-gray-500 dark:text-gray-400 hover:bg-gray-200 dark:hover:bg-gray-700"
|
|
|
|
|
|
}`}
|
|
|
|
|
|
>
|
2025-10-19 11:55:46 +08:00
|
|
|
|
{t("providerPreset.custom")}
|
2025-10-16 21:40:42 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 预设按钮 */}
|
|
|
|
|
|
{categoryKeys.map((category) => {
|
|
|
|
|
|
const entries = groupedPresets[category];
|
|
|
|
|
|
if (!entries || entries.length === 0) return null;
|
2025-10-19 23:11:48 +08:00
|
|
|
|
return entries.map((entry) => {
|
|
|
|
|
|
const isSelected = selectedPresetId === entry.id;
|
2025-11-06 15:22:38 +08:00
|
|
|
|
const isPartner = entry.preset.isPartner;
|
2025-10-19 23:11:48 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<button
|
|
|
|
|
|
key={entry.id}
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
onClick={() => onPresetChange(entry.id)}
|
2025-11-06 15:22:38 +08:00
|
|
|
|
className={`${getPresetButtonClass(isSelected, entry.preset)} relative`}
|
2025-10-19 23:11:48 +08:00
|
|
|
|
style={getPresetButtonStyle(isSelected, entry.preset)}
|
|
|
|
|
|
title={
|
|
|
|
|
|
presetCategoryLabels[category] ??
|
|
|
|
|
|
t("providerPreset.categoryOther", {
|
|
|
|
|
|
defaultValue: "其他",
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
|
|
|
|
|
{renderPresetIcon(entry.preset)}
|
|
|
|
|
|
{entry.preset.name}
|
2025-11-06 15:22:38 +08:00
|
|
|
|
{isPartner && (
|
|
|
|
|
|
<span className="absolute -top-1 -right-1 flex items-center gap-0.5 rounded-full bg-gradient-to-r from-amber-500 to-yellow-500 px-1.5 py-0.5 text-[10px] font-bold text-white shadow-md">
|
|
|
|
|
|
<Star className="h-2.5 w-2.5 fill-current" />
|
|
|
|
|
|
</span>
|
|
|
|
|
|
)}
|
2025-10-19 23:11:48 +08:00
|
|
|
|
</button>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2025-10-16 21:40:42 +08:00
|
|
|
|
})}
|
|
|
|
|
|
</div>
|
2025-10-24 13:02:35 +08:00
|
|
|
|
<p className="text-xs text-muted-foreground">{getCategoryHint()}</p>
|
2025-10-16 21:40:42 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|