2025-10-16 19:56:00 +08:00
|
|
|
import { useMemo } from "react";
|
2025-10-30 14:59:15 +08:00
|
|
|
import type { AppId } from "@/lib/api";
|
2025-10-16 19:56:00 +08:00
|
|
|
import type { ProviderCategory } from "@/types";
|
2025-11-02 21:05:48 +08:00
|
|
|
import type { ProviderPreset } from "@/config/claudeProviderPresets";
|
2025-10-16 19:56:00 +08:00
|
|
|
import type { CodexProviderPreset } from "@/config/codexProviderPresets";
|
|
|
|
|
|
|
|
|
|
type PresetEntry = {
|
|
|
|
|
id: string;
|
|
|
|
|
preset: ProviderPreset | CodexProviderPreset;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface UseApiKeyLinkProps {
|
2025-10-30 14:59:15 +08:00
|
|
|
appId: AppId;
|
2025-10-16 19:56:00 +08:00
|
|
|
category?: ProviderCategory;
|
|
|
|
|
selectedPresetId: string | null;
|
|
|
|
|
presetEntries: PresetEntry[];
|
|
|
|
|
formWebsiteUrl: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 管理 API Key 获取链接的显示和 URL
|
|
|
|
|
*/
|
|
|
|
|
export function useApiKeyLink({
|
2025-10-30 14:59:15 +08:00
|
|
|
appId,
|
2025-10-16 19:56:00 +08:00
|
|
|
category,
|
|
|
|
|
selectedPresetId,
|
|
|
|
|
presetEntries,
|
|
|
|
|
formWebsiteUrl,
|
|
|
|
|
}: UseApiKeyLinkProps) {
|
|
|
|
|
// 判断是否显示 API Key 获取链接
|
|
|
|
|
const shouldShowApiKeyLink = useMemo(() => {
|
|
|
|
|
return (
|
|
|
|
|
category !== "official" &&
|
|
|
|
|
(category === "cn_official" ||
|
|
|
|
|
category === "aggregator" ||
|
|
|
|
|
category === "third_party")
|
|
|
|
|
);
|
|
|
|
|
}, [category]);
|
|
|
|
|
|
|
|
|
|
// 获取当前供应商的网址(用于 API Key 链接)
|
|
|
|
|
const getWebsiteUrl = useMemo(() => {
|
|
|
|
|
if (selectedPresetId && selectedPresetId !== "custom") {
|
|
|
|
|
const entry = presetEntries.find((item) => item.id === selectedPresetId);
|
|
|
|
|
if (entry) {
|
|
|
|
|
const preset = entry.preset;
|
|
|
|
|
// 第三方供应商优先使用 apiKeyUrl
|
|
|
|
|
return preset.category === "third_party"
|
|
|
|
|
? preset.apiKeyUrl || preset.websiteUrl || ""
|
|
|
|
|
: preset.websiteUrl || "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return formWebsiteUrl || "";
|
|
|
|
|
}, [selectedPresetId, presetEntries, formWebsiteUrl]);
|
|
|
|
|
|
|
|
|
|
return {
|
2025-10-30 15:31:08 +08:00
|
|
|
shouldShowApiKeyLink:
|
|
|
|
|
appId === "claude"
|
|
|
|
|
? shouldShowApiKeyLink
|
|
|
|
|
: appId === "codex"
|
|
|
|
|
? shouldShowApiKeyLink
|
|
|
|
|
: false,
|
2025-10-16 19:56:00 +08:00
|
|
|
websiteUrl: getWebsiteUrl,
|
|
|
|
|
};
|
|
|
|
|
}
|