Files
CC-Switch/src/components/providers/forms/hooks/useApiKeyLink.ts
T
Jason b5b45c2703 chore: bump version to 3.10.3
- Update version in package.json, Cargo.toml, and tauri.conf.json
- Add missing changelog entries for OpenCode API key link and AICodeMirror preset
- Fix Prettier formatting for new hook files
2026-01-31 17:29:09 +08:00

93 lines
2.6 KiB
TypeScript

import { useMemo } from "react";
import type { AppId } from "@/lib/api";
import type { ProviderCategory } from "@/types";
import type { ProviderPreset } from "@/config/claudeProviderPresets";
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
import type { GeminiProviderPreset } from "@/config/geminiProviderPresets";
import type { OpenCodeProviderPreset } from "@/config/opencodeProviderPresets";
type PresetEntry = {
id: string;
preset:
| ProviderPreset
| CodexProviderPreset
| GeminiProviderPreset
| OpenCodeProviderPreset;
};
interface UseApiKeyLinkProps {
appId: AppId;
category?: ProviderCategory;
selectedPresetId: string | null;
presetEntries: PresetEntry[];
formWebsiteUrl: string;
}
/**
* 管理 API Key 获取链接的显示和 URL
*/
export function useApiKeyLink({
appId,
category,
selectedPresetId,
presetEntries,
formWebsiteUrl,
}: UseApiKeyLinkProps) {
// 判断是否显示 API Key 获取链接
const shouldShowApiKeyLink = useMemo(() => {
return (
category !== "official" &&
(category === "cn_official" ||
category === "aggregator" ||
category === "third_party")
);
}, [category]);
// 获取当前预设条目
const currentPresetEntry = useMemo(() => {
if (selectedPresetId && selectedPresetId !== "custom") {
return presetEntries.find((item) => item.id === selectedPresetId);
}
return undefined;
}, [selectedPresetId, presetEntries]);
// 获取当前供应商的网址(用于 API Key 链接)
const getWebsiteUrl = useMemo(() => {
if (currentPresetEntry) {
const preset = currentPresetEntry.preset;
// 对于 cn_official、aggregator、third_party,优先使用 apiKeyUrl(可能包含推广参数)
if (
preset.category === "cn_official" ||
preset.category === "aggregator" ||
preset.category === "third_party"
) {
return preset.apiKeyUrl || preset.websiteUrl || "";
}
return preset.websiteUrl || "";
}
return formWebsiteUrl || "";
}, [currentPresetEntry, formWebsiteUrl]);
// 提取合作伙伴信息
const isPartner = useMemo(() => {
return currentPresetEntry?.preset.isPartner ?? false;
}, [currentPresetEntry]);
const partnerPromotionKey = useMemo(() => {
return currentPresetEntry?.preset.partnerPromotionKey;
}, [currentPresetEntry]);
return {
shouldShowApiKeyLink:
appId === "claude" ||
appId === "codex" ||
appId === "gemini" ||
appId === "opencode"
? shouldShowApiKeyLink
: false,
websiteUrl: getWebsiteUrl,
isPartner,
partnerPromotionKey,
};
}