@@ -276,19 +276,38 @@ export const useLocalFS = create<LocalFSState>()(
276
276
) => {
277
277
const oldName = fsObject . name ;
278
278
const oldPath = fsObject . path ;
279
-
280
- // Update the FSObject to have the new name
281
- fsObject . name = newName ;
282
- fsObject . path = [
279
+ const newPath = [
283
280
...fsObject . path . split ( pathSeparator ) . slice ( 0 , - 1 ) ,
284
281
newName ,
285
282
] . join ( pathSeparator ) ;
286
283
287
- // Delete the old one from the parent
288
- delete parentDirectory . contents [ oldName ] ;
289
-
290
284
// Update the parent so it knows about the rename
291
285
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 ) ;
292
311
293
312
// If the FSObject is in the favorites, update that too.
294
313
const favorites = get ( ) . favorites ;
0 commit comments