| 1 | import { createContext, useContext, type ReactNode } from "react"; |
| 2 | |
| 3 | import type { NanobotClient } from "@/lib/nanobot-client"; |
| 4 | |
| 5 | interface ClientContextValue { |
| 6 | client: NanobotClient; |
| 7 | token: string; |
| 8 | modelName: string | null; |
| 9 | webuiUserId: string | null; |
| 10 | } |
| 11 | |
| 12 | const ClientContext = createContext<ClientContextValue | null>(null); |
| 13 | |
| 14 | export function ClientProvider({ |
| 15 | client, |
| 16 | token, |
| 17 | modelName = null, |
| 18 | webuiUserId = null, |
| 19 | children, |
| 20 | }: { |
| 21 | client: NanobotClient; |
| 22 | token: string; |
| 23 | modelName?: string | null; |
| 24 | webuiUserId?: string | null; |
| 25 | children: ReactNode; |
| 26 | }) { |
| 27 | return ( |
| 28 | <ClientContext.Provider value={{ client, token, modelName, webuiUserId }}> |
| 29 | {children} |
| 30 | </ClientContext.Provider> |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | export function useClient(): ClientContextValue { |
| 35 | const ctx = useContext(ClientContext); |
| 36 | if (!ctx) { |
| 37 | throw new Error("useClient must be used within a ClientProvider"); |
| 38 | } |
| 39 | return ctx; |
| 40 | } |
| 41 |