Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | 'use client'; |
| 2 | |
| 3 | import { usePathname } from 'next/navigation'; |
| 4 | import { useState, useRef, useEffect } from 'react'; |
| 5 | import { useApiVersion } from '@/lib/use-api-version'; |
| 6 | |
| 7 | const VERSIONS = [ |
| 8 | { value: '3.1', label: 'v3.1', badge: 'Latest' }, |
| 9 | { value: '3.0', label: 'v3.0' }, |
| 10 | ] as const; |
| 11 | |
| 12 | export function NavVersionSelector() { |
| 13 | const pathname = usePathname(); |
| 14 | const isReferencePage = pathname.startsWith('/reference'); |
| 15 | const version = useApiVersion(); |
| 16 | const [open, setOpen] = useState(false); |
| 17 | const ref = useRef<HTMLDivElement>(null); |
| 18 | |
| 19 | useEffect(() => { |
| 20 | const handler = (e: MouseEvent) => { |
| 21 | if (ref.current && !ref.current.contains(e.target as Node)) { |
| 22 | setOpen(false); |
| 23 | } |
| 24 | }; |
| 25 | document.addEventListener('mousedown', handler); |
| 26 | return () => document.removeEventListener('mousedown', handler); |
| 27 | }, []); |
| 28 | |
| 29 | if (!isReferencePage) return null; |
| 30 | |
| 31 | const navigate = (newVersion: string) => { |
| 32 | setOpen(false); |
| 33 | if (newVersion === version) return; |
| 34 | const path = window.location.pathname; |
| 35 | |
| 36 | if (newVersion === '3.0') { |
| 37 | // Shared pages — go to v3 overview |
| 38 | if (path.includes('/sdk-reference') || path === '/reference') { |
| 39 | window.location.href = '/reference/v3'; |
| 40 | } else { |
| 41 | window.location.href = path.replace('/reference/', '/reference/v3/'); |
| 42 | } |
| 43 | } else if (newVersion === '3.1') { |
| 44 | if (path === '/reference/v3') { |
| 45 | window.location.href = '/reference'; |
| 46 | } else { |
| 47 | window.location.href = path.replace('/reference/v3/', '/reference/'); |
| 48 | } |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | const current = VERSIONS.find((v) => v.value === version)!; |
| 53 | |
| 54 | return ( |
| 55 | <div ref={ref} className="relative"> |
| 56 | <button |
| 57 | onClick={(e) => { |
| 58 | e.preventDefault(); |
| 59 | e.stopPropagation(); |
| 60 | setOpen(!open); |
| 61 | }} |
| 62 | onKeyDown={(e) => { if (e.key === 'Escape') setOpen(false); }} |
| 63 | aria-haspopup="listbox" |
| 64 | aria-expanded={open} |
| 65 | aria-label={`API version: ${current.label}. Switch version`} |
| 66 | className="inline-flex items-center gap-1 rounded-md border px-1.5 py-0.5 text-xs font-medium transition-colors hover:bg-fd-accent/50" |
| 67 | style={{ |
| 68 | borderColor: 'color-mix(in srgb, var(--composio-orange, #f97316) 40%, transparent)', |
| 69 | color: 'var(--composio-orange, #f97316)', |
| 70 | }} |
| 71 | > |
| 72 | {current.label} |
| 73 | <svg |
| 74 | aria-hidden="true" |
| 75 | width="10" |
| 76 | height="10" |
| 77 | viewBox="0 0 24 24" |
| 78 | fill="none" |
| 79 | stroke="currentColor" |
| 80 | strokeWidth="2.5" |
| 81 | strokeLinecap="round" |
| 82 | strokeLinejoin="round" |
| 83 | className={`transition-transform ${open ? 'rotate-180' : ''}`} |
| 84 | > |
| 85 | <path d="m6 9 6 6 6-6" /> |
| 86 | </svg> |
| 87 | </button> |
| 88 | |
| 89 | {open && ( |
| 90 | <div |
| 91 | role="listbox" |
| 92 | aria-label="API version" |
| 93 | className="absolute left-0 top-full z-50 mt-1.5 min-w-[120px] overflow-hidden rounded-lg border border-fd-border bg-fd-popover p-1 shadow-lg" |
| 94 | onClick={(e) => e.stopPropagation()} |
| 95 | > |
| 96 | {VERSIONS.map((v) => ( |
| 97 | <button |
| 98 | key={v.value} |
| 99 | role="option" |
| 100 | aria-selected={v.value === version} |
| 101 | onClick={(e) => { |
| 102 | e.preventDefault(); |
| 103 | e.stopPropagation(); |
| 104 | navigate(v.value); |
| 105 | }} |
| 106 | className={`flex w-full items-center gap-2 rounded-md px-2.5 py-1.5 text-xs transition-colors ${ |
| 107 | v.value === version |
| 108 | ? 'bg-fd-accent text-fd-accent-foreground font-medium' |
| 109 | : 'text-fd-muted-foreground hover:bg-fd-accent/50 hover:text-fd-foreground' |
| 110 | }`} |
| 111 | > |
| 112 | <span>{v.label}</span> |
| 113 | {'badge' in v && v.badge && ( |
| 114 | <span |
| 115 | className="rounded px-1 py-0.5 text-[10px] font-medium leading-none" |
| 116 | style={{ |
| 117 | backgroundColor: 'color-mix(in srgb, var(--composio-orange, #f97316) 15%, transparent)', |
| 118 | color: 'var(--composio-orange, #f97316)', |
| 119 | }} |
| 120 | > |
| 121 | {v.badge} |
| 122 | </span> |
| 123 | )} |
| 124 | {v.value === version && ( |
| 125 | <svg |
| 126 | aria-hidden="true" |
| 127 | width="12" |
| 128 | height="12" |
| 129 | viewBox="0 0 24 24" |
| 130 | fill="none" |
| 131 | stroke="currentColor" |
| 132 | strokeWidth="3" |
| 133 | strokeLinecap="round" |
| 134 | strokeLinejoin="round" |
| 135 | className="ml-auto" |
| 136 | > |
| 137 | <path d="M20 6 9 17l-5-5" /> |
| 138 | </svg> |
| 139 | )} |
| 140 | </button> |
| 141 | ))} |
| 142 | </div> |
| 143 | )} |
| 144 | </div> |
| 145 | ); |
| 146 | } |