2021-07-26 12:54:22 +00:00
|
|
|
import React from "react"
|
|
|
|
import { useState } from "react"
|
2021-08-10 09:21:18 +00:00
|
|
|
import { useContextMenu } from "react-contexify"
|
2021-08-06 11:59:01 +00:00
|
|
|
import uploadFile from "../functions/uploadFile"
|
2021-07-26 12:54:22 +00:00
|
|
|
import { useOpenDirQuery } from "../generated/graphql"
|
2021-07-27 17:24:52 +00:00
|
|
|
import Breadcrum from "./Breadcrum"
|
2021-08-01 19:42:26 +00:00
|
|
|
import DragAndDrop from "./DragAndDrop"
|
2021-08-10 09:21:18 +00:00
|
|
|
import FileBrowserContextMenu, { CONTEXT_MENU_DIR, CONTEXT_MENU_FILE } from "./FileBrowserContextMenu"
|
2021-07-26 12:54:22 +00:00
|
|
|
import FileBrowserElement from "./FileBrowserElement"
|
2021-07-28 10:49:35 +00:00
|
|
|
import FileOpen from "./FileOpen"
|
2021-07-26 12:54:22 +00:00
|
|
|
|
|
|
|
const FileBrowser: React.FC = () => {
|
|
|
|
const [path,setPath] = useState("/")
|
2021-07-28 10:49:35 +00:00
|
|
|
const [openFileId, setOpenFileId] = useState("")
|
|
|
|
const [showFile, setShowFile] = useState(false)
|
2021-07-26 12:54:22 +00:00
|
|
|
|
2021-08-10 09:21:18 +00:00
|
|
|
const { show: showFileContext } = useContextMenu({
|
|
|
|
id: CONTEXT_MENU_FILE,
|
|
|
|
})
|
|
|
|
|
|
|
|
const { show: showDirContext } = useContextMenu({
|
|
|
|
id: CONTEXT_MENU_DIR,
|
|
|
|
})
|
|
|
|
|
2021-07-30 20:54:36 +00:00
|
|
|
const { data, loading } = useOpenDirQuery({
|
2021-07-26 12:54:22 +00:00
|
|
|
variables:{
|
|
|
|
path
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-08-06 11:59:01 +00:00
|
|
|
async function handleDrop(files:FileList) {
|
|
|
|
const wait: Promise<boolean>[] = []
|
2021-08-01 19:42:26 +00:00
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
const file = files[i]
|
2021-08-06 11:59:01 +00:00
|
|
|
wait.push(uploadFile(file, path + file.name))
|
2021-08-01 19:42:26 +00:00
|
|
|
}
|
2021-08-06 11:59:01 +00:00
|
|
|
|
|
|
|
await Promise.all(wait)
|
2021-08-01 19:42:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 09:21:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-26 12:54:22 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2021-08-10 09:21:18 +00:00
|
|
|
<FileBrowserContextMenu/ >
|
2021-08-01 19:42:26 +00:00
|
|
|
<DragAndDrop
|
2021-08-06 11:59:01 +00:00
|
|
|
handleDrop={async (files)=>{
|
|
|
|
await handleDrop(files)
|
|
|
|
}}
|
2021-08-01 19:42:26 +00:00
|
|
|
>
|
|
|
|
<Breadcrum path={path} onDirClick={(newPath)=>{
|
|
|
|
setPath(newPath)
|
|
|
|
}}/>
|
|
|
|
<div>
|
|
|
|
{loading &&
|
|
|
|
<div className="loading loading-lg"></div> // TODO: center
|
|
|
|
}
|
|
|
|
<table className="table table-striped table-hover">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>Name</th>
|
2021-08-06 17:24:47 +00:00
|
|
|
<th>Last Modified</th>
|
2021-08-01 19:42:26 +00:00
|
|
|
<th>Size</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2021-07-26 12:54:22 +00:00
|
|
|
|
2021-08-01 19:42:26 +00:00
|
|
|
{ data?.directorys.map(v => (<FileBrowserElement
|
|
|
|
key={v?.id}
|
|
|
|
dir={v}
|
|
|
|
onClick={(dir)=>{
|
|
|
|
setPath(dir.id)
|
|
|
|
}}
|
2021-08-10 09:21:18 +00:00
|
|
|
onContextMenu={(e)=>{
|
|
|
|
openDirContextMenu(e,v.id)
|
|
|
|
}}
|
2021-08-01 19:42:26 +00:00
|
|
|
/>))}
|
|
|
|
|
|
|
|
{ data?.files.map(v => (<FileBrowserElement
|
|
|
|
key={v?.id}
|
|
|
|
file={v}
|
|
|
|
onClick={(file)=>{
|
|
|
|
setOpenFileId(file.id)
|
|
|
|
setShowFile(true)
|
|
|
|
}}
|
2021-08-10 09:21:18 +00:00
|
|
|
onContextMenu={(e)=>{
|
|
|
|
openFileContextMenu(e,v.id)
|
|
|
|
}}
|
2021-08-01 19:42:26 +00:00
|
|
|
/>))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
{<FileOpen id={openFileId} show={showFile} onCloseClick={()=>{
|
|
|
|
setShowFile(false)
|
|
|
|
}} />}
|
|
|
|
</DragAndDrop>
|
2021-07-26 12:54:22 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileBrowser
|