import React from "react" import { useState } from "react" import { useContextMenu } from "react-contexify" import uploadFile from "../functions/uploadFile" import { useDeleteFileMutation, useOpenDirQuery } from "../generated/graphql" import Breadcrum from "./Breadcrum" import DragAndDrop from "./DragAndDrop" import FileBrowserContextMenu, { Action, CONTEXT_MENU_DIR, CONTEXT_MENU_FILE } from "./FileBrowserContextMenu" import FileBrowserElement from "./FileBrowserElement" import FileOpen from "./FileOpen" const FileBrowser: React.FC = () => { const [path,setPath] = useState("/") const [openFileId, setOpenFileId] = useState("") const [showFile, setShowFile] = useState(false) const [deleteMutation] = useDeleteFileMutation() const { show: showFileContext } = useContextMenu({ id: CONTEXT_MENU_FILE, }) const { show: showDirContext } = useContextMenu({ id: CONTEXT_MENU_DIR, }) const { data, loading } = 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) } 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 } }) } function onContextSelect(action:Action, id: string) { console.debug(action) switch (action) { case Action.FileDelete: deleteMutation({variables:{id}}) break default: break } } return (
{ await handleDrop(files) }} > { setPath(newPath) }}/>
{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