1- import { useInstanceClientIdParams } from '@/config/useInstanceClient' ;
21import { buildItems } from '@/features/instance/applications/components/ApplicationsSidebar/buildItems' ;
32import {
43 DirectoryIcon ,
@@ -8,19 +7,15 @@ import type { DirectoryEntry } from '@/features/instance/applications/context/di
87import type { FileEntry } from '@/features/instance/applications/context/fileEntry' ;
98import { isDirectory } from '@/features/instance/applications/context/isDirectory' ;
109import { useEditorView } from '@/features/instance/applications/hooks/useEditorView' ;
11- import { dropComponent } from '@/features/instance/operations/mutations/dropComponent' ;
12- import { setComponentFile } from '@/features/instance/operations/mutations/setComponentFile' ;
13- import { getComponentFile } from '@/features/instance/operations/queries/getComponentFile' ;
10+ import { useRenameFile } from '@/features/instance/applications/hooks/useRenameFile' ;
1411import { useCallback , useEffect , useMemo } from 'react' ;
1512import { ControlledTreeEnvironment , Tree , TreeItem } from 'react-complex-tree' ;
1613import './file-explorer-modern.css' ;
17- import { toast } from 'sonner' ;
1814import { FileTypeIcon } from './FileTreeExplorer/FileTypeIcon' ;
1915
2016export function ApplicationsSidebar ( ) {
2117 const {
2218 rootEntries,
23- reloadRootEntries,
2419 openedEntry,
2520 setOpenedEntry,
2621 focusedItem,
@@ -31,7 +26,6 @@ export function ApplicationsSidebar() {
3126 setSelectedItems,
3227 } = useEditorView ( ) ;
3328
34- const instanceParams = useInstanceClientIdParams ( ) ;
3529 const { items, rootId } = useMemo ( ( ) => buildItems ( rootEntries ) , [ rootEntries ] ) ;
3630
3731 useEffect ( function setOpenedEntryFromFocusedItem ( ) {
@@ -60,75 +54,8 @@ export function ApplicationsSidebar() {
6054 // TODO: on drop file or folder from outside the editor to upload stuff rapidly
6155 // TODO: open file after creating it
6256 // TODO: select folder after creating it
63-
64- const onRenameItem = useCallback ( async ( item : TreeItem < FileEntry | DirectoryEntry | null > , name : string ) => {
65- if ( item . data && item . data ?. name !== name ) {
66- const oldFile = item . data . path . split ( '/' ) . slice ( 1 ) . join ( '/' ) ;
67- const newFile = item . data . path . split ( '/' ) . slice ( 1 , - 1 ) . join ( '/' ) + '/' + name ;
68-
69- const toastId = toast . loading ( 'Renaming' , {
70- description : 'Loading existing contents...' ,
71- duration : 0 ,
72- } ) ;
73- const fileContents = await getComponentFile ( {
74- ...instanceParams ,
75- file : oldFile ,
76- project : item . data . project ,
77- } ) ;
78-
79- toast . loading ( 'Renaming' , {
80- id : toastId ,
81- description : 'Copying to new name...' ,
82- duration : 0 ,
83- } ) ;
84- await setComponentFile ( {
85- ...instanceParams ,
86- file : newFile ,
87- project : item . data . project ,
88- payload : fileContents . message ,
89- } ) ;
90-
91- toast . loading ( 'Renaming' , {
92- id : toastId ,
93- description : 'Removing old copy...' ,
94- duration : 0 ,
95- } ) ;
96- await dropComponent ( {
97- ...instanceParams ,
98- file : oldFile ,
99- project : item . data . project ,
100- } ) ;
101- toast . success ( 'Renamed!' , {
102- id : toastId ,
103- description : 'All done!' ,
104- duration : 3000 ,
105- } ) ;
106-
107- reloadRootEntries ( ) ;
108-
109- const existingIndex = item . index ;
110- const newIndex = ( item . index as string ) . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) + '/' + name ;
111-
112- setSelectedItems ( selectedItems => {
113- const selectedIndex = selectedItems . indexOf ( existingIndex ) ;
114- if ( selectedIndex >= 0 ) {
115- return [
116- ...selectedItems . slice ( 0 , selectedIndex ) ,
117- ...selectedItems . slice ( selectedIndex + 1 ) ,
118- newIndex ,
119- ] ;
120- }
121- return selectedItems ;
122- } ) ;
123-
124- setFocusedItem ( focusedItem => {
125- if ( focusedItem === existingIndex ) {
126- return newIndex ;
127- }
128- return focusedItem ;
129- } ) ;
130- }
131- } , [ openedEntry ] ) ;
57+ const renameFile = useRenameFile ( ) ;
58+ const onRenameItem = useCallback ( ( item : TreeItem < FileEntry | DirectoryEntry | null > , name : string ) => renameFile ( item . data , name ) , [ renameFile ] ) ;
13259
13360 return (
13461 < div className = "h-full overflow-auto pr-1.5" >
0 commit comments