-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
64 lines (58 loc) · 2.25 KB
/
App.tsx
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
import React, { useState } from 'react';
import axios from 'axios';
function App() {
const [input, setInput] = useState('');
const [messages, setMessages] = useState<{ type: 'user' | 'assistant'; content: string }[]>([]);
const [isLoading, setIsLoading] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!input.trim()) return;
setMessages(prev => [...prev, { type: 'user', content: input }]);
setInput('');
setIsLoading(true);
try {
const response = await axios.post('http://localhost:8000/chat', { message: input });
setMessages(prev => [...prev, { type: 'assistant', content: response.data.response }]);
} catch (error) {
console.error('Error:', error);
setMessages(prev => [...prev, { type: 'assistant', content: 'Sorry, an error occurred.' }]);
} finally {
setIsLoading(false);
}
};
return (
<div className="flex flex-col h-screen bg-gray-100">
<header className="bg-blue-600 text-white p-4">
<h1 className="text-2xl font-bold">VALORANT Esports Assistant</h1>
</header>
<main className="flex-1 overflow-y-auto p-4">
{messages.map((message, index) => (
<div key={index} className={`mb-4 ${message.type === 'user' ? 'text-right' : 'text-left'}`}>
<div className={`inline-block p-2 rounded-lg ${message.type === 'user' ? 'bg-blue-500 text-white' : 'bg-white'}`}>
{message.content}
</div>
</div>
))}
{isLoading && <div className="text-center">Loading...</div>}
</main>
<form onSubmit={handleSubmit} className="p-4 bg-white">
<div className="flex">
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
className="flex-1 p-2 border rounded-l-lg focus:outline-none focus:ring-2 focus:ring-blue-500"
placeholder="Ask about VALORANT esports..."
/>
<button
type="submit"
className="bg-blue-500 text-white p-2 rounded-r-lg hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
>
Send
</button>
</div>
</form>
</div>
);
}
export default App;