@@ -122,14 +122,6 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
122122 ) ;
123123} ) ;
124124
125- // Building the tree walks every file, and the compiler would redo it each render
126- // because the expansion state hook runs after it.
127- function useTurnDiffTree ( files : ReadonlyArray < TurnDiffFileChange > ) {
128- "use memo" ;
129- const treeNodes = buildTurnDiffTree ( files ) ;
130- return { treeNodes, directoryPathsKey : collectDirectoryPaths ( treeNodes ) . join ( "\u0000" ) } ;
131- }
132-
133125export const ChangedFilesTree = memo ( function ChangedFilesTree ( props : {
134126 runId : RunId ;
135127 files : ReadonlyArray < TurnDiffFileChange > ;
@@ -140,16 +132,17 @@ export const ChangedFilesTree = memo(function ChangedFilesTree(props: {
140132} ) {
141133 const { files, allDirectoriesExpanded, onOpenTurnDiff, resolvedTheme, runId, onFileContextMenu } =
142134 props ;
143- const { treeNodes , directoryPathsKey } = useTurnDiffTree ( files ) ;
144- const hasDirectoryNodes = directoryPathsKey . length > 0 ;
145- const expansionStateKey = ` ${ allDirectoriesExpanded ? "expanded" : "collapsed" } \u0000 ${ directoryPathsKey } ` ;
135+ // Overrides apply only while their key matches the current one, so starting from no key is
136+ // the same as starting with no overrides. That lets this hook run before the tree is built,
137+ // and the compiler caches the tree on `files`.
146138 const [ directoryExpansionState , setDirectoryExpansionState ] = useState < {
147- key : string ;
139+ key : string | null ;
148140 overrides : Record < string , boolean > ;
149- } > ( ( ) => ( {
150- key : expansionStateKey ,
151- overrides : { } ,
152- } ) ) ;
141+ } > ( { key : null , overrides : { } } ) ;
142+ const treeNodes = buildTurnDiffTree ( files ) ;
143+ const directoryPathsKey = collectDirectoryPaths ( treeNodes ) . join ( "\u0000" ) ;
144+ const hasDirectoryNodes = directoryPathsKey . length > 0 ;
145+ const expansionStateKey = `${ allDirectoriesExpanded ? "expanded" : "collapsed" } \u0000${ directoryPathsKey } ` ;
153146 const expandedDirectories =
154147 directoryExpansionState . key === expansionStateKey
155148 ? directoryExpansionState . overrides
0 commit comments