-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathpage.tsx
More file actions
133 lines (121 loc) · 4.05 KB
/
page.tsx
File metadata and controls
133 lines (121 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use client';
import React, { useMemo, useState } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
type ChatMessageRole = 'system' | 'user' | 'assistant';
interface ChatMessage {
role: ChatMessageRole;
content: string;
}
interface ChatEntry {
role: 'user' | 'bot';
content: string;
}
export default function ClientSideChatPage() {
const [messages, setMessages] = useState<ChatMessage[]>([
{ role: 'system', content: 'You are a friendly assistant that loves using emojis.' },
]);
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const chatLog = useMemo<ChatEntry[]>(
() =>
messages
.filter((msg) => msg.role !== 'system')
.map((msg) => ({
role: msg.role === 'user' ? 'user' : 'bot',
content: msg.content,
})),
[messages]
);
const sendQuestion = async () => {
const trimmed = input.trim();
if (!trimmed) return;
setLoading(true);
setInput('');
const nextMessages: ChatMessage[] = [...messages, { role: 'user', content: trimmed }];
setMessages(nextMessages);
try {
const res = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages: nextMessages }),
});
const data = (await res.json()) as {
message?: string;
role?: ChatMessageRole;
error?: string;
};
if (res.ok && data.message) {
const message = data.message;
setMessages((prev) => [...prev, { role: data.role ?? 'assistant', content: message }]);
} else {
setMessages((prev) => [
...prev,
{ role: 'assistant', content: data.error || 'Failed to get a valid response.' },
]);
}
} catch {
setMessages((prev) => [
...prev,
{ role: 'assistant', content: 'Network error, please try again.' },
]);
}
setLoading(false);
};
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter') {
e.preventDefault();
sendQuestion();
}
};
return (
<div className="flex flex-col h-[calc(100vh-60px)] border rounded shadow bg-white dark:bg-gray-800">
{/* Chat Messages */}
<div className="flex-grow p-4 overflow-auto">
{chatLog.map((entry, index) => (
<div
key={index}
className={`mb-4 flex ${entry.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`max-w-[75%] p-3 rounded-lg ${
entry.role === 'user'
? 'bg-blue-500 text-white rounded-tr-none'
: 'bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-100 rounded-tl-none'
}`}
>
<ReactMarkdown remarkPlugins={[remarkGfm]}>{entry.content}</ReactMarkdown>
</div>
</div>
))}
{loading && (
<div className="mb-4 flex justify-start">
<div className="max-w-[75%] p-3 bg-gray-100 dark:bg-gray-700 text-gray-800 dark:text-gray-100 rounded-lg rounded-tl-none">
Thinking...
</div>
</div>
)}
</div>
{/* Input Area */}
<div className="border-t p-4 bg-gray-50 pl-20 dark:bg-gray-900">
<div className="flex space-x-2">
<input
type="text"
placeholder="Type your question..."
className="flex-grow p-2 border border-gray-300 dark:border-gray-600 rounded focus:outline-none bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-100"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={handleKeyPress}
/>
<button
onClick={sendQuestion}
disabled={loading || !input.trim()}
className="bg-blue-500 text-white px-4 py-2 rounded disabled:bg-gray-400"
>
{loading ? 'Sending...' : 'Send'}
</button>
</div>
</div>
</div>
);
}