'use client'; import { usePathname } from 'next/navigation'; import { useState, useRef, useEffect } from 'react'; import { useApiVersion } from '@/lib/use-api-version'; const VERSIONS = [ { value: '3.1', label: 'v3.1', badge: 'Latest' }, { value: '3.0', label: 'v3.0' }, ] as const; export function NavVersionSelector() { const pathname = usePathname(); const isReferencePage = pathname.startsWith('/reference'); const version = useApiVersion(); const [open, setOpen] = useState(false); const ref = useRef(null); useEffect(() => { const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, []); if (!isReferencePage) return null; const navigate = (newVersion: string) => { setOpen(false); if (newVersion === version) return; const path = window.location.pathname; if (newVersion === '3.0') { // Shared pages — go to v3 overview if (path.includes('/sdk-reference') || path === '/reference') { window.location.href = '/reference/v3'; } else { window.location.href = path.replace('/reference/', '/reference/v3/'); } } else if (newVersion === '3.1') { if (path === '/reference/v3') { window.location.href = '/reference'; } else { window.location.href = path.replace('/reference/v3/', '/reference/'); } } }; const current = VERSIONS.find((v) => v.value === version)!; return (
{open && (
e.stopPropagation()} > {VERSIONS.map((v) => ( ))}
)}
); }