mirror of
https://github.com/browseros-ai/BrowserOS.git
synced 2026-05-19 11:31:03 +00:00
git-subtree-dir: packages/browseros-agent git-subtree-mainline:8f148d0918git-subtree-split:90bd4be300
30 lines
711 B
TypeScript
30 lines
711 B
TypeScript
import { createContext, type FC, type ReactNode, use, useMemo } from 'react'
|
|
import { getClient, type RpcClient } from './getClient'
|
|
|
|
const RpcClientContext = createContext<Promise<RpcClient> | null>(null)
|
|
|
|
/**
|
|
* @public
|
|
*/
|
|
export const RpcClientProvider: FC<{ children: ReactNode }> = ({
|
|
children,
|
|
}) => {
|
|
const clientPromise = useMemo(() => getClient(), [])
|
|
return (
|
|
<RpcClientContext.Provider value={clientPromise}>
|
|
{children}
|
|
</RpcClientContext.Provider>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* @public
|
|
*/
|
|
export function useRpcClient(): RpcClient {
|
|
const promise = use(RpcClientContext)
|
|
if (!promise) {
|
|
throw new Error('useRpcClient must be used within RpcClientProvider')
|
|
}
|
|
return use(promise)
|
|
}
|