cryogenic22 commited on
Commit
625fb4f
·
verified ·
1 Parent(s): 6dbe119

Create ChatInterface.tsx

Browse files
frontend/src/components/ai-tutor/ChatInterface.tsx ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React, { useState } from 'react';
2
+ import { Card, CardContent } from '@/components/ui/card';
3
+ import TutorAvatar from './TutorAvatar';
4
+ import MessageBubble from './MessageBubble';
5
+
6
+ const ChatInterface = () => {
7
+ const [messages, setMessages] = useState([]);
8
+ const [input, setInput] = useState('');
9
+ const [tutorState, setTutorState] = useState('neutral');
10
+
11
+ const handleSendMessage = async () => {
12
+ if (!input.trim()) return;
13
+
14
+ const newMessage = {
15
+ role: 'user',
16
+ content: input,
17
+ timestamp: new Date().toISOString()
18
+ };
19
+
20
+ setMessages(prev => [...prev, newMessage]);
21
+ setInput('');
22
+ setTutorState('thinking');
23
+
24
+ try {
25
+ const response = await fetch('/api/chat', {
26
+ method: 'POST',
27
+ headers: { 'Content-Type': 'application/json' },
28
+ body: JSON.stringify({ message: input })
29
+ });
30
+
31
+ const data = await response.json();
32
+
33
+ setMessages(prev => [...prev, {
34
+ role: 'assistant',
35
+ content: data.response,
36
+ timestamp: new Date().toISOString()
37
+ }]);
38
+
39
+ setTutorState('happy');
40
+ } catch (error) {
41
+ console.error('Chat error:', error);
42
+ setTutorState('neutral');
43
+ }
44
+ };
45
+
46
+ return (
47
+ <Card className="h-[600px] flex flex-col">
48
+ <CardContent className="flex-1 flex flex-col p-4">
49
+ <TutorAvatar state={tutorState} />
50
+
51
+ <div className="flex-1 overflow-y-auto mb-4 space-y-4">
52
+ {messages.map((msg, idx) => (
53
+ <MessageBubble key={idx} {...msg} />
54
+ ))}
55
+ </div>
56
+
57
+ <div className="flex gap-2 mt-auto">
58
+ <input
59
+ type="text"
60
+ value={input}
61
+ onChange={(e) => setInput(e.target.value)}
62
+ onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()}
63
+ placeholder="Ask your question..."
64
+ className="flex-1 p-2 border rounded"
65
+ />
66
+ <button
67
+ onClick={handleSendMessage}
68
+ className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
69
+ >
70
+ Send
71
+ </button>
72
+ </div>
73
+ </CardContent>
74
+ </Card>
75
+ );
76
+ };
77
+
78
+ export default ChatInterface;