import { useEffect } from 'react'; import { useChat } from '../context/ChatProvider'; import { INSUFFICIENT_BALANCE_NOTICE } from '../hooks/useTKMindChat'; import type { PageEditSubChatBridge } from '../hooks/usePageEditSubChat'; import type { MindSpaceChatContext, MindSpaceSaveCategory, PortalUser } from '../types'; import { formatContextChip } from '../utils/mindspaceChatContext'; import { ChatPanel } from './ChatPanel'; import { TKMindAvatar } from './TKMindAvatar'; export function SpaceChatPanel({ open, context, user, onClose, onOpenFullChat, onPageSaved, chatBridge, hideOpenFullChat = false, title = 'TKMind', }: { open: boolean; context: MindSpaceChatContext; user: PortalUser; onClose: () => void; onOpenFullChat: () => void; onPageSaved?: (result: { kind: 'page' | 'category'; pageId?: string; categoryCode?: MindSpaceSaveCategory; }) => void; chatBridge?: PageEditSubChatBridge; hideOpenFullChat?: boolean; title?: string; }) { const mainChat = useChat(); const chat = chatBridge ?? mainChat; const { session, messages, chatState, pendingTool, submit, stop, approveTool, error, notice, dismissNotice, openRecharge, retryConnect, } = chat; useEffect(() => { if (!open) return; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') onClose(); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [open, onClose]); if (!open) return null; const contextLabel = formatContextChip(context); return (
{title} {contextLabel}
{!hideOpenFullChat ? ( ) : null}
{notice && (
{notice} {notice === INSUFFICIENT_BALANCE_NOTICE ? ( <> ) : ( )}
)} {error && (
{error}
)} chatBridge ? void submit(text, context) : void submit(text, { mindspaceContext: context }) } onStop={stop} onApproveTool={approveTool} onPageSaved={onPageSaved} onClose={onClose} />
); }