@@ -28,7 +28,13 @@ import { PIERRE_TREE_UNSAFE_CSS, pierreTreeStyle } from "~/pierre-tree-theme";
2828
2929import { joinHostPath } from "./filePath" ;
3030import { createFileTreeDragMentionController } from "./fileTreeDragMention" ;
31- import { areAllDirectoriesExpanded , setAllDirectoriesExpanded } from "./fileTreeExpansion" ;
31+ import {
32+ areAllDirectoriesExpanded ,
33+ coalesceReentrantCalls ,
34+ readExpandedDirectories ,
35+ setAllDirectoriesExpanded ,
36+ writeExpandedDirectories ,
37+ } from "./fileTreeExpansion" ;
3238import { buildFileTreePathUpdates } from "./fileTreePathReconciliation" ;
3339import { loadFileAncestors } from "./loadFileAncestors" ;
3440import { useDirectoryEntries } from "./useDirectoryEntries" ;
@@ -344,30 +350,45 @@ export default function FileBrowserPanel({
344350 return subscribePreviewAction ( onPanelAction ) ;
345351 } , [ answersPanelFind ] ) ;
346352 const expandedPathsRef = useRef ( new Set < string > ( ) ) ;
353+ const expansionRoot = `${ environmentId } :${ cwd } ` ;
354+ // Folders expanded when the panel last unmounted; each reopens once lazy loading lists it.
355+ const [ pendingExpandedPaths ] = useState ( ( ) => new Set ( readExpandedDirectories ( expansionRoot ) ) ) ;
347356 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the Pierre tree model to load directories as they are expanded
348357 useEffect ( ( ) => {
349358 const currentPaths = new Set ( directoryPaths ) ;
350359 for ( const path of expandedPathsRef . current ) {
351360 if ( ! currentPaths . has ( path ) ) expandedPathsRef . current . delete ( path ) ;
352361 }
353- const loadExpanded = ( ) => {
362+ // Restoring calls item.expand(), which notifies subscribers synchronously.
363+ const loadExpanded = coalesceReentrantCalls ( ( ) => {
354364 if ( model . isSearchOpen ( ) ) return ;
365+ let changed = false ;
355366 for ( const path of directoryPaths ) {
356367 const item = model . getItem ( path ) ;
368+ if ( item ?. isDirectory ( ) && pendingExpandedPaths . delete ( path ) && "expand" in item ) {
369+ item . expand ( ) ;
370+ }
357371 if ( item ?. isDirectory ( ) && "isExpanded" in item && item . isExpanded ( ) ) {
358372 if ( ! expandedPathsRef . current . has ( path ) ) {
359373 expandedPathsRef . current . add ( path ) ;
374+ changed = true ;
360375 void load ( path . replace ( / \/ $ / , "" ) ) ;
361376 }
362377 } else {
363378 if ( item ?. isDirectory ( ) && expandedPathsRef . current . has ( path ) ) setExpandAll ( false ) ;
364- expandedPathsRef . current . delete ( path ) ;
379+ if ( expandedPathsRef . current . delete ( path ) ) changed = true ;
365380 }
366381 }
367- } ;
382+ if ( changed ) {
383+ writeExpandedDirectories ( expansionRoot , [
384+ ...expandedPathsRef . current ,
385+ ...pendingExpandedPaths ,
386+ ] ) ;
387+ }
388+ } ) ;
368389 loadExpanded ( ) ;
369390 return model . subscribe ( loadExpanded ) ;
370- } , [ directoryPaths , load , model ] ) ;
391+ } , [ directoryPaths , expansionRoot , load , model , pendingExpandedPaths ] ) ;
371392 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes ignored-file status into the imperative Pierre tree model
372393 useEffect ( ( ) => {
373394 model . setGitStatus (
0 commit comments