import React from "react" import { useState } from "react" import { useContextMenu } from "react-contexify" import normalizeDirPath from "../functions/normalizeDirPath" import uploadFile from "../functions/uploadFile" import { useCopyMutation, useCreateDirMutation, useDeleteDirMutation, useDeleteFileMutation, useMoveMutation, useOpenDirQuery } from "../generated/graphql" import Breadcrum from "./Breadcrum" import CreateDirButton from "./CreateDirButton" import DragAndDrop from "./DragAndDrop" import FileBrowserContextMenu, { Action, CONTEXT_MENU_DIR, CONTEXT_MENU_FILE } from "./FileBrowserContextMenu" import FileBrowserElement from "./FileBrowserElement" import FileOpen from "./FileOpen" import FileUploadButton from "./FileUploadButton" const FileBrowser: React.FC = () => { const [path,setPath] = useState("/") const [openFileId, setOpenFileId] = useState("") const [showFile, setShowFile] = useState(false) const [srcID,setSrcID] = useState("") const [pasteAction,setPasteAction] = useState() const [deleteMutation] = useDeleteFileMutation() const [copyMutation] = useCopyMutation() const [moveMutation] = useMoveMutation() const [createDirMutation] = useCreateDirMutation() const [deleteDirMutation] = useDeleteDirMutation() const { show: showFileContext } = useContextMenu({ id: CONTEXT_MENU_FILE, }) const { show: showDirContext } = useContextMenu({ id: CONTEXT_MENU_DIR, }) const { data, loading, refetch: refetchDir } = useOpenDirQuery({ variables:{ path } }) async function handleDrop(files:FileList) { const wait: Promise[] = [] for (let i = 0; i < files.length; i++) { const file = files[i] wait.push(uploadFile(file, path + file.name)) } await Promise.all(wait) refetchDir() } function openFileContextMenu(e: React.MouseEvent, id: string) { e.preventDefault() showFileContext(e,{ props:{ id } }) } function openDirContextMenu(e: React.MouseEvent, id: string) { e.preventDefault() showDirContext(e,{ props:{ id } }) } async function onContextSelect(action:Action, id: string) { switch (action) { case Action.FileDelete: await deleteMutation({variables:{id}}) refetchDir() break case Action.FileCopy: case Action.FileMove: setSrcID(id) setPasteAction(action) break case Action.FilePaste: if (pasteAction === Action.FileCopy){ await copyMutation({variables:{src:srcID,dest:path}}) refetchDir() } if (pasteAction === Action.FileMove){ await moveMutation({variables:{src:srcID,dest:path}}) refetchDir() } break case Action.DirDelete: await deleteDirMutation({variables:{path:id}}) refetchDir() break default: break } } return (
{ await handleDrop(files) }} >
{ setPath(newPath) }}/>
{ const fullPath = normalizeDirPath(normalizeDirPath(path) + dirName) await createDirMutation({variables:{path: fullPath}}) refetchDir() }} />
handleDrop(files)} />
{loading &&
Loading...
// TODO: center } { data?.directorys.map(v => ({ setPath(dir.id) }} onContextMenu={(e)=>{ openDirContextMenu(e,v.id) }} />))} { data?.files.map(v => ({ setOpenFileId(file.id) setShowFile(true) }} onContextMenu={(e)=>{ openFileContextMenu(e,v.id) }} />))}
Name Last Modified Size
{{ setShowFile(false) }} />}
) } export default FileBrowser