| import React, { useState, useRef, useEffect } from 'react'; | |
| import './App.css'; | |
| function App() { | |
| const [input, setInput] = useState(''); | |
| const [messages, setMessages] = useState([]); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const messagesEndRef = useRef(null); | |
| const scrollToBottom = () => { | |
| messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); | |
| }; | |
| useEffect(scrollToBottom, [messages]); | |
| const handleSend = async () => { | |
| if (input.trim() === '') return; | |
| setMessages([...messages, { text: input, user: true }]); | |
| setInput(''); | |
| setIsLoading(true); | |
| try { | |
| const response = await fetch('http://127.0.0.1:8000/query', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify({ input: input }), | |
| }); | |
| const data = await response.json(); | |
| setMessages(prevMessages => [...prevMessages, { text: data.answer, user: false }]); | |
| } catch (error) { | |
| console.error('Error:', error); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }; | |
| return ( | |
| <div className="app"> | |
| <div className="messages-container"> | |
| {messages.map((message, index) => ( | |
| <div key={index} className={`message ${message.user ? 'user-message' : 'bot-message'}`}> | |
| <p>{message.text}</p> | |
| </div> | |
| ))} | |
| {isLoading && ( | |
| <div className="loading-container"> | |
| <div className="loading-bar"></div> | |
| </div> | |
| )} | |
| <div ref={messagesEndRef} /> | |
| </div> | |
| <div className="input-container"> | |
| <input | |
| type="text" | |
| value={input} | |
| onChange={(e) => setInput(e.target.value)} | |
| placeholder="Type your message..." | |
| onKeyPress={(e) => e.key === 'Enter' && handleSend()} | |
| /> | |
| <button onClick={handleSend}>Send</button> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| export default App; |