@@ -276,19 +276,38 @@ export const useLocalFS = create<LocalFSState>()(
276276 ) => {
277277 const oldName = fsObject . name ;
278278 const oldPath = fsObject . path ;
279-
280- // Update the FSObject to have the new name
281- fsObject . name = newName ;
282- fsObject . path = [
279+ const newPath = [
283280 ...fsObject . path . split ( pathSeparator ) . slice ( 0 , - 1 ) ,
284281 newName ,
285282 ] . join ( pathSeparator ) ;
286283
287- // Delete the old one from the parent
288- delete parentDirectory . contents [ oldName ] ;
289-
290284 // Update the parent so it knows about the rename
291285 parentDirectory . contents [ newName ] = fsObject ;
286+ delete parentDirectory . contents [ oldName ] ;
287+
288+ // Update the FSObject to have the new name
289+ fsObject . name = newName ;
290+
291+ // The path is also stored on each of the children,
292+ // so we need to update the renamed dir on them to.
293+ // This isnt ideal, as we'll going to have to loop over
294+ // them all recursively. A better way to do this might be to not
295+ // store the path on each FSObject and instead store a reference to
296+ // the parent FSDirectory, but this would very tricky to persist
297+ function updatePathRecursive ( fsObject : FSObject ) {
298+ console . log ( "Updating" , fsObject . path ) ;
299+ const regex = new RegExp ( `^${ oldPath } ` ) ;
300+ fsObject . path = fsObject . path . replace ( regex , newPath ) ;
301+ console . log ( "Updated to" , fsObject . path ) ;
302+
303+ if ( ! isFSDirectory ( fsObject ) ) return ;
304+
305+ for ( const child of Object . values < FSObject > ( fsObject . contents ) ) {
306+ updatePathRecursive ( child ) ;
307+ }
308+ }
309+
310+ updatePathRecursive ( fsObject ) ;
292311
293312 // If the FSObject is in the favorites, update that too.
294313 const favorites = get ( ) . favorites ;
0 commit comments