41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { createContext, useContext, useState, type ReactNode } from "react";
|
|
|
|
export interface CanvasThreadContextValue {
|
|
threadId: string | null;
|
|
modelName: string | null;
|
|
setThreadId: (id: string | null) => void;
|
|
setModelName: (name: string | null) => void;
|
|
}
|
|
|
|
const CanvasThreadContext = createContext<CanvasThreadContextValue | null>(null);
|
|
|
|
export function CanvasThreadProvider({
|
|
children,
|
|
initialThreadId,
|
|
}: {
|
|
children: ReactNode;
|
|
initialThreadId?: string | null;
|
|
}) {
|
|
const [threadId, setThreadId] = useState<string | null>(
|
|
initialThreadId ?? null,
|
|
);
|
|
const [modelName, setModelName] = useState<string | null>(null);
|
|
|
|
return (
|
|
<CanvasThreadContext.Provider
|
|
value={{ threadId, setThreadId, modelName, setModelName }}
|
|
>
|
|
{children}
|
|
</CanvasThreadContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useCanvasThreadContext() {
|
|
const ctx = useContext(CanvasThreadContext);
|
|
if (!ctx)
|
|
throw new Error(
|
|
"useCanvasThreadContext must be used within CanvasThreadProvider",
|
|
);
|
|
return ctx;
|
|
}
|