import React, { useState } from 'react'; import { Card, CardContent } from '@/components/ui/card'; import TutorAvatar from './TutorAvatar'; import MessageBubble from './MessageBubble'; const ChatInterface = () => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [tutorState, setTutorState] = useState('neutral'); const handleSendMessage = async () => { if (!input.trim()) return; const newMessage = { role: 'user', content: input, timestamp: new Date().toISOString() }; setMessages(prev => [...prev, newMessage]); setInput(''); setTutorState('thinking'); try { const response = await fetch('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: input }) }); const data = await response.json(); setMessages(prev => [...prev, { role: 'assistant', content: data.response, timestamp: new Date().toISOString() }]); setTutorState('happy'); } catch (error) { console.error('Chat error:', error); setTutorState('neutral'); } }; return (
{messages.map((msg, idx) => ( ))}
setInput(e.target.value)} onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()} placeholder="Ask your question..." className="flex-1 p-2 border rounded" />
); }; export default ChatInterface;