"use client"; import * as React from "react"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { CodeBlock, type CodeLang } from "@/components/ui/code-block"; import { cn } from "@/lib/utils"; export interface CodeTab { /** Tab label, e.g. "cURL". Also used as the tab value. */ label: string; lang: CodeLang; code: string; title?: string; } const STORAGE_KEY = "fetcha-docs-lang"; const EVENT = "fetcha-docs-lang-change"; /** * Language switcher for code samples. The selected language is remembered in localStorage * and synchronised across every CodeTabs instance on the page. */ export function CodeTabs({ tabs, className, maxHeight, lineNumbers }: { tabs: CodeTab[]; className?: string; maxHeight?: number | string; lineNumbers?: boolean }) { const labels = React.useMemo(() => tabs.map((t) => t.label), [tabs]); const [value, setValue] = React.useState(labels[0] ?? ""); React.useEffect(() => { const apply = (stored: string | null) => { if (stored && labels.includes(stored)) setValue(stored); }; try { apply(window.localStorage.getItem(STORAGE_KEY)); } catch {} const onChange = (e: Event) => apply((e as CustomEvent).detail); window.addEventListener(EVENT, onChange); return () => window.removeEventListener(EVENT, onChange); }, [labels]); const onValueChange = (v: string) => { setValue(v); try { window.localStorage.setItem(STORAGE_KEY, v); } catch {} window.dispatchEvent(new CustomEvent(EVENT, { detail: v })); }; if (!tabs.length) return null; return ( {tabs.map((t) => ( {t.label} ))} {tabs.map((t) => ( ))} ); }