11import type { GitStatusEntry } from "@pierre/trees" ;
22import { FileTree , useFileTree , useFileTreeSelector } from "@pierre/trees/react" ;
33import { ChevronsDownUp , ChevronsUpDown } from "lucide" ;
4- import { useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
4+ import { useEffect , useRef , useState , type ReactNode } from "react" ;
55
6+ import { useStableCallback } from "~/hooks/useStableCallback" ;
67import { useTheme } from "~/hooks/useTheme" ;
78import { cn } from "~/lib/utils" ;
89import { T3_PIERRE_ICONS } from "~/pierre-icons" ;
@@ -44,9 +45,9 @@ export function DiffFileTree({
4445 className,
4546} : DiffFileTreeProps ) {
4647 const { resolvedTheme } = useTheme ( ) ;
47- const paths = useMemo ( ( ) => entries . map ( ( entry ) => entry . path ) , [ entries ] ) ;
48- const directoryPaths = useMemo ( ( ) => collectDirectoryPaths ( paths ) , [ paths ] ) ;
49- const positions = useMemo ( ( ) => diffFileTreePositions ( paths ) , [ paths ] ) ;
48+ const paths = entries . map ( ( entry ) => entry . path ) ;
49+ const directoryPaths = collectDirectoryPaths ( paths ) ;
50+ const positions = diffFileTreePositions ( paths ) ;
5051 const [ ordering ] = useState ( ( ) => {
5152 let currentPositions : ReadonlyMap < string , number > = new Map ( ) ;
5253 return {
@@ -56,31 +57,27 @@ export function DiffFileTree({
5657 } ,
5758 } ;
5859 } ) ;
59- const gitStatus = useMemo < ReadonlyArray < GitStatusEntry > > (
60- ( ) => entries . map ( ( entry ) => ( { path : entry . path , status : entry . status } ) ) ,
61- [ entries ] ,
62- ) ;
63- const filePathsRef = useRef < ReadonlySet < string > > ( new Set ( paths ) ) ;
64- const onSelectFileRef = useRef ( onSelectFile ) ;
60+ const gitStatus : ReadonlyArray < GitStatusEntry > = entries . map ( ( entry ) => ( {
61+ path : entry . path ,
62+ status : entry . status ,
63+ } ) ) ;
6564 const syncingSelectionRef = useRef ( false ) ;
6665 const handledRevealRef = useRef < { path : string ; revealRequestId : number } | null > ( null ) ;
6766 const mountedPathsRef = useRef < ReadonlyArray < string > | null > ( null ) ;
6867
69- useEffect ( ( ) => {
70- filePathsRef . current = new Set ( paths ) ;
71- onSelectFileRef . current = onSelectFile ;
72- } , [ onSelectFile , paths ] ) ;
68+ // The tree model keeps the first selection handler it was given.
69+ const handleSelectionChange = useStableCallback ( ( selectedPaths : ReadonlyArray < string > ) => {
70+ if ( syncingSelectionRef . current ) return ;
71+ const path = selectedPaths . at ( - 1 ) ?. replace ( / \/ $ / , "" ) ;
72+ if ( path && paths . includes ( path ) ) onSelectFile ( path ) ;
73+ } ) ;
7374
7475 const { model } = useFileTree ( {
7576 density : "compact" ,
7677 flattenEmptyDirectories : true ,
7778 initialExpansion : "open" ,
7879 icons : T3_PIERRE_ICONS ,
79- onSelectionChange : ( selectedPaths ) => {
80- if ( syncingSelectionRef . current ) return ;
81- const path = selectedPaths . at ( - 1 ) ?. replace ( / \/ $ / , "" ) ;
82- if ( path && filePathsRef . current . has ( path ) ) onSelectFileRef . current ( path ) ;
83- } ,
80+ onSelectionChange : handleSelectionChange ,
8481 paths : [ ] ,
8582 search : false ,
8683 sort : ordering . sort ,
@@ -144,8 +141,13 @@ export function DiffFileTree({
144141 queueMicrotask ( ( ) => {
145142 syncingSelectionRef . current = false ;
146143 } ) ;
147- // `paths` is a dependency so a file that arrives after it was asked for is still revealed.
148- } , [ model , paths , revealRequestId , selectedPath ] ) ;
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+ ] ) ;
149151
150152 return (
151153 < div className = { cn ( "flex min-h-0 flex-1 flex-col bg-background" , className ) } >
@@ -202,13 +204,13 @@ export function DiffFileTree({
202204 // Read selection before the row handles the click so new selections reveal only once.
203205 const selected = model . getSelectedPaths ( ) ;
204206 const path = selected . length === 1 ? selected [ 0 ] : undefined ;
205- if ( ! path || ! filePathsRef . current . has ( path ) ) return ;
207+ if ( ! path || ! paths . includes ( path ) ) return ;
206208 const clickedSelectedRow = event . nativeEvent
207209 . composedPath ( )
208210 . some (
209211 ( node ) => node instanceof HTMLElement && node . getAttribute ( "data-item-path" ) === path ,
210212 ) ;
211- if ( clickedSelectedRow ) onSelectFileRef . current ( path ) ;
213+ if ( clickedSelectedRow ) onSelectFile ( path ) ;
212214 } }
213215 className = "min-h-0 flex-1 overflow-hidden"
214216 style = { pierreTreeStyle ( resolvedTheme ) }
0 commit comments