@@ -237,6 +237,7 @@ const App: React.FC = () => {
237237 const [ treeExpandedKeys , setTreeExpandedKeys ] = useState < React . Key [ ] > ( [ ] ) ;
238238 const [ treeSelectedKey , setTreeSelectedKey ] = useState < React . Key > ( '' ) ;
239239 const [ currentModulePath , setCurrentModulePath ] = useState ( '' ) ;
240+ const [ currentModule , setCurrentModule ] = useState < commonStorage . Module | null > ( null ) ;
240241 const [ renameTooltip , setRenameTooltip ] = useState ( 'Rename' ) ;
241242 const [ copyTooltip , setCopyTooltip ] = useState ( 'Copy' ) ;
242243 const [ deleteTooltip , setDeleteTooltip ] = useState ( 'Delete' ) ;
@@ -404,38 +405,47 @@ const App: React.FC = () => {
404405
405406 // When a module path has become the current module path (either by fetching
406407 // the most recent module path (soon after the app is loaded) or by the user
407- // selecting it in the tree, set the current workspace, expend the
408- // workspace node in the tree .
408+ // selecting it in the tree, set the current module, update some tooltips, and
409+ // load the module into the blockly editor .
409410 useEffect ( ( ) => {
410411 if ( ignoreEffect ( ) ) {
411412 return ;
412413 }
413- if ( currentModulePath && modules . length > 0 ) {
414- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
415- const workspacePath = commonStorage . makeModulePath ( workspaceName , workspaceName ) ;
416414
417- if ( currentModulePath === workspacePath ) {
415+ const module = ( modules . length > 0 && currentModulePath )
416+ ? commonStorage . findModule ( modules , currentModulePath )
417+ : null ;
418+ setCurrentModule ( module ) ;
419+
420+ if ( module != null ) {
421+ if ( module . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
418422 setRenameTooltip ( 'Rename Workspace' ) ;
419423 setCopyTooltip ( 'Copy Workspace' ) ;
420424 setDeleteTooltip ( 'Delete Workspace' ) ;
421- } else {
425+ } else if ( module . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
422426 setRenameTooltip ( 'Rename OpMode' ) ;
423427 setCopyTooltip ( 'Copy OpMode' ) ;
424428 setDeleteTooltip ( 'Delete OpMode' ) ;
429+ } else if ( module . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
430+ setRenameTooltip ( 'Rename Mechanism' ) ;
431+ setCopyTooltip ( 'Copy Mechanism' ) ;
432+ setDeleteTooltip ( 'Delete Mechanism' ) ;
425433 }
426434
427435 storage . saveEntry ( 'mostRecentModulePath' , currentModulePath ) ;
428436 if ( blocksEditor . current ) {
429- blocksEditor . current . loadModuleBlocks ( currentModulePath , workspacePath ) ;
437+ blocksEditor . current . loadModuleBlocks ( module ) ;
430438 }
431439 } else {
440+ setCurrentModule ( null ) ;
441+
432442 setRenameTooltip ( 'Rename' ) ;
433443 setCopyTooltip ( 'Copy' ) ;
434444 setDeleteTooltip ( 'Delete' ) ;
435445
436446 storage . saveEntry ( 'mostRecentModulePath' , '' ) ;
437447 if ( blocksEditor . current ) {
438- blocksEditor . current . loadModuleBlocks ( '' , '' ) ;
448+ blocksEditor . current . loadModuleBlocks ( null ) ;
439449 }
440450 }
441451 } , [ currentModulePath ] ) ;
@@ -540,9 +550,9 @@ const App: React.FC = () => {
540550 } ;
541551
542552 const handleNewWorkspaceNameOk = ( newWorkspaceName : string ) => {
543- const newWorkspacePath = commonStorage . makeModulePath ( newWorkspaceName , newWorkspaceName ) ;
553+ const newWorkspacePath = commonStorage . makeWorkspacePath ( newWorkspaceName ) ;
544554 if ( newWorkspaceNameModalPurpose === 'NewWorkspace' ) {
545- const workspaceContent = commonStorage . newModuleContent ( ) ;
555+ const workspaceContent = commonStorage . newWorkspaceContent ( newWorkspaceName ) ;
546556 storage . createModule (
547557 commonStorage . MODULE_TYPE_WORKSPACE , newWorkspacePath , workspaceContent ,
548558 ( success : boolean , errorMessage : string ) => {
@@ -595,10 +605,14 @@ const App: React.FC = () => {
595605 } ) ;
596606 } ;
597607
608+ // Provide a callback so the NewOpModeNameModal will know what the current
609+ // workspace name is.
598610 const getCurrentWorkspaceName = ( ) : string => {
599- return commonStorage . getWorkspaceName ( currentModulePath ) ;
611+ return currentModule ? currentModule . workspaceName : '' ;
600612 } ;
601613
614+ // Provide a callback so the NewOpModeNameModal will know what the existing
615+ // OpMode names are in the current workspace.
602616 const getOpModeNames = ( workspaceName : string ) : string [ ] => {
603617 const opModeNames : string [ ] = [ ] ;
604618 for ( const workspace of modules ) {
@@ -615,7 +629,7 @@ const App: React.FC = () => {
615629 const handleNewOpModeNameOk = ( workspaceName : string , newOpModeName : string ) => {
616630 const newOpModePath = commonStorage . makeModulePath ( workspaceName , newOpModeName ) ;
617631 if ( newOpModeNameModalPurpose === 'NewOpMode' ) {
618- const opModeContent = commonStorage . newModuleContent ( ) ;
632+ const opModeContent = commonStorage . newOpModeContent ( workspaceName , newOpModeName ) ;
619633 storage . createModule (
620634 commonStorage . MODULE_TYPE_OPMODE , newOpModePath , opModeContent ,
621635 ( success : boolean , errorMessage : string ) => {
@@ -685,61 +699,70 @@ const App: React.FC = () => {
685699
686700 const handleRenameClicked = ( ) => {
687701 checkIfBlocksWereModified ( ( ) => {
688- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
689- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
690- if ( workspaceName === moduleName ) {
702+ if ( ! currentModule ) {
703+ return ;
704+ }
705+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
691706 // This is a workspace.
692707 setNewWorkspaceNameModalPurpose ( 'RenameWorkspace' ) ;
693- setNewWorkspaceNameModalInitialValue ( workspaceName ) ;
708+ setNewWorkspaceNameModalInitialValue ( currentModule . workspaceName ) ;
694709 setNewWorkspaceNameModalIsOpen ( true ) ;
695- } else {
710+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
696711 // This is an OpMode.
697712 setNewOpModeNameModalPurpose ( 'RenameOpMode' ) ;
698- setNewOpModeNameModalInitialValue ( moduleName ) ;
713+ setNewOpModeNameModalInitialValue ( currentModule . moduleName ) ;
699714 setNewOpModeNameModalIsOpen ( true ) ;
700715 }
701716 } ) ;
702717 } ;
703718
704719 const handleCopyClicked = ( ) => {
705720 checkIfBlocksWereModified ( ( ) => {
706- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
707- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
708- if ( workspaceName === moduleName ) {
721+ if ( ! currentModule ) {
722+ return ;
723+ }
724+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
709725 // This is a workspace.
710726 setNewWorkspaceNameModalPurpose ( 'CopyWorkspace' ) ;
711- setNewWorkspaceNameModalInitialValue ( workspaceName + '_copy' ) ;
727+ setNewWorkspaceNameModalInitialValue ( currentModule . workspaceName + '_copy' ) ;
712728 setNewWorkspaceNameModalIsOpen ( true ) ;
713- } else {
729+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
714730 // This is an OpMode.
715731 setNewOpModeNameModalPurpose ( 'CopyOpMode' ) ;
716- setNewOpModeNameModalInitialValue ( moduleName + '_copy' ) ;
732+ setNewOpModeNameModalInitialValue ( currentModule . moduleName + '_copy' ) ;
717733 setNewOpModeNameModalIsOpen ( true ) ;
718734 }
719735 } ) ;
720736 } ;
721737
722738 const handleDeleteClicked = ( ) => {
723- const workspaceName = commonStorage . getWorkspaceName ( currentModulePath ) ;
724- const moduleName = commonStorage . getModuleName ( currentModulePath ) ;
725-
739+ if ( ! currentModule ) {
740+ return ;
741+ }
726742 // Show a bubble confirmation box to ask the user if they are sure.
727743 setPopconfirmTitle ( 'Are you sure?' ) ;
728- if ( workspaceName === moduleName ) {
744+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
729745 setPopconfirmDescription ( 'Press ok to delete this Workspace' ) ;
730- } else {
746+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
731747 setPopconfirmDescription ( 'Press ok to delete this OpMode' ) ;
748+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_MECHANISM ) {
749+ // TODO: delete the mechanism.
750+ return ;
732751 }
733752 // Set the function to be executed if the user clicks 'ok'.
734753 afterPopconfirmOk . current = ( ) => {
735754 setOpenPopconfirm ( false ) ;
736755 checkIfBlocksWereModified ( ( ) => {
737- if ( workspaceName === moduleName ) {
756+ if ( ! currentModule ) {
757+ return ;
758+ }
759+ if ( currentModule . moduleType == commonStorage . MODULE_TYPE_WORKSPACE ) {
738760 // This is a workspace.
739761 // Before deleting it, select another workspace, if there is one.
762+ const workspaceNameToDelete = currentModule . workspaceName ;
740763 let foundAnotherWorkspace = false ;
741764 for ( const workspace of modules ) {
742- if ( workspace . workspaceName !== workspaceName ) {
765+ if ( workspace . workspaceName !== workspaceNameToDelete ) {
743766 setCurrentModulePath ( workspace . modulePath ) ;
744767 foundAnotherWorkspace = true ;
745768 break ;
@@ -748,7 +771,7 @@ const App: React.FC = () => {
748771 if ( ! foundAnotherWorkspace ) {
749772 setCurrentModulePath ( '' ) ;
750773 }
751- storage . deleteWorkspace ( workspaceName ,
774+ storage . deleteWorkspace ( workspaceNameToDelete ,
752775 ( success : boolean , errorMessage : string ) => {
753776 if ( success ) {
754777 setTriggerListModules ( ! triggerListModules ) ;
@@ -757,11 +780,12 @@ const App: React.FC = () => {
757780 setAlertErrorVisible ( true ) ;
758781 }
759782 } ) ;
760- } else {
783+ } else if ( currentModule . moduleType == commonStorage . MODULE_TYPE_OPMODE ) {
761784 // This is an OpMode.
762- const workspacePath = commonStorage . makeModulePath ( workspaceName , workspaceName ) ;
785+ const modulePathToDelete = currentModulePath ;
786+ const workspacePath = commonStorage . makeWorkspacePath ( currentModule . workspaceName ) ;
763787 setCurrentModulePath ( workspacePath ) ;
764- storage . deleteOpMode ( currentModulePath ,
788+ storage . deleteOpMode ( modulePathToDelete ,
765789 ( success : boolean , errorMessage : string ) => {
766790 if ( success ) {
767791 setTriggerListModules ( ! triggerListModules ) ;
0 commit comments