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 (
{messages.map((message, index) => (

{message.text}

))} {isLoading && (
)}
setInput(e.target.value)} placeholder="Type your message..." onKeyPress={(e) => e.key === 'Enter' && handleSend()} />
); } export default App;