import { useState, useRef, useEffect } from "react"; import { ChevronLeft, FileText, Menu } from "lucide-react"; export function UploadDocumentsForm() { const [isLoading, setIsLoading] = useState(false); const [document, setDocument] = useState(null); const [uploadedDocuments, setUploadedDocuments] = useState([]); const formRef = useRef(null); const inputRef = useRef(null); const [formState, setFormState] = useState(true); const [errorMessage, setErrorMessage] = useState(""); const handleFileInput = (event) => { setDocument(event.target.files[0]); }; const ingest = async (e) => { e.preventDefault(); setIsLoading(true); if (!document) { setErrorMessage("Selecione um documento para enviar"); setIsLoading(false); return; } const isDocumentUploaded = uploadedDocuments.find( (doc) => doc.name === document.name, ); if (isDocumentUploaded) { setErrorMessage("Este documento já foi enviado"); setIsLoading(false); return; } try { const formData = new FormData(); formData.append("file", document); const url = sessionStorage.getItem("ragChat@sessiorId") ? `/retriveal/ingest/${sessionStorage.getItem("ragChat@sessiorId")}` : "/retriveal/ingest"; const response = await fetch(url, { method: 'POST', body: formData, }); if (response.status === 200) { console.log("Document ingested successfully"); const data = await response.json() // Salva o nome do arquivo e o tamanho do arquivo no estado uploadedDocuments setUploadedDocuments((prevState) => { return [...prevState, { name: document.name, size: document.size }]; }); // Salva essas informações no sessionStorage sessionStorage.setItem( "uploadedDocuments", JSON.stringify(uploadedDocuments), ); // Salva id recebido no sessionStorage if (!sessionStorage.getItem("ragChat@sessiorId")) sessionStorage.setItem("ragChat@sessiorId", data.uuid); setErrorMessage(""); setDocument(null); if (inputRef.current) inputRef.current.value = ""; } else if (response.status < 500) { console.error("Error ingesting document"); console.log(await response.text()) setErrorMessage("Erro ao enviar o documento! Recurso não encontrado."); } } catch (error) { console.error("Error ingesting document", error); setErrorMessage( "Erro ao enviar o documento! Tente novamente mais tarde.", ); } setIsLoading(false); }; useEffect(() => { const savedDocuments = sessionStorage.getItem("uploadedDocuments"); if (savedDocuments) { setUploadedDocuments(JSON.parse(savedDocuments)); } if (window.innerWidth) setFormState(window.innerWidth > 768) window.addEventListener("resize", () => { if (window.innerWidth > 768) { setFormState(true); } else { setFormState(false); } }); }, []); return ( <>
> ); }