"use client"; import * as React from "react"; import Link from "next/link"; import type { FetchRequestInput } from "@fetcha/core/client"; import { KeyRound } from "lucide-react"; import { CodeBlock } from "@/components/ui/code-block"; import { CopyButton } from "@/components/ui/copy-button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { CODEGEN_LANGS, codegenInfo, generateCode, type CodegenLang } from "@/lib/codegen"; import { cn } from "@/lib/utils"; import type { PlaygroundKey } from "./types"; export function keyPlaceholder(k: PlaygroundKey | undefined): string { if (!k) return "YOUR_API_KEY"; const prefix = k.keyPrefix.endsWith("_") ? k.keyPrefix : `${k.keyPrefix}_`; return `${prefix}••••${k.last4}`; } /** Language tabs + API key selector + generated snippet for the current request. */ export function CodePanel({ request, keys, baseUrl, lang, onLangChange, keyId, onKeyChange, className }: { request: FetchRequestInput; keys: PlaygroundKey[]; baseUrl: string; lang: CodegenLang; onLangChange: (l: CodegenLang) => void; keyId: string; onKeyChange: (id: string) => void; className?: string }) { const key = keys.find((k) => k.id === keyId) ?? keys[0]; const info = codegenInfo(lang); const code = React.useMemo(() => generateCode(lang, request, { apiKeyPlaceholder: keyPlaceholder(key), baseUrl }), [lang, request, key, baseUrl]); return (
{keys.length ? ( <> {keyPlaceholder(key)} is a placeholder for {key?.name}. Replace it with your key — keys are only shown once, at creation.{" "} > ) : ( <> This project has no API key yet.{" "} Create one {" "} and replace YOUR_API_KEY.{" "} > )} {lang === "typescript" || lang === "python-sdk" ? "SDKs are not published yet: install from source (repo packages/sdk and sdk-python)." : null}