import { ToastContainer, toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; import { useState, useEffect } from "react"; import { ChatMessageBubble } from "./ChatMessageBubble"; import { api } from "../lib/axios"; export function ChatWindow({ endpoint, placeholder, showIngestForm, showIntermediateStepsToggle }) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const [sourcesForMessages, setSourcesForMessages] = useState({}); const handleInputChange = (e) => setInput(e.currentTarget.value); const sendMessage = async (e) => { e.preventDefault(); setIsLoading(true); if (sessionStorage.getItem("ragChat@sessiorId") === null) { toast("É necessário enviar um arquivo!", { theme: "dark", }); } if (!input) { setIsLoading(false); return; } const tmp = "Me responda isso em português: " + input; setInput(""); if (!messages.length) { await new Promise((resolve) => setTimeout(resolve, 300)); } setMessages( messages.concat({ id: messages.length.toString(), content: input, role: "user", }), ); try { const response = await api.post( `/retriveal/stream/${sessionStorage.getItem("ragChat@sessiorId")}`, { content: tmp } ); const json = response.data; if (response.status === 200) { setMessages((prevState) => { const prev = [...prevState]; prev.push({ id: prev.length.toString(), content: json.replace("", " "), role: "assistant", }); return prev; }); } else { if (json.error) { toast(json.error, { theme: "dark", }); throw new Error(json.error); } } } catch (error) { toast(error.message, { theme: "dark", }); } setIsLoading(false); }; // Preciso que quando essa página for fechada o sessionStorage, alertar o usuário que ele perderá os dados // e enviar uma requisição para o servidor para que ele apague os dados do usuário useEffect(() => { const listener = () => { const session = sessionStorage.getItem("ragChat@sessiorId"); if (!session) return; try { navigator.sendBeacon(`/session/${session}`); sessionStorage.removeItem("ragChat@sessiorId"); sessionStorage.removeItem("uploadedDocuments"); } catch (error) { console.log(error); } } window.addEventListener("unload", listener); return () => window.removeEventListener("unload", listener) }, []); return (