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 Image from 'next/image'; |
| 4 | import { |
| 5 | createContext, |
| 6 | useContext, |
| 7 | useState, |
| 8 | useEffect, |
| 9 | useRef, |
| 10 | type ReactNode, |
| 11 | } from 'react'; |
| 12 | import { Wrench } from 'lucide-react'; |
| 13 | |
| 14 | // Types |
| 15 | interface ToolTypeData { |
| 16 | id: string; |
| 17 | name: string; |
| 18 | icon: 'native' | 'mcp'; |
| 19 | } |
| 20 | |
| 21 | interface ToolTypeContextValue { |
| 22 | selectedId: string; |
| 23 | setSelectedId: (id: string) => void; |
| 24 | registerToolType: (data: ToolTypeData) => void; |
| 25 | toolTypes: ToolTypeData[]; |
| 26 | } |
| 27 | |
| 28 | // Context to share state between ToolTypeFlow and ToolTypeOption |
| 29 | const ToolTypeContext = createContext<ToolTypeContextValue | null>(null); |
| 30 | |
| 31 | export function useToolType() { |
| 32 | const context = useContext(ToolTypeContext); |
| 33 | return context?.selectedId ?? null; |
| 34 | } |
| 35 | |
| 36 | // Tool type option component - registers itself and conditionally renders content |
| 37 | interface ToolTypeOptionProps { |
| 38 | id: string; |
| 39 | name: string; |
| 40 | icon: 'native' | 'mcp'; |
| 41 | children: ReactNode; |
| 42 | } |
| 43 | |
| 44 | export function ToolTypeOption({ id, name, icon, children }: ToolTypeOptionProps) { |
| 45 | const context = useContext(ToolTypeContext); |
| 46 | const hasRegistered = useRef(false); |
| 47 | |
| 48 | useEffect(() => { |
| 49 | if (context && !hasRegistered.current) { |
| 50 | context.registerToolType({ id, name, icon }); |
| 51 | hasRegistered.current = true; |
| 52 | } |
| 53 | }, [context, id, name, icon]); |
| 54 | |
| 55 | // Only render if this tool type is selected |
| 56 | if (!context || context.selectedId !== id) { |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | return <>{children}</>; |
| 61 | } |
| 62 | |
| 63 | // Card component for tool type selection |
| 64 | interface ToolTypeCardProps { |
| 65 | id: string; |
| 66 | name: string; |
| 67 | icon: 'native' | 'mcp'; |
| 68 | selected: boolean; |
| 69 | onSelect: () => void; |
| 70 | } |
| 71 | |
| 72 | function ToolTypeCard({ name, icon, selected, onSelect }: ToolTypeCardProps) { |
| 73 | return ( |
| 74 | <button |
| 75 | type="button" |
| 76 | onClick={onSelect} |
| 77 | className={` |
| 78 | flex items-center gap-3 rounded-lg border px-4 py-3 text-left transition-all |
| 79 | focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2 |
| 80 | ${selected |
| 81 | ? 'border-[var(--composio-orange)] bg-fd-accent/30' |
| 82 | : 'border-fd-border bg-fd-card hover:bg-fd-accent/50 hover:border-[color-mix(in_srgb,var(--composio-orange)_50%,transparent)]' |
| 83 | } |
| 84 | `} |
| 85 | > |
| 86 | <div className="flex h-6 w-6 shrink-0 items-center justify-center"> |
| 87 | {icon === 'native' ? ( |
| 88 | <Wrench |
| 89 | size={20} |
| 90 | className={selected ? 'text-fd-foreground' : 'text-fd-muted-foreground'} |
| 91 | /> |
| 92 | ) : ( |
| 93 | <> |
| 94 | <Image |
| 95 | src="/images/mcp-logo.svg" |
| 96 | alt="MCP logo" |
| 97 | width={20} |
| 98 | height={20} |
| 99 | className="h-5 w-5 dark:hidden" |
| 100 | /> |
| 101 | <Image |
| 102 | src="/images/mcp-logo-dark.svg" |
| 103 | alt="MCP logo" |
| 104 | width={20} |
| 105 | height={20} |
| 106 | className="h-5 w-5 hidden dark:block" |
| 107 | /> |
| 108 | </> |
| 109 | )} |
| 110 | </div> |
| 111 | <span className="text-sm font-medium text-fd-foreground">{name}</span> |
| 112 | </button> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | // Main wrapper component |
| 117 | interface ToolTypeFlowProps { |
| 118 | children: ReactNode; |
| 119 | } |
| 120 | |
| 121 | export function ToolTypeFlow({ children }: ToolTypeFlowProps) { |
| 122 | const [toolTypes, setToolTypes] = useState<ToolTypeData[]>([]); |
| 123 | const [selectedId, setSelectedId] = useState<string>(''); |
| 124 | const registeredIds = useRef<Set<string>>(new Set()); |
| 125 | |
| 126 | const registerToolType = (data: ToolTypeData) => { |
| 127 | if (!registeredIds.current.has(data.id)) { |
| 128 | registeredIds.current.add(data.id); |
| 129 | setToolTypes((prev) => { |
| 130 | // Check if already exists |
| 131 | if (prev.some((t) => t.id === data.id)) { |
| 132 | return prev; |
| 133 | } |
| 134 | return [...prev, data]; |
| 135 | }); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | // Set default selection to first tool type once registered |
| 140 | useEffect(() => { |
| 141 | if (toolTypes.length > 0 && !selectedId) { |
| 142 | setSelectedId(toolTypes[0].id); |
| 143 | } |
| 144 | }, [toolTypes, selectedId]); |
| 145 | |
| 146 | const contextValue: ToolTypeContextValue = { |
| 147 | selectedId, |
| 148 | setSelectedId, |
| 149 | registerToolType, |
| 150 | toolTypes, |
| 151 | }; |
| 152 | |
| 153 | return ( |
| 154 | <ToolTypeContext.Provider value={contextValue}> |
| 155 | {/* Tool type selector */} |
| 156 | {toolTypes.length > 0 && ( |
| 157 | <div className="not-prose mb-6"> |
| 158 | <div className="flex gap-3"> |
| 159 | {toolTypes.map((toolType) => ( |
| 160 | <ToolTypeCard |
| 161 | key={toolType.id} |
| 162 | id={toolType.id} |
| 163 | name={toolType.name} |
| 164 | icon={toolType.icon} |
| 165 | selected={selectedId === toolType.id} |
| 166 | onSelect={() => setSelectedId(toolType.id)} |
| 167 | /> |
| 168 | ))} |
| 169 | </div> |
| 170 | </div> |
| 171 | )} |
| 172 | |
| 173 | {/* Render all children - ToolTypeOption will conditionally show based on selection */} |
| 174 | {children} |
| 175 | </ToolTypeContext.Provider> |
| 176 | ); |
| 177 | } |