@@ -119,6 +119,36 @@ export default function FileManager() {
119119 } ) ;
120120 } ;
121121
122+ // Handle URL changes from browser back/forward buttons
123+ useEffect ( ( ) => {
124+ if ( isLoadingStorages || storages . length === 0 || ! isInitialized ) {
125+ return ;
126+ }
127+
128+ const urlParam = getUrlFromSearchParams ( ) ;
129+
130+ if ( ! urlParam ) {
131+ // No URL in params - reset to root if we have a storage selected
132+ if ( selectedStorageId ) {
133+ const storage = storages . find ( ( s ) => s . id === selectedStorageId ) ;
134+ if ( storage ) {
135+ setCurrentPath ( "/" ) ;
136+ removeUrlFromStorage ( ) ;
137+ }
138+ }
139+ return ;
140+ }
141+
142+ // URL changed - update state to match
143+ const matchingStorage = storages . find ( ( s ) => urlParam === s . url || urlParam . startsWith ( s . url ) ) ;
144+
145+ if ( matchingStorage ) {
146+ setSelectedStorageId ( matchingStorage . id ) ;
147+ setCurrentPath ( urlParam === matchingStorage . url ? "/" : urlParam ) ;
148+ saveUrlToStorage ( urlParam ) ;
149+ }
150+ } , [ searchParams , storages , isLoadingStorages , isInitialized , selectedStorageId ] ) ;
151+
122152 useEffect ( ( ) => {
123153 if ( isLoadingStorages || storages . length === 0 || isInitialized ) {
124154 return ;
@@ -187,19 +217,28 @@ export default function FileManager() {
187217 } ;
188218 } , [ contextMenuState ] ) ;
189219
190- const updateUrl = ( url : string | null ) => {
220+ const updateUrl = ( url : string | null , addToHistory : boolean = true ) => {
191221 if ( ! url || url === "/" ) {
192222 removeUrlFromStorage ( ) ;
193223 if ( typeof window !== "undefined" && window . location . search ) {
194- router . replace ( "/" , { scroll : false } ) ;
224+ if ( addToHistory ) {
225+ router . push ( "/" , { scroll : false } ) ;
226+ } else {
227+ router . replace ( "/" , { scroll : false } ) ;
228+ }
195229 }
196230 return ;
197231 }
198232
199233 const params = new URLSearchParams ( ) ;
200234 params . set ( "url" , safeEncodeUrl ( url ) ) ;
201235 saveUrlToStorage ( url ) ;
202- router . replace ( `/?${ params . toString ( ) } ` , { scroll : false } ) ;
236+
237+ if ( addToHistory ) {
238+ router . push ( `/?${ params . toString ( ) } ` , { scroll : false } ) ;
239+ } else {
240+ router . replace ( `/?${ params . toString ( ) } ` , { scroll : false } ) ;
241+ }
203242 } ;
204243
205244 const containerUrlToBrowse = selectedStorageId
@@ -273,7 +312,7 @@ export default function FileManager() {
273312
274313 if ( fileToRename && currentPath === fileToRename . url ) {
275314 setCurrentPath ( newUrl ) ;
276- updateUrl ( newUrl ) ;
315+ updateUrl ( newUrl , false ) ;
277316 }
278317 // Trigger refresh to update file list immediately
279318 setRefreshKey ( ( prev ) => prev + 1 ) ;
@@ -600,11 +639,11 @@ export default function FileManager() {
600639 setSelectedStorageId ( file . id ) ;
601640 setCurrentPath ( "/" ) ;
602641 setSelectedFileIds ( [ ] ) ;
603- updateUrl ( file . url ) ;
642+ updateUrl ( file . url , true ) ;
604643 } else if ( selectedStorageId ) {
605644 setCurrentPath ( file . url ) ;
606645 setSelectedFileIds ( [ ] ) ;
607- updateUrl ( file . url ) ;
646+ updateUrl ( file . url , true ) ;
608647 }
609648 } else {
610649 window . open ( file . url , "_blank" ) ;
@@ -725,15 +764,15 @@ export default function FileManager() {
725764 setSelectedStorageId ( null ) ;
726765 setCurrentPath ( "/" ) ;
727766 setSelectedFileIds ( [ ] ) ;
728- updateUrl ( null ) ;
767+ updateUrl ( null , true ) ;
729768 } else {
730769 const selectedStorage = storages . find ( ( s ) => s . id === selectedStorageId ) ;
731770 if ( selectedStorage && path === selectedStorage . url ) {
732771 setCurrentPath ( "/" ) ;
733- updateUrl ( selectedStorage . url ) ;
772+ updateUrl ( selectedStorage . url , true ) ;
734773 } else {
735774 setCurrentPath ( path ) ;
736- updateUrl ( path ) ;
775+ updateUrl ( path , true ) ;
737776 }
738777 setSelectedFileIds ( [ ] ) ;
739778 }
@@ -907,8 +946,7 @@ export default function FileManager() {
907946 resourceUrl = { sharedResourceUrl }
908947 resourceName = { sharedResourceName }
909948 onOpenInApp = { ( url ) => {
910- // Navigate to the resource URL in the file manager
911- updateUrl ( url ) ;
949+ updateUrl ( url , true ) ;
912950 } }
913951 />
914952 { isDragActive && (
0 commit comments