import React from 'react'; import { useSessionStore } from '@openchamber/ui/stores/useSessionStore'; import { useNavigation } from '../hooks/useNavigation'; import { VSCodeHeader } from './VSCodeHeader'; import { SimpleMessageRenderer } from './SimpleMessageRenderer'; export function ChatPanel() { const { goToSessions } = useNavigation(); const messagesEndRef = React.useRef(null); const scrollContainerRef = React.useRef(null); const currentSessionId = useSessionStore((s) => s.currentSessionId); const messages = useSessionStore((s) => s.messages); const sessions = useSessionStore((s) => s.sessions); const sendMessage = useSessionStore((s) => s.sendMessage); const abortCurrentOperation = useSessionStore((s) => s.abortCurrentOperation); const streamingMessageIds = useSessionStore((s) => s.streamingMessageIds); const [inputValue, setInputValue] = React.useState(''); const [isSending, setIsSending] = React.useState(false); const currentSession = sessions.find((s) => s.id === currentSessionId); const sessionTitle = currentSession?.title || 'New Chat'; const sessionMessages = currentSessionId ? messages.get(currentSessionId) || [] : []; const isStreaming = currentSessionId ? streamingMessageIds.has(currentSessionId) : false; // Auto-scroll to bottom when new messages arrive React.useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [sessionMessages.length]); const handleSend = async () => { if (!inputValue.trim() || !currentSessionId || isSending) return; const messageText = inputValue.trim(); setInputValue(''); setIsSending(true); try { await sendMessage(messageText); } catch (error) { console.error('Failed to send message:', error); setInputValue(messageText); // Restore input on error } finally { setIsSending(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const handleAbort = () => { if (currentSessionId) { abortCurrentOperation(currentSessionId); } }; return (
{/* Header */} {/* Messages */}
{sessionMessages.length === 0 ? (
Start a conversation
) : (
{sessionMessages.map((msg) => ( ))}
)}
{/* Input */}