Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 251 lines 8.27 KB
Raw
1 'use client';
2
3 import Image from 'next/image';
4 import {
5 createContext,
6 useContext,
7 useState,
8 useEffect,
9 useRef,
10 type ReactNode,
11 } from 'react';
12
13 interface ClientData {
14 id: string;
15 name: string;
16 description: string;
17 icon?: string;
18 iconDark?: string;
19 category: 'popular' | 'ide' | 'other';
20 }
21
22 interface ConnectContextValue {
23 selectedId: string;
24 setSelectedId: (id: string) => void;
25 registerClient: (data: ClientData) => void;
26 clients: ClientData[];
27 }
28
29 const ConnectContext = createContext<ConnectContextValue | null>(null);
30
31 function ClientIcon({ icon, iconDark, name, size = 16 }: { icon?: string; iconDark?: string; name: string; size?: number }) {
32 if (!icon) return null;
33
34 if (iconDark) {
35 return (
36 <>
37 <Image src={icon} alt={`${name} logo`} width={size} height={size} className="h-4 w-4 shrink-0 dark:hidden" />
38 <Image src={iconDark} alt={`${name} logo`} width={size} height={size} className="h-4 w-4 shrink-0 hidden dark:block" />
39 </>
40 );
41 }
42
43 return <Image src={icon} alt={`${name} logo`} width={size} height={size} className="h-4 w-4 shrink-0" />;
44 }
45
46 function PopularTab({
47 client,
48 selected,
49 onSelect,
50 }: {
51 client: ClientData;
52 selected: boolean;
53 onSelect: () => void;
54 }) {
55 return (
56 <button
57 type="button"
58 onClick={onSelect}
59 className={`
60 flex items-center gap-2 rounded-md px-3 py-1.5 text-sm font-medium transition-all whitespace-nowrap
61 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500
62 ${selected
63 ? 'bg-fd-background text-fd-foreground shadow-sm dark:bg-fd-accent/60 dark:shadow-none'
64 : 'text-fd-muted-foreground hover:text-fd-foreground'
65 }
66 `}
67 >
68 <ClientIcon icon={client.icon} iconDark={client.iconDark} name={client.name} />
69 {client.name}
70 </button>
71 );
72 }
73
74 interface ConnectFlowProps {
75 children: ReactNode;
76 }
77
78 export function ConnectFlow({ children }: ConnectFlowProps) {
79 const [clients, setClients] = useState<ClientData[]>([]);
80 const [selectedId, setSelectedId] = useState<string>('');
81 const registeredIds = useRef<Set<string>>(new Set());
82 const [dropdownOpen, setDropdownOpen] = useState(false);
83 const dropdownRef = useRef<HTMLDivElement>(null);
84
85 const registerClient = (data: ClientData) => {
86 if (!registeredIds.current.has(data.id)) {
87 registeredIds.current.add(data.id);
88 setClients((prev) => {
89 if (prev.some((c) => c.id === data.id)) return prev;
90 return [...prev, data];
91 });
92 }
93 };
94
95 useEffect(() => {
96 if (clients.length > 0 && !selectedId) {
97 setSelectedId(clients[0].id);
98 }
99 }, [clients, selectedId]);
100
101 // Close dropdown on outside click
102 useEffect(() => {
103 function handleClick(e: MouseEvent) {
104 if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
105 setDropdownOpen(false);
106 }
107 }
108 if (dropdownOpen) {
109 document.addEventListener('mousedown', handleClick);
110 return () => document.removeEventListener('mousedown', handleClick);
111 }
112 }, [dropdownOpen]);
113
114 const contextValue: ConnectContextValue = {
115 selectedId,
116 setSelectedId,
117 registerClient,
118 clients,
119 };
120
121 const popular = clients.filter((c) => c.category === 'popular');
122 const others = clients.filter((c) => c.category !== 'popular');
123 const selectedIsOther = others.some((c) => c.id === selectedId);
124 const selectedOtherClient = others.find((c) => c.id === selectedId);
125
126 return (
127 <ConnectContext.Provider value={contextValue}>
128 {clients.length > 0 && (
129 <div className="not-prose mb-8 rounded-xl border border-fd-border bg-fd-card/50">
130 <div className="flex items-center justify-between gap-3 p-3">
131 {/* Popular tabs */}
132 <div className={`flex items-center gap-1 overflow-x-auto rounded-lg p-1 transition-all ${selectedIsOther ? 'bg-fd-muted/30 opacity-40' : 'bg-fd-muted/50 dark:bg-fd-muted/30'}`}>
133 {popular.map((client) => (
134 <PopularTab
135 key={client.id}
136 client={client}
137 selected={selectedId === client.id}
138 onSelect={() => {
139 setSelectedId(client.id);
140 setDropdownOpen(false);
141 }}
142 />
143 ))}
144 </div>
145
146 {/* More clients dropdown */}
147 {others.length > 0 && (
148 <div className="relative shrink-0" ref={dropdownRef}>
149 <button
150 type="button"
151 onClick={() => setDropdownOpen(!dropdownOpen)}
152 aria-expanded={dropdownOpen}
153 aria-haspopup="menu"
154 onKeyDown={(e) => { if (e.key === 'Escape') setDropdownOpen(false); }}
155 className={`
156 flex items-center gap-2 rounded-lg border px-3 py-1.5 text-sm font-medium transition-all
157 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500
158 ${selectedIsOther
159 ? 'border-[var(--composio-orange)] bg-fd-accent/30 text-fd-foreground'
160 : 'border-fd-border bg-fd-card text-fd-muted-foreground hover:text-fd-foreground hover:bg-fd-accent/50'
161 }
162 `}
163 >
164 {selectedIsOther && selectedOtherClient && (
165 <ClientIcon icon={selectedOtherClient.icon} iconDark={selectedOtherClient.iconDark} name={selectedOtherClient.name} />
166 )}
167 {selectedIsOther ? selectedOtherClient?.name : 'More clients'}
168 <svg width="12" height="12" viewBox="0 0 12 12" fill="none" className="shrink-0 opacity-60">
169 <path d="M3 4.5L6 7.5L9 4.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
170 </svg>
171 </button>
172
173 {dropdownOpen && (
174 <div role="menu" className="absolute right-0 top-full z-50 mt-1 min-w-[220px] rounded-lg border border-fd-border bg-fd-card p-1 shadow-lg">
175 {others.map((client) => (
176 <button
177 key={client.id}
178 type="button"
179 role="menuitem"
180 onClick={() => {
181 setSelectedId(client.id);
182 setDropdownOpen(false);
183 }}
184 className={`
185 flex w-full items-center gap-3 rounded-md px-3 py-2 text-left text-sm transition-colors
186 ${selectedId === client.id
187 ? 'bg-fd-accent/50 text-fd-foreground'
188 : 'text-fd-muted-foreground hover:bg-fd-accent/30 hover:text-fd-foreground'
189 }
190 `}
191 >
192 <ClientIcon icon={client.icon} iconDark={client.iconDark} name={client.name} />
193 <div>
194 <span className="block font-medium">{client.name}</span>
195 <span className="block text-xs text-fd-muted-foreground">{client.description}</span>
196 </div>
197 </button>
198 ))}
199 </div>
200 )}
201 </div>
202 )}
203 </div>
204 </div>
205 )}
206
207 {children}
208 </ConnectContext.Provider>
209 );
210 }
211
212 interface ConnectClientOptionProps {
213 id: string;
214 name: string;
215 description: string;
216 icon?: string;
217 iconDark?: string;
218 category?: 'popular' | 'ide' | 'other';
219 children: ReactNode;
220 }
221
222 export function ConnectClientOption({
223 id,
224 name,
225 description,
226 icon,
227 iconDark,
228 category = 'other',
229 children,
230 }: ConnectClientOptionProps) {
231 const context = useContext(ConnectContext);
232 const hasRegistered = useRef(false);
233
234 useEffect(() => {
235 if (context && !hasRegistered.current) {
236 context.registerClient({ id, name, description, icon, iconDark, category });
237 hasRegistered.current = true;
238 }
239 }, [context, id, name, description, icon, iconDark, category]);
240
241 if (!context || context.selectedId !== id) {
242 return null;
243 }
244
245 return (
246 <>
247 <h2 className="text-2xl font-semibold tracking-tight mt-8 mb-4">{name}</h2>
248 {children}
249 </>
250 );
251 }