mirror of
https://github.com/EthanMarti/infio-copilot.git
synced 2026-01-17 08:35:10 +00:00
40 lines
845 B
TypeScript
40 lines
845 B
TypeScript
import {
|
|
PropsWithChildren,
|
|
createContext,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
} from 'react'
|
|
|
|
import { McpHub } from '../core/mcp/McpHub'
|
|
|
|
export type McpHubContextType = {
|
|
getMcpHub: () => Promise<McpHub>
|
|
}
|
|
|
|
const McpHubContext = createContext<McpHubContextType | null>(null)
|
|
|
|
export function McpHubProvider({
|
|
getMcpHub,
|
|
children,
|
|
}: PropsWithChildren<{ getMcpHub: () => Promise<McpHub> }>) {
|
|
useEffect(() => {
|
|
// start initialization of mcpHub in the background
|
|
void getMcpHub()
|
|
}, [getMcpHub])
|
|
|
|
const value = useMemo(() => {
|
|
return { getMcpHub }
|
|
}, [getMcpHub])
|
|
|
|
return <McpHubContext.Provider value={value}>{children}</McpHubContext.Provider>
|
|
}
|
|
|
|
export function useMcpHub() {
|
|
const context = useContext(McpHubContext)
|
|
if (!context) {
|
|
throw new Error('useMcpHub must be used within a McpHubProvider')
|
|
}
|
|
return context
|
|
}
|