@@ -17,6 +17,7 @@ import {
1717 buildDiffFileTreeUpdates ,
1818 compareDiffFileTreeEntries ,
1919 collectDirectoryPaths ,
20+ diffFileTreeModel ,
2021 diffFileTreePositions ,
2122 type DiffFileTreeEntry ,
2223} from "./diffFileTree.logic" ;
@@ -46,8 +47,10 @@ export function DiffFileTree({
4647} : DiffFileTreeProps ) {
4748 const { resolvedTheme } = useTheme ( ) ;
4849 const paths = entries . map ( ( entry ) => entry . path ) ;
49- const directoryPaths = collectDirectoryPaths ( paths ) ;
50- const positions = diffFileTreePositions ( paths ) ;
50+ const presented = diffFileTreeModel ( paths ) ;
51+ const modelPaths = presented . paths ;
52+ const directoryPaths = collectDirectoryPaths ( modelPaths ) ;
53+ const positions = diffFileTreePositions ( modelPaths ) ;
5154 const [ ordering ] = useState ( ( ) => {
5255 let currentPositions : ReadonlyMap < string , number > = new Map ( ) ;
5356 return {
@@ -57,19 +60,28 @@ export function DiffFileTree({
5760 } ,
5861 } ;
5962 } ) ;
60- const gitStatus : ReadonlyArray < GitStatusEntry > = entries . map ( ( entry ) => ( {
61- path : entry . path ,
63+ const gitStatus : ReadonlyArray < GitStatusEntry > = entries . map ( ( entry , index ) => ( {
64+ path : modelPaths [ index ] ?? entry . path ,
6265 status : entry . status ,
6366 } ) ) ;
6467 const syncingSelectionRef = useRef ( false ) ;
65- const handledRevealRef = useRef < { path : string ; revealRequestId : number } | null > ( null ) ;
68+ const handledRevealRef = useRef < {
69+ path : string ;
70+ revealRequestId : number ;
71+ modelPath : string ;
72+ } | null > ( null ) ;
6673 const mountedPathsRef = useRef < ReadonlyArray < string > | null > ( null ) ;
6774
6875 // The tree model keeps the first selection handler it was given.
6976 const handleSelectionChange = useStableCallback ( ( selectedPaths : ReadonlyArray < string > ) => {
7077 if ( syncingSelectionRef . current ) return ;
71- const path = selectedPaths . at ( - 1 ) ?. replace ( / \/ $ / , "" ) ;
72- if ( path && paths . includes ( path ) ) onSelectFile ( path ) ;
78+ const raw = selectedPaths . at ( - 1 ) ;
79+ if ( ! raw ) return ;
80+ // Directory ids end in `/`. A file that shares that directory's name is stored under a
81+ // different model path, so stripping the slash must not select it.
82+ const modelPath = modelPaths . includes ( raw ) ? raw : raw . replace ( / \/ $ / , "" ) ;
83+ if ( ! modelPaths . includes ( modelPath ) ) return ;
84+ onSelectFile ( presented . selectionPath ( modelPath ) ) ;
7385 } ) ;
7486
7587 const { model } = useFileTree ( {
@@ -90,64 +102,66 @@ export function DiffFileTree({
90102 useEffect ( ( ) => {
91103 ordering . update ( positions ) ;
92104 const mountedPaths = mountedPathsRef . current ;
93- if ( mountedPaths === paths ) return ;
94- mountedPathsRef . current = paths ;
105+ if ( mountedPaths === modelPaths ) return ;
106+ mountedPathsRef . current = modelPaths ;
95107 if ( mountedPaths === null ) {
96- model . resetPaths ( paths ) ;
97- } else if ( mountedPaths . every ( ( path , index ) => paths [ index ] === path ) ) {
98- const updates = buildDiffFileTreeUpdates ( mountedPaths , paths ) ;
108+ model . resetPaths ( modelPaths ) ;
109+ } else if ( mountedPaths . every ( ( path , index ) => modelPaths [ index ] === path ) ) {
110+ const updates = buildDiffFileTreeUpdates ( mountedPaths , modelPaths ) ;
99111 if ( updates . length > 0 ) model . batch ( updates ) ;
100112 } else {
101- // A refreshed diff can change the rank of existing siblings. Mutations do not reorder
102- // those rows, so rebuild while carrying the reader's folder expansion forward.
113+ // A refreshed diff can change the rank of existing siblings. A file that becomes a
114+ // directory prefix also changes its model path, which Pierre cannot rename in place.
115+ // Rebuild while carrying the reader's folder expansion forward.
103116 const collapsedDirectories = directoryPaths . filter ( ( path ) => {
104117 const directory = model . getItem ( path ) ;
105118 return directory !== null && "isExpanded" in directory && ! directory . isExpanded ( ) ;
106119 } ) ;
107- model . resetPaths ( paths ) ;
120+ model . resetPaths ( modelPaths ) ;
108121 for ( const path of collapsedDirectories ) {
109122 const directory = model . getItem ( path ) ;
110123 if ( directory !== null && "collapse" in directory ) directory . collapse ( ) ;
111124 }
112125 }
113126 model . setGitStatus ( gitStatus ) ;
114- } , [ directoryPaths , gitStatus , model , ordering , paths , positions ] ) ;
127+ } , [ directoryPaths , gitStatus , model , modelPaths , ordering , positions ] ) ;
115128
116129 useEffect ( ( ) => {
117130 if ( selectedPath === null ) {
118131 handledRevealRef . current = null ;
119132 return ;
120133 }
121- const item = model . getItem ( selectedPath ) ;
134+ const modelPath = presented . modelPath ( selectedPath ) ;
135+ const item = model . getItem ( modelPath ) ;
122136 if ( item === null || item . isDirectory ( ) ) {
123137 handledRevealRef . current = null ;
124138 return ;
125139 }
126140 const handled = handledRevealRef . current ;
127- if ( handled ?. path === selectedPath && handled . revealRequestId === revealRequestId ) return ;
128- handledRevealRef . current = { path : selectedPath , revealRequestId } ;
141+ if (
142+ handled ?. path === selectedPath &&
143+ handled . revealRequestId === revealRequestId &&
144+ handled . modelPath === modelPath
145+ ) {
146+ return ;
147+ }
148+ handledRevealRef . current = { path : selectedPath , revealRequestId, modelPath } ;
129149 syncingSelectionRef . current = true ;
130150 for ( const path of model . getSelectedPaths ( ) ) {
131- if ( path !== selectedPath ) model . getItem ( path ) ?. deselect ( ) ;
151+ if ( path !== modelPath ) model . getItem ( path ) ?. deselect ( ) ;
132152 }
133153 let ancestor = "" ;
134- for ( const segment of selectedPath . split ( "/" ) . slice ( 0 , - 1 ) ) {
154+ for ( const segment of modelPath . split ( "/" ) . slice ( 0 , - 1 ) ) {
135155 ancestor += `${ segment } /` ;
136156 const directory = model . getItem ( ancestor ) ;
137157 if ( directory !== null && "expand" in directory ) directory . expand ( ) ;
138158 }
139159 item . select ( ) ;
140- model . scrollToPath ( selectedPath , { offset : "nearest" } ) ;
160+ model . scrollToPath ( modelPath , { offset : "nearest" } ) ;
141161 queueMicrotask ( ( ) => {
142162 syncingSelectionRef . current = false ;
143163 } ) ;
144- } , [
145- model ,
146- // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A file that arrives after it was asked for is still revealed.
147- paths ,
148- revealRequestId ,
149- selectedPath ,
150- ] ) ;
164+ } , [ model , presented , revealRequestId , selectedPath ] ) ;
151165
152166 return (
153167 < div className = { cn ( "flex min-h-0 flex-1 flex-col bg-background" , className ) } >
@@ -203,14 +217,16 @@ export function DiffFileTree({
203217 // Pierre does not emit a selection change for its sole selected row.
204218 // Read selection before the row handles the click so new selections reveal only once.
205219 const selected = model . getSelectedPaths ( ) ;
206- const path = selected . length === 1 ? selected [ 0 ] : undefined ;
207- if ( ! path || ! paths . includes ( path ) ) return ;
220+ const raw = selected . length === 1 ? selected [ 0 ] : undefined ;
221+ if ( ! raw ) return ;
222+ const path = modelPaths . includes ( raw ) ? raw : raw . replace ( / \/ $ / , "" ) ;
223+ if ( ! modelPaths . includes ( path ) ) return ;
208224 const clickedSelectedRow = event . nativeEvent
209225 . composedPath ( )
210226 . some (
211227 ( node ) => node instanceof HTMLElement && node . getAttribute ( "data-item-path" ) === path ,
212228 ) ;
213- if ( clickedSelectedRow ) onSelectFile ( path ) ;
229+ if ( clickedSelectedRow ) onSelectFile ( presented . selectionPath ( path ) ) ;
214230 } }
215231 className = "min-h-0 flex-1 overflow-hidden"
216232 style = { pierreTreeStyle ( resolvedTheme ) }
0 commit comments