"use client"; import Link from "next/link"; import { useTranslations } from "next-intl"; import { useEffect, useMemo, useState } from "react"; import { useCopyToClipboard } from "@/shared/hooks/useCopyToClipboard"; import { useDisplayBaseUrl } from "@/shared/hooks"; import { Card } from "@/shared/components"; type EndpointApiKeySummary = { id: string; key: string; rawKey?: string; isActive?: boolean; }; export default function VscodeTokenAliasCard({ className = "", variant = "highlight", }: Readonly<{ className?: string; variant?: "highlight" | "catalog" }>) { const t = useTranslations("endpoint"); const [cliApiKeys, setCliApiKeys] = useState([]); const [keysLoading, setKeysLoading] = useState(true); const [keysError, setKeysError] = useState(false); const { copied, copy } = useCopyToClipboard(); const displayBaseUrl = useDisplayBaseUrl(); useEffect(() => { let cancelled = false; const loadCliApiKeys = async () => { setKeysLoading(true); setKeysError(false); try { const res = await fetch("/api/cli-tools/keys"); if (!res.ok) { throw new Error("Failed to load CLI keys"); } const data = await res.json(); if (!cancelled) { setCliApiKeys(data.keys || []); } } catch { if (!cancelled) { setCliApiKeys([]); setKeysError(true); } } finally { if (!cancelled) { setKeysLoading(false); } } }; void loadCliApiKeys(); return () => { cancelled = true; }; }, []); const preferredCliApiKey = useMemo(() => { if (cliApiKeys.length === 0) { return null; } const storedCopilotKeyId = typeof window !== "undefined" ? window.localStorage.getItem("omniroute-cli-key-copilot") : null; return ( (storedCopilotKeyId ? cliApiKeys.find((key) => key.id === storedCopilotKeyId) : null) ?? cliApiKeys.find((key) => key.isActive !== false) ?? cliApiKeys[0] ?? null ); }, [cliApiKeys]); const tokenSegment = preferredCliApiKey?.rawKey || "{token}"; const hasRealToken = Boolean(preferredCliApiKey?.rawKey); const translationToken = { token: "{token}" }; const baseUrl = `${displayBaseUrl}/api/v1/vscode/${tokenSegment}`; const vscodeTokenizedUrls = [ { label: t("vscodeAliasBaseLabel"), url: `${baseUrl}/`, key: "vscode_token_base", }, { label: t("vscodeAliasModelsLabel"), url: `${baseUrl}/models`, key: "vscode_token_models", }, { label: t("vscodeAliasChatLabel"), url: `${baseUrl}/chat/completions`, key: "vscode_token_chat", }, ]; const description = hasRealToken ? t("vscodeAliasDescriptionReady", translationToken) : keysError ? t("vscodeAliasDescriptionError") : keysLoading ? t("vscodeAliasDescriptionLoading") : t("vscodeAliasDescriptionPlaceholder", translationToken); if (variant === "catalog") { return (
key

{t("vscodeAliasTitle")}

{t("vscodeAliasManage")}

{description}

{vscodeTokenizedUrls.map(({ label, url, key }) => ( void copy(text, copyKey)} copied={copied} variant="catalog" /> ))}
); } return (

{t("vscodeAliasTitle")}

{description}

{t("vscodeAliasManage")}
{vscodeTokenizedUrls.map(({ label, url, key }) => ( void copy(text, copyKey)} copied={copied} variant="highlight" /> ))}
); } function CopyableEndpointRow({ label, url, copyKey, copy, copied, variant = "highlight", }: Readonly<{ label: string; url: string; copyKey: string; copy: (text: string, key?: string) => void; copied?: string | null; variant?: "highlight" | "catalog"; }>) { const rowClassName = variant === "catalog" ? "flex items-center gap-2 min-w-0 rounded-lg border border-black/10 dark:border-white/10 bg-black/[0.02] dark:bg-white/[0.02] px-3 py-2.5" : "flex items-center gap-2 min-w-0 rounded-md border border-sky-500/15 bg-background/60 px-2.5 py-2"; return (
{label} {url}
); }