'use client'; import Image from 'next/image'; import { createContext, useContext, useState, useEffect, useRef, type ReactNode, } from 'react'; interface ClientData { id: string; name: string; description: string; icon?: string; iconDark?: string; category: 'popular' | 'ide' | 'other'; } interface ConnectContextValue { selectedId: string; setSelectedId: (id: string) => void; registerClient: (data: ClientData) => void; clients: ClientData[]; } const ConnectContext = createContext(null); function ClientIcon({ icon, iconDark, name, size = 16 }: { icon?: string; iconDark?: string; name: string; size?: number }) { if (!icon) return null; if (iconDark) { return ( <> {`${name} {`${name} ); } return {`${name}; } function PopularTab({ client, selected, onSelect, }: { client: ClientData; selected: boolean; onSelect: () => void; }) { return ( ); } interface ConnectFlowProps { children: ReactNode; } export function ConnectFlow({ children }: ConnectFlowProps) { const [clients, setClients] = useState([]); const [selectedId, setSelectedId] = useState(''); const registeredIds = useRef>(new Set()); const [dropdownOpen, setDropdownOpen] = useState(false); const dropdownRef = useRef(null); const registerClient = (data: ClientData) => { if (!registeredIds.current.has(data.id)) { registeredIds.current.add(data.id); setClients((prev) => { if (prev.some((c) => c.id === data.id)) return prev; return [...prev, data]; }); } }; useEffect(() => { if (clients.length > 0 && !selectedId) { setSelectedId(clients[0].id); } }, [clients, selectedId]); // Close dropdown on outside click useEffect(() => { function handleClick(e: MouseEvent) { if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) { setDropdownOpen(false); } } if (dropdownOpen) { document.addEventListener('mousedown', handleClick); return () => document.removeEventListener('mousedown', handleClick); } }, [dropdownOpen]); const contextValue: ConnectContextValue = { selectedId, setSelectedId, registerClient, clients, }; const popular = clients.filter((c) => c.category === 'popular'); const others = clients.filter((c) => c.category !== 'popular'); const selectedIsOther = others.some((c) => c.id === selectedId); const selectedOtherClient = others.find((c) => c.id === selectedId); return ( {clients.length > 0 && (
{/* Popular tabs */}
{popular.map((client) => ( { setSelectedId(client.id); setDropdownOpen(false); }} /> ))}
{/* More clients dropdown */} {others.length > 0 && (
{dropdownOpen && (
{others.map((client) => ( ))}
)}
)}
)} {children}
); } interface ConnectClientOptionProps { id: string; name: string; description: string; icon?: string; iconDark?: string; category?: 'popular' | 'ide' | 'other'; children: ReactNode; } export function ConnectClientOption({ id, name, description, icon, iconDark, category = 'other', children, }: ConnectClientOptionProps) { const context = useContext(ConnectContext); const hasRegistered = useRef(false); useEffect(() => { if (context && !hasRegistered.current) { context.registerClient({ id, name, description, icon, iconDark, category }); hasRegistered.current = true; } }, [context, id, name, description, icon, iconDark, category]); if (!context || context.selectedId !== id) { return null; } return ( <>

{name}

{children} ); }