"use client"; import { Dispatch, SetStateAction, useCallback, useState } 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"; export default function UploadPage() { const [message, setMessage] = useState(null); const [isPending, setIsPending] = useState(false); const [files, setFiles] = useState([]); const [pendingFileIndex, setPendingFileIndex] = useState(0); const upload = useCallback(async (file: File) => { const formData = new FormData(); formData.append("file", file); try { const response = await axios.post( "http://localhost:5000/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 (

Add Knowledge

Upload files to your second brain

{files.length > 0 ? ( {files.map((file, i) => ( ))} ) : null} {isDragActive ? (

Drop the files here...

) : ( )}
{message && (

{message.text}

)}
); } const FileComponent = ({ file, setFiles, }: { file: File; setFiles: Dispatch>; }) => { return (
{file.name} {(file.size / 1000).toFixed(3)} kb
); };