11import type { GitStatusEntry } from "@pierre/trees" ;
22import { FileTree , useFileTree , useFileTreeSelector } from "@pierre/trees/react" ;
33import { ChevronsDownUpIcon , ChevronsUpDownIcon } from "lucide-react" ;
4- import { useEffect , useMemo , useRef , type ReactNode } from "react" ;
4+ import { useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
55
66import { useTheme } from "~/hooks/useTheme" ;
77import { cn } from "~/lib/utils" ;
@@ -13,7 +13,9 @@ import { Button } from "../ui/button";
1313import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
1414import {
1515 buildDiffFileTreeUpdates ,
16+ compareDiffFileTreeEntries ,
1617 collectDirectoryPaths ,
18+ diffFileTreePositions ,
1719 type DiffFileTreeEntry ,
1820} from "./diffFileTree.logic" ;
1921
@@ -54,6 +56,16 @@ export function DiffFileTree({
5456 const { resolvedTheme } = useTheme ( ) ;
5557 const paths = useMemo ( ( ) => entries . map ( ( entry ) => entry . path ) , [ entries ] ) ;
5658 const directoryPaths = useMemo ( ( ) => collectDirectoryPaths ( paths ) , [ paths ] ) ;
59+ const positions = useMemo ( ( ) => diffFileTreePositions ( paths ) , [ paths ] ) ;
60+ const [ ordering ] = useState ( ( ) => {
61+ let currentPositions : ReadonlyMap < string , number > = new Map ( ) ;
62+ return {
63+ sort : compareDiffFileTreeEntries ( ( ) => currentPositions ) ,
64+ update : ( nextPositions : ReadonlyMap < string , number > ) => {
65+ currentPositions = nextPositions ;
66+ } ,
67+ } ;
68+ } ) ;
5769 const gitStatus = useMemo < ReadonlyArray < GitStatusEntry > > (
5870 ( ) => entries . map ( ( entry ) => ( { path : entry . path , status : entry . status } ) ) ,
5971 [ entries ] ,
@@ -83,24 +95,39 @@ export function DiffFileTree({
8395 } ,
8496 paths : [ ] ,
8597 search : false ,
98+ sort : ordering . sort ,
8699 unsafeCSS : PIERRE_TREE_UNSAFE_CSS ,
87100 } ) ;
88101 const allDirectoriesExpanded = useFileTreeSelector ( model , ( currentModel ) =>
89102 areAllDirectoriesExpanded ( currentModel , directoryPaths ) ,
90103 ) ;
91104
92105 useEffect ( ( ) => {
106+ ordering . update ( positions ) ;
93107 const mountedPaths = mountedPathsRef . current ;
94108 if ( mountedPaths === paths ) return ;
95109 mountedPathsRef . current = paths ;
96110 if ( mountedPaths === null ) {
97111 model . resetPaths ( paths ) ;
98- } else {
112+ } else if ( mountedPaths . every ( ( path , index ) => paths [ index ] === path ) ) {
113+ // PR slices only append files, so keep the existing tree and its open folders.
99114 const updates = buildDiffFileTreeUpdates ( mountedPaths , paths ) ;
100115 if ( updates . length > 0 ) model . batch ( updates ) ;
116+ } else {
117+ // A refreshed diff can change the rank of existing siblings. Mutations do not reorder
118+ // those rows, so rebuild while carrying the reader's folder expansion forward.
119+ const collapsedDirectories = directoryPaths . filter ( ( path ) => {
120+ const directory = model . getItem ( path ) ;
121+ return directory !== null && "isExpanded" in directory && ! directory . isExpanded ( ) ;
122+ } ) ;
123+ model . resetPaths ( paths ) ;
124+ for ( const path of collapsedDirectories ) {
125+ const directory = model . getItem ( path ) ;
126+ if ( directory !== null && "collapse" in directory ) directory . collapse ( ) ;
127+ }
101128 }
102129 model . setGitStatus ( gitStatus ) ;
103- } , [ gitStatus , model , paths ] ) ;
130+ } , [ directoryPaths , gitStatus , model , ordering , paths , positions ] ) ;
104131
105132 useEffect ( ( ) => {
106133 if ( selectedPath === null ) {
0 commit comments