mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-05-03 00:05:55 +00:00
38 lines
819 B
TypeScript
38 lines
819 B
TypeScript
import { createContext, useContext, type ReactNode } from "react";
|
|
|
|
import type { NanobotClient } from "@/lib/nanobot-client";
|
|
|
|
interface ClientContextValue {
|
|
client: NanobotClient;
|
|
token: string;
|
|
modelName: string | null;
|
|
}
|
|
|
|
const ClientContext = createContext<ClientContextValue | null>(null);
|
|
|
|
export function ClientProvider({
|
|
client,
|
|
token,
|
|
modelName = null,
|
|
children,
|
|
}: {
|
|
client: NanobotClient;
|
|
token: string;
|
|
modelName?: string | null;
|
|
children: ReactNode;
|
|
}) {
|
|
return (
|
|
<ClientContext.Provider value={{ client, token, modelName }}>
|
|
{children}
|
|
</ClientContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useClient(): ClientContextValue {
|
|
const ctx = useContext(ClientContext);
|
|
if (!ctx) {
|
|
throw new Error("useClient must be used within a ClientProvider");
|
|
}
|
|
return ctx;
|
|
}
|