@@ -20,53 +20,49 @@ export type FSObject = FSDirectory | FSFile;
2020export type FSObjectType = FSObject [ "type" ] ;
2121
2222const pathSeparator = "/" ;
23+ const rootDirPath = "/" ;
24+ const homeDirPath = rootDirPath + "home" ;
25+ const userDirPath = [ homeDirPath , "user" ] . join ( pathSeparator ) ;
26+ const documentsDirPath = [ userDirPath , "Documents" ] . join ( pathSeparator ) ;
27+ const picturesDirPath = [ userDirPath , "Pictures" ] . join ( pathSeparator ) ;
28+ const downloadsDirPath = [ userDirPath , "Downloads" ] . join ( pathSeparator ) ;
29+ const musicDirPath = [ userDirPath , "Music" ] . join ( pathSeparator ) ;
30+ const videosDirPath = [ userDirPath , "Videos" ] . join ( pathSeparator ) ;
2331
2432const documentsDir : FSDirectory = {
2533 name : "Documents" ,
26- get path ( ) {
27- return [ userDir . path , this . name ] . join ( pathSeparator ) ;
28- } ,
34+ path : documentsDirPath ,
2935 type : "directory" ,
3036 contents : { } ,
3137} ;
3238const picturesDir : FSDirectory = {
3339 name : "Pictures" ,
34- get path ( ) {
35- return [ userDir . path , this . name ] . join ( pathSeparator ) ;
36- } ,
40+ path : picturesDirPath ,
3741 type : "directory" ,
3842 contents : { } ,
3943} ;
4044const downloadsDir : FSDirectory = {
4145 name : "Downloads" ,
42- get path ( ) {
43- return [ userDir . path , this . name ] . join ( pathSeparator ) ;
44- } ,
46+ path : downloadsDirPath ,
4547 type : "directory" ,
4648 contents : { } ,
4749} ;
4850const musicDir : FSDirectory = {
4951 name : "Music" ,
50- get path ( ) {
51- return [ userDir . path , this . name ] . join ( pathSeparator ) ;
52- } ,
52+ path : musicDirPath ,
5353 type : "directory" ,
5454 contents : { } ,
5555} ;
5656const videosDir : FSDirectory = {
5757 name : "Videos" ,
58- get path ( ) {
59- return [ userDir . path , this . name ] . join ( pathSeparator ) ;
60- } ,
58+ path : videosDirPath ,
6159 type : "directory" ,
6260 contents : { } ,
6361} ;
6462
6563const userDir : FSDirectory = {
6664 name : "user" ,
67- get path ( ) {
68- return [ homeDir . path , this . name ] . join ( pathSeparator ) ;
69- } ,
65+ path : userDirPath ,
7066 type : "directory" ,
7167 contents : {
7268 [ documentsDir . name ] : documentsDir ,
@@ -79,9 +75,7 @@ const userDir: FSDirectory = {
7975
8076const homeDir : FSDirectory = {
8177 name : "home" ,
82- get path ( ) {
83- return pathSeparator + this . name ;
84- } ,
78+ path : homeDirPath ,
8579 type : "directory" ,
8680 contents : { [ userDir . name ] : userDir } ,
8781} ;
@@ -113,7 +107,7 @@ export interface LocalFSState {
113107 parentDirectory : FSDirectory ,
114108 contents ?: string
115109 ) => FSFile | null ;
116- favorites : Array < { path : string ; name : string } > ;
110+ favorites : Array < string > ;
117111 validateFSObjectName : ( name : string ) => string | null ;
118112 fsObjectNameIsAvailable : ( name : string , directory : FSDirectory ) => boolean ;
119113 create : (
@@ -278,24 +272,39 @@ export const useLocalFS = create<LocalFSState>()(
278272 newName
279273 ) => {
280274 const oldName = fsObject . name ;
275+ const oldPath = fsObject . path ;
276+
277+ // Update the FSObject to have the new name
281278 fsObject . name = newName ;
282279 fsObject . path = [
283280 ...fsObject . path . split ( pathSeparator ) . slice ( 0 , - 1 ) ,
284281 newName ,
285282 ] . join ( pathSeparator ) ;
286- parentDirectory . contents [ newName ] = fsObject ;
283+
284+ // Delete the old one from the parent
287285 delete parentDirectory . contents [ oldName ] ;
288286
289- set ( { root : get ( ) . root } ) ;
287+ // Update the parent so it knows about the rename
288+ parentDirectory . contents [ newName ] = fsObject ;
289+
290+ // If the FSObject is in the favorites, update that too.
291+ const favorites = get ( ) . favorites ;
292+ const favIndex = favorites . findIndex ( ( fav ) => fav === oldPath ) ;
293+ if ( favIndex ) {
294+ favorites [ favIndex ] = fsObject . path ;
295+ }
296+
297+ set ( { root : get ( ) . root , favorites } ) ;
290298 } ;
291299 return {
292300 root : rootDir ,
293301 favorites : [
294- { name : userDir . name , path : userDir . path } ,
295- { name : documentsDir . name , path : documentsDir . path } ,
296- { name : downloadsDir . name , path : downloadsDir . path } ,
297- { name : musicDir . name , path : musicDir . path } ,
298- { name : videosDir . name , path : videosDir . path } ,
302+ userDir . path ,
303+ documentsDir . path ,
304+ downloadsDir . path ,
305+ musicDir . path ,
306+ picturesDir . path ,
307+ videosDir . path ,
299308 ] ,
300309 create,
301310 createDirectory,
0 commit comments