2e14873f2d
Track application source and tests; exclude local env, user workspaces, and runtime data via .gitignore. Co-authored-by: Cursor <cursoragent@cursor.com>
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { createContext, useContext, type ReactNode } from 'react';
|
|
import { RechargeModal } from '../components/RechargeModal';
|
|
import { useTKMindChat } from '../hooks/useTKMindChat';
|
|
import type { CapabilityMap, PortalUser } from '../types';
|
|
|
|
type ChatContextValue = ReturnType<typeof useTKMindChat>;
|
|
|
|
const ChatContext = createContext<ChatContextValue | null>(null);
|
|
|
|
export function ChatProvider({
|
|
user,
|
|
capabilities,
|
|
onUserUpdate,
|
|
children,
|
|
}: {
|
|
user?: PortalUser | null;
|
|
capabilities?: CapabilityMap | null;
|
|
onUserUpdate?: (user: PortalUser) => void;
|
|
children: ReactNode;
|
|
}) {
|
|
const chat = useTKMindChat(user, onUserUpdate, capabilities);
|
|
|
|
return (
|
|
<ChatContext.Provider value={chat}>
|
|
{children}
|
|
{typeof chat.balanceCents === 'number' && chat.rechargePrompt && (
|
|
<RechargeModal
|
|
open={chat.rechargePrompt}
|
|
force={chat.rechargeForced}
|
|
balanceCents={chat.balanceCents}
|
|
onClose={chat.dismissRecharge}
|
|
onSuccess={(nextBalance) => {
|
|
chat.completeRecharge(nextBalance);
|
|
}}
|
|
/>
|
|
)}
|
|
</ChatContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useChat(): ChatContextValue {
|
|
const value = useContext(ChatContext);
|
|
if (!value) {
|
|
throw new Error('useChat must be used within ChatProvider');
|
|
}
|
|
return value;
|
|
}
|