import React, { useEffect, useState, useRef } from "react"; import NavBar from "../components/NavBar"; import ChatWindow from "../components/ChatWindow"; const POLL_INTERVAL = 500; // 0.5 seconds export default function App() { const containerRef = useRef(null); const inputRef = useRef(null); const [conversation, setConversation] = useState([]); const [lastMessage, setLastMessage] = useState(null); // New state for tracking the last message const [userInput, setUserInput] = useState(""); const [loading, setLoading] = useState(false); const [done, setDone] = useState(true); useEffect(() => { // Poll /get-conversation-history every 0.5 seconds const intervalId = setInterval(async () => { try { const res = await fetch("http://127.0.0.1:8000/get-conversation-history"); if (res.ok) { const data = await res.json(); const newConversation = data.messages || []; setConversation(newConversation); if (newConversation.length > 0) { const lastMsg = newConversation[newConversation.length - 1]; setLoading(lastMsg.actor !== "agent"); setDone(lastMsg.response.next === "done"); // Only scroll if the last message changes if (!lastMessage || lastMsg.response.response !== lastMessage.response.response) { setLastMessage(lastMsg); // Update the last message } } else { setLoading(false); setDone(true); setLastMessage(null); // Clear last message if no messages } } } catch (err) { console.error("Error fetching conversation history:", err); } }, POLL_INTERVAL); return () => clearInterval(intervalId); }, [lastMessage]); useEffect(() => { if (containerRef.current && lastMessage) { containerRef.current.scrollTop = containerRef.current.scrollHeight; } }, [lastMessage]); // Scroll only when the last message changes useEffect(() => { if (inputRef.current) { inputRef.current.focus(); // Ensure the input box retains focus } }, [userInput, loading, done]); const handleSendMessage = async () => { if (!userInput.trim()) return; try { setLoading(true); await fetch( `http://127.0.0.1:8000/send-prompt?prompt=${encodeURIComponent(userInput)}`, { method: "POST" } ); setUserInput(""); } catch (err) { console.error("Error sending prompt:", err); setLoading(false); } }; const handleConfirm = async () => { try { setLoading(true); await fetch("http://127.0.0.1:8000/confirm", { method: "POST" }); } catch (err) { console.error("Confirm error:", err); setLoading(false); } }; const handleStartNewChat = async () => { try { await fetch( `http://127.0.0.1:8000/send-prompt?prompt=${encodeURIComponent("I'd like to travel for an event.")}`, { method: "POST" } ); setConversation([]); // Clear local state setLastMessage(null); // Reset last message } catch (err) { console.error("Error ending chat:", err); } }; const handleKeyPress = (e) => { if (e.key === "Enter") { handleSendMessage(); } }; return (
{/* Centered content, but no manual bottom margin */}
{/* Scrollable chat area */}
{done && (
Chat ended
)}
{/* Fixed bottom input */}
setUserInput(e.target.value)} onKeyPress={handleKeyPress} disabled={loading || done} />
); }