"use client"; import { Dispatch, SetStateAction, useCallback, useState, useRef } from "react"; import { FileRejection, useDropzone } from "react-dropzone"; import axios from "axios"; import { Message } from "@/lib/types"; import Button from "../components/ui/Button"; import { MdClose } from "react-icons/md"; import { AnimatePresence, motion } from "framer-motion"; import Link from "next/link"; import Card from "../components/ui/Card"; import PageHeading from "../components/ui/PageHeading"; export default function UploadPage() { const [message, setMessage] = useState(null); const [isPending, setIsPending] = useState(false); const [files, setFiles] = useState([]); const [pendingFileIndex, setPendingFileIndex] = useState(0); const urlInputRef = useRef(null); const crawlWebsite = useCallback(async () => { // Validate URL const url = urlInputRef.current ? urlInputRef.current.value : null; if (!url || !isValidUrl(url)) { // Assuming you have a function to validate URLs setMessage({ type: "error", text: "Invalid URL", }); return; } // Configure parameters const config = { url: url, js: false, depth: 1, max_pages: 100, max_time: 60, }; try { const response = await axios.post( `${process.env.NEXT_PUBLIC_BACKEND_URL}/crawl`, config ); setMessage({ type: response.data.type, text: response.data.message, }); } catch (error: any) { setMessage({ type: "error", text: "Failed to crawl website: " + error.toString(), }); } }, []); const upload = useCallback(async (file: File) => { const formData = new FormData(); formData.append("file", file); try { const response = await axios.post( `${process.env.NEXT_PUBLIC_BACKEND_URL}/upload`, formData ); setMessage({ type: response.data.type, text: (response.data.type === "success" ? "File uploaded successfully: " : "") + JSON.stringify(response.data.message), }); } catch (error: any) { setMessage({ type: "error", text: "Failed to upload file: " + error.toString(), }); } }, []); const onDrop = (acceptedFiles: File[], fileRejections: FileRejection[]) => { if (fileRejections.length > 0) { setMessage({ type: "error", text: "File too big." }); return; } setMessage(null); for (let i = 0; i < acceptedFiles.length; i++) { const file = acceptedFiles[i]; const isAlreadyInFiles = files.filter((f) => f.name === file.name && f.size === file.size) .length > 0; if (isAlreadyInFiles) { setMessage({ type: "warning", text: `${file.name} was already added` }); acceptedFiles.splice(i, 1); } } setFiles((files) => [...files, ...acceptedFiles]); }; const uploadAllFiles = async () => { setIsPending(true); setMessage(null); // files.forEach((file) => upload(file)); for (const file of files) { await upload(file); setPendingFileIndex((i) => i + 1); } setPendingFileIndex(0); setIsPending(false); }; const { getRootProps, getInputProps, isDragActive, open } = useDropzone({ onDrop, noClick: true, maxSize: 100000000, // 1 MB }); return (
{/* Wrap the cards in a flex container */}
{/* Assign a width of 50% to each card */}
{files.length > 0 ? ( {files.map((file, i) => ( ))} ) : null} {isDragActive ? (

Drop the files here...

) : ( )}
{/* Assign a width of 50% to each card */}
{message && (

{message.text}

)}
); } const FileComponent = ({ file, setFiles, }: { file: File; setFiles: Dispatch>; }) => { return (
{file.name} {(file.size / 1000).toFixed(3)} kb
); }; function isValidUrl(string: string) { try { new URL(string); return true; } catch (_) { return false; } }