11import React , { useEffect , useRef , useState } from "react" ;
22import { ReactComponent as FolderIcon } from "../../assets/icons/folder-icon.svg" ;
33
4- import { FSDirectory , FSObject , isFSDirectory } from "../../stores/localFS" ;
4+ import useLocalFS , {
5+ FSDirectory ,
6+ FSObject ,
7+ FSObjectType ,
8+ isFSDirectory ,
9+ } from "../../stores/localFS" ;
510import useLocalFSWithHistory from "../../hooks/useLocalFSWithHistory" ;
611
712import "./FileBrowser.scss" ;
813import AppSideBar from "../../components/AppSideBar" ;
914import ContextMenu from "../../ContextMenu/ContextMenu" ;
1015import { getMainContentContextItems } from "./contextMenus" ;
16+ import FullAppPrompt from "../../components/FullAppPrompt" ;
1117const defaultPath = "/home/user" ;
1218
1319interface FileBrowserProps {
@@ -57,30 +63,47 @@ function TopBar({
5763interface MainContentProps {
5864 currentDirectory : FSDirectory ;
5965 openFSObject : ( fsObject : FSObject ) => void ;
66+ appRef : React . RefObject < HTMLDivElement > ;
6067}
6168
62- function MainContent ( { currentDirectory, openFSObject } : MainContentProps ) {
69+ function MainContent ( {
70+ currentDirectory,
71+ openFSObject,
72+ appRef,
73+ } : MainContentProps ) {
6374 const [ selected , setSelected ] = useState < string > ( "" ) ;
6475 const clickPosition = useRef < { x : number ; y : number } > ( { x : 0 , y : 0 } ) ;
65- const [ open , setOpen ] = useState ( false ) ;
76+ const [ contextMenuOpen , setContextMenuOpen ] = useState ( false ) ;
77+ const [ promptFor , setPromptFor ] = useState < FSObjectType | null > ( null ) ;
78+ const fs = useLocalFS ( ) ;
6679
6780 function handleRightClick ( e : React . MouseEvent ) {
6881 clickPosition . current = { x : e . clientX , y : e . clientY } ;
6982 e . stopPropagation ( ) ;
7083 e . preventDefault ( ) ;
71- setOpen ( true ) ;
84+ setContextMenuOpen ( true ) ;
7285 }
7386
7487 return (
7588 < div
7689 className = "file-browser__main-content"
7790 onContextMenu = { handleRightClick }
7891 >
79- { open && (
92+ { promptFor && (
93+ < FullAppPrompt
94+ fieldName = "Name"
95+ promptName = { `Create ${ promptFor } ` }
96+ appRef = { appRef }
97+ close = { ( ) => setPromptFor ( null ) }
98+ submit = { ( value ) => fs . create ( promptFor , value , currentDirectory ) }
99+ validate = { fs . validateFSObjectName }
100+ />
101+ ) }
102+ { contextMenuOpen && (
80103 < ContextMenu
81104 position = { clickPosition . current }
82- items = { getMainContentContextItems ( ) }
83- close = { ( ) => setOpen ( ! open ) }
105+ items = { getMainContentContextItems ( setPromptFor , setContextMenuOpen ) }
106+ close = { ( ) => setContextMenuOpen ( ! contextMenuOpen ) }
84107 />
85108 ) }
86109 { Object . values < FSObject > ( currentDirectory . contents ) . map ( ( fsObject ) => (
@@ -133,6 +156,7 @@ function DirectoryOrFile({
133156
134157function FileBrowser ( { path = defaultPath } : FileBrowserProps ) {
135158 const fs = useLocalFSWithHistory ( path ) ;
159+ const appRef = useRef < HTMLDivElement > ( null ) ;
136160
137161 const [ pathSearch , setPathSearch ] = useState < string > (
138162 fs . currentDirectory . path
@@ -153,7 +177,7 @@ function FileBrowser({ path = defaultPath }: FileBrowserProps) {
153177 }
154178
155179 return (
156- < div className = "file-browser" >
180+ < div className = "file-browser" ref = { appRef } >
157181 < TopBar
158182 pathSearch = { pathSearch }
159183 onPathInputChange = { onPathInputChange }
@@ -173,6 +197,7 @@ function FileBrowser({ path = defaultPath }: FileBrowserProps) {
173197 < MainContent
174198 currentDirectory = { fs . currentDirectory }
175199 openFSObject = { fs . navToObject }
200+ appRef = { appRef }
176201 />
177202 < div className = "file-browser__bottom-bar" > </ div >
178203 </ div >
0 commit comments