"use client"; import { useState } from "react"; import ChatMessage from "./ChatMessage"; import FileUpload from "./FileUpload"; interface Message { type: "user" | "assistant"; content: string; } export default function Chat() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const [hasUploadedFile, setHasUploadedFile] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!input.trim() || isLoading) return; const userMessage = input.trim(); setInput(""); setMessages((prev) => [...prev, { type: "user", content: userMessage }]); setIsLoading(true); try { const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/ask`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text: userMessage }), }); const data = await response.json(); setMessages((prev) => [ ...prev, { type: "assistant", content: data.answer }, ]); } catch (error) { console.error("Error:", error); setMessages((prev) => [ ...prev, { type: "assistant", content: "Sorry, there was an error processing your request.", }, ]); } finally { setIsLoading(false); } }; return (
{!hasUploadedFile ? ( setHasUploadedFile(true)} /> ) : ( <>
{messages.map((message, index) => ( ))} {isLoading && (
Loading...
)}
setInput(e.target.value)} placeholder="Ask a question about your PDF..." className="flex-1 p-2 border rounded-lg dark:bg-gray-800" disabled={isLoading} />
)}
); }