From 24e0bcbf92f2833f60e00b3d1d081de70ca1888e Mon Sep 17 00:00:00 2001 From: Niklas Date: Sat, 28 Aug 2021 15:28:38 +0200 Subject: [PATCH] refactor file browser list --- src/components/FileBrowser.tsx | 80 +++++++++------------------ src/components/FileBrowserElement.tsx | 8 +-- src/components/FileBrowserList.tsx | 51 +++++++++++++++++ 3 files changed, 81 insertions(+), 58 deletions(-) create mode 100644 src/components/FileBrowserList.tsx diff --git a/src/components/FileBrowser.tsx b/src/components/FileBrowser.tsx index ec16c5b..90713e6 100644 --- a/src/components/FileBrowser.tsx +++ b/src/components/FileBrowser.tsx @@ -11,10 +11,10 @@ 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" import { ReactComponent as Spinner } from "./../assets/spinner.svg" +import FileBrowserList from "./FileBrowserList" function uriToPath(pathname:string) { // strip the "/f" from e.g. "/f/dir1/dir2" @@ -65,24 +65,6 @@ const FileBrowser: React.FC = (props) => { 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: @@ -149,48 +131,38 @@ const FileBrowser: React.FC = (props) => {
- - - - - - - - - - { data?.directorys.map(v => ({ - props.history.push(pathToUri(dir.id)) - }} - onContextMenu={(e)=>{ - openDirContextMenu(e,v.id) - }} - />))} - - { data?.files.map(v => ({ - setOpenFileId(file.id) - setShowFile(true) - }} - onContextMenu={(e)=>{ - openFileContextMenu(e,v.id) - }} - />))} - -
NameLast ModifiedSize
+ { + props.history.push(pathToUri(path)) + }} + + onDirContext={(e,path)=>{ + e.preventDefault() + showDirContext(e,{props:{id:path}}) + }} + + onFileClick={(e,id)=>{ + setOpenFileId(id) + setShowFile(true) + }} + + onFileContext={(e,id)=>{ + e.preventDefault() + showFileContext(e,{props:{id}}) + }} + /> {loading &&
}
- {{ + { setShowFile(false) - }} />} + }} /> ) diff --git a/src/components/FileBrowserElement.tsx b/src/components/FileBrowserElement.tsx index c2c3606..f10f7c6 100644 --- a/src/components/FileBrowserElement.tsx +++ b/src/components/FileBrowserElement.tsx @@ -7,7 +7,7 @@ import FileElement from "./FileElement" interface Props { file?: File | null dir?: Directory | null - onClick?: (data: File | Directory) => void + onClick?: (event: React.MouseEvent ,data: File | Directory) => void onContextMenu?: (e:React.MouseEvent) => void } @@ -15,11 +15,11 @@ const FileBrowserElement: React.FC = (props) => { return ( { + onClick={(e)=>{ if(props.file){ - props.onClick?.(props.file) + props.onClick?.(e,props.file) }else if(props.dir){ - props.onClick?.(props.dir) + props.onClick?.(e,props.dir) } }} diff --git a/src/components/FileBrowserList.tsx b/src/components/FileBrowserList.tsx new file mode 100644 index 0000000..c523811 --- /dev/null +++ b/src/components/FileBrowserList.tsx @@ -0,0 +1,51 @@ +import React from "react" +import { Directory, File } from "../generated/graphql" +import FileBrowserElement from "./FileBrowserElement" + +interface Props{ + directorys: Directory[] + files: File[] + onFileContext?: (event: React.MouseEvent, id: string)=>void + onDirContext?: (event: React.MouseEvent, path: string)=>void + onFileClick?: (event: React.MouseEvent,id: string)=>void + onDirClick?: (event: React.MouseEvent,path: string)=>void +} + +const FileBrowserList: React.FC = (props) => { + return <> + + + + + + + + + + { props.directorys.map(v => ({ + props.onDirClick?.(e,dir.id) + }} + onContextMenu={(e)=>{ + props.onDirContext?.(e,v.id) + }} + />))} + + { props.files.map(v => ({ + props.onFileClick?.(e,file.id) + }} + onContextMenu={(e)=>{ + props.onFileContext?.(e,v.id) + }} + />))} + +
NameLast ModifiedSize
+ +} + +export default FileBrowserList