s3browser-frontend/src/components/FileBrowser.tsx

134 lines
2.9 KiB
TypeScript
Raw Normal View History

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-08-13 23:33:20 +00:00
import { useDeleteFileMutation, 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-13 23:33:20 +00:00
import FileBrowserContextMenu, { Action, 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-13 23:33:20 +00:00
const [deleteMutation] = useDeleteFileMutation()
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-08-13 23:33:20 +00:00
function onContextSelect(action:Action, id: string) {
console.debug(action)
switch (action) {
case Action.FileDelete:
deleteMutation({variables:{id}})
break
default:
break
}
}
2021-07-26 12:54:22 +00:00
return (
<div>
2021-08-13 23:33:20 +00:00
<FileBrowserContextMenu
onSelect={onContextSelect}
/>
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