2026-04-18 18:51:53 +00:00
|
|
|
import { createContext, useContext, type ReactNode } from "react";
|
|
|
|
|
|
|
|
|
|
import type { NanobotClient } from "@/lib/nanobot-client";
|
2026-07-14 14:47:42 +08:00
|
|
|
import type { WebUIIngressLimits } from "@/lib/types";
|
2026-04-18 18:51:53 +00:00
|
|
|
|
|
|
|
|
interface ClientContextValue {
|
|
|
|
|
client: NanobotClient;
|
|
|
|
|
token: string;
|
|
|
|
|
modelName: string | null;
|
2026-07-14 14:47:42 +08:00
|
|
|
ingressLimits: WebUIIngressLimits | null;
|
2026-04-18 18:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ClientContext = createContext<ClientContextValue | null>(null);
|
|
|
|
|
|
|
|
|
|
export function ClientProvider({
|
|
|
|
|
client,
|
|
|
|
|
token,
|
|
|
|
|
modelName = null,
|
2026-07-14 14:47:42 +08:00
|
|
|
ingressLimits = null,
|
2026-04-18 18:51:53 +00:00
|
|
|
children,
|
|
|
|
|
}: {
|
|
|
|
|
client: NanobotClient;
|
|
|
|
|
token: string;
|
|
|
|
|
modelName?: string | null;
|
2026-07-14 14:47:42 +08:00
|
|
|
ingressLimits?: WebUIIngressLimits | null;
|
2026-04-18 18:51:53 +00:00
|
|
|
children: ReactNode;
|
|
|
|
|
}) {
|
|
|
|
|
return (
|
2026-07-14 14:47:42 +08:00
|
|
|
<ClientContext.Provider value={{ client, token, modelName, ingressLimits }}>
|
2026-04-18 18:51:53 +00:00
|
|
|
{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;
|
|
|
|
|
}
|