From 37afa3411f08dd33680d1e66da89a313915bd128 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Wed, 7 Aug 2024 18:51:20 +0530 Subject: [PATCH] Improve the file upload experience in the settings page --- src/interface/web/app/settings/page.tsx | 155 +++++++++++++++++++++++- 1 file changed, 150 insertions(+), 5 deletions(-) diff --git a/src/interface/web/app/settings/page.tsx b/src/interface/web/app/settings/page.tsx index e3527003..c6a219fd 100644 --- a/src/interface/web/app/settings/page.tsx +++ b/src/interface/web/app/settings/page.tsx @@ -3,7 +3,7 @@ import styles from "./settings.module.css"; import "intl-tel-input/styles"; -import { Suspense, useEffect, useState } from "react"; +import { Suspense, useEffect, useRef, useState } from "react"; import { useToast } from "@/components/ui/use-toast"; import { useUserConfig, ModelOptions, UserConfig } from "../common/auth"; @@ -67,11 +67,47 @@ import SidePanel from "../components/sidePanel/chatHistorySidePanel"; import Loading from "../components/loading/loading"; import IntlTelInput from "intl-tel-input/react"; +import { uploadDataForIndexing } from "../common/chatFunctions"; +import { + AlertDialog, + AlertDialogAction, + AlertDialogContent, + AlertDialogDescription, + AlertDialogHeader, + AlertDialogTitle, +} from "@/components/ui/alert-dialog"; +import { Progress } from "@/components/ui/progress"; +import Link from "next/link"; const ManageFilesModal: React.FC<{ onClose: () => void }> = ({ onClose }) => { const [syncedFiles, setSyncedFiles] = useState([]); const [selectedFiles, setSelectedFiles] = useState([]); const [searchQuery, setSearchQuery] = useState(""); + const [isDragAndDropping, setIsDragAndDropping] = useState(false); + + const [warning, setWarning] = useState(null); + const [error, setError] = useState(null); + const [uploading, setUploading] = useState(false); + const [progressValue, setProgressValue] = useState(0); + const [uploadedFiles, setUploadedFiles] = useState([]); + const fileInputRef = useRef(null); + + useEffect(() => { + if (!uploading) { + setProgressValue(0); + } + + if (uploading) { + const interval = setInterval(() => { + setProgressValue((prev) => { + const increment = Math.floor(Math.random() * 5) + 1; // Generates a random number between 1 and 5 + const nextValue = prev + increment; + return nextValue < 100 ? nextValue : 100; // Ensures progress does not exceed 100 + }); + }, 800); + return () => clearInterval(interval); + } + }, [uploading]); useEffect(() => { const fetchFiles = async () => { @@ -94,7 +130,7 @@ const ManageFilesModal: React.FC<{ onClose: () => void }> = ({ onClose }) => { }; fetchFiles(); - }, []); + }, [uploadedFiles]); const filteredFiles = syncedFiles.filter((file) => file.toLowerCase().includes(searchQuery.toLowerCase()), @@ -161,9 +197,105 @@ const ManageFilesModal: React.FC<{ onClose: () => void }> = ({ onClose }) => { } }; + function handleDragOver(event: React.DragEvent) { + event.preventDefault(); + setIsDragAndDropping(true); + } + + function handleDragLeave(event: React.DragEvent) { + event.preventDefault(); + setIsDragAndDropping(false); + } + + function handleDragAndDropFiles(event: React.DragEvent) { + event.preventDefault(); + setIsDragAndDropping(false); + + if (!event.dataTransfer.files) return; + + uploadFiles(event.dataTransfer.files); + } + + function openFileInput() { + if (fileInputRef && fileInputRef.current) { + fileInputRef.current.click(); + } + } + + function handleFileChange(event: React.ChangeEvent) { + if (!event.target.files) return; + + uploadFiles(event.target.files); + } + + function uploadFiles(files: FileList) { + uploadDataForIndexing(files, setWarning, setUploading, setError, setUploadedFiles); + } + return ( + + + + Alert + + {warning || error} + { + setWarning(null); + setError(null); + setUploading(false); + }} + > + Close + + + +
+ +
+ Upload files + {uploading && ( + + )} +
+
+
+ {isDragAndDropping ? ( +
+ + Drop files to upload +
+ ) : ( +
+ + Drag and drop files here +
+ )} +
+
+
+
Synced files
void }> = ({ onClose }) => { onValueChange={setSearchQuery} />
-
- No such files synced. + + {syncedFiles.length === 0 ? ( +
+ + No files synced +
+ ) : ( +
+ Couldn't find a good match. + + Need advanced search? Click here. + +
+ )} +
{filteredFiles.map((filename: string) => ( void }> = ({ onClose }) => { > {selectedFiles.length > 0 - ? `Delect Selected (${selectedFiles.length})` + ? `Delete Selected (${selectedFiles.length})` : "Delete All"}