@@ -17,6 +17,7 @@ import { |
17 | 17 | buildDiffFileTreeUpdates, |
18 | 18 | compareDiffFileTreeEntries, |
19 | 19 | collectDirectoryPaths, |
| 20 | + diffFileTreeModel, |
20 | 21 | diffFileTreePositions, |
21 | 22 | type DiffFileTreeEntry, |
22 | 23 | } from "./diffFileTree.logic"; |
@@ -46,8 +47,10 @@ export function DiffFileTree({ |
46 | 47 | }: DiffFileTreeProps) { |
47 | 48 | const { resolvedTheme } = useTheme(); |
48 | 49 | 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); |
51 | 54 | const [ordering] = useState(() => { |
52 | 55 | let currentPositions: ReadonlyMap<string, number> = new Map(); |
53 | 56 | return { |
@@ -57,19 +60,28 @@ export function DiffFileTree({ |
57 | 60 | }, |
58 | 61 | }; |
59 | 62 | }); |
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, |
62 | 65 | status: entry.status, |
63 | 66 | })); |
64 | 67 | 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); |
66 | 73 | const mountedPathsRef = useRef<ReadonlyArray<string> | null>(null); |
67 | 74 |
|
68 | 75 | // The tree model keeps the first selection handler it was given. |
69 | 76 | const handleSelectionChange = useStableCallback((selectedPaths: ReadonlyArray<string>) => { |
70 | 77 | 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)); |
73 | 85 | }); |
74 | 86 |
|
75 | 87 | const { model } = useFileTree({ |
@@ -90,64 +102,66 @@ export function DiffFileTree({ |
90 | 102 | useEffect(() => { |
91 | 103 | ordering.update(positions); |
92 | 104 | const mountedPaths = mountedPathsRef.current; |
93 | | - if (mountedPaths === paths) return; |
94 | | - mountedPathsRef.current = paths; |
| 105 | + if (mountedPaths === modelPaths) return; |
| 106 | + mountedPathsRef.current = modelPaths; |
95 | 107 | 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); |
99 | 111 | if (updates.length > 0) model.batch(updates); |
100 | 112 | } 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. |
103 | 116 | const collapsedDirectories = directoryPaths.filter((path) => { |
104 | 117 | const directory = model.getItem(path); |
105 | 118 | return directory !== null && "isExpanded" in directory && !directory.isExpanded(); |
106 | 119 | }); |
107 | | - model.resetPaths(paths); |
| 120 | + model.resetPaths(modelPaths); |
108 | 121 | for (const path of collapsedDirectories) { |
109 | 122 | const directory = model.getItem(path); |
110 | 123 | if (directory !== null && "collapse" in directory) directory.collapse(); |
111 | 124 | } |
112 | 125 | } |
113 | 126 | model.setGitStatus(gitStatus); |
114 | | - }, [directoryPaths, gitStatus, model, ordering, paths, positions]); |
| 127 | + }, [directoryPaths, gitStatus, model, modelPaths, ordering, positions]); |
115 | 128 |
|
116 | 129 | useEffect(() => { |
117 | 130 | if (selectedPath === null) { |
118 | 131 | handledRevealRef.current = null; |
119 | 132 | return; |
120 | 133 | } |
121 | | - const item = model.getItem(selectedPath); |
| 134 | + const modelPath = presented.modelPath(selectedPath); |
| 135 | + const item = model.getItem(modelPath); |
122 | 136 | if (item === null || item.isDirectory()) { |
123 | 137 | handledRevealRef.current = null; |
124 | 138 | return; |
125 | 139 | } |
126 | 140 | 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 }; |
129 | 149 | syncingSelectionRef.current = true; |
130 | 150 | for (const path of model.getSelectedPaths()) { |
131 | | - if (path !== selectedPath) model.getItem(path)?.deselect(); |
| 151 | + if (path !== modelPath) model.getItem(path)?.deselect(); |
132 | 152 | } |
133 | 153 | let ancestor = ""; |
134 | | - for (const segment of selectedPath.split("/").slice(0, -1)) { |
| 154 | + for (const segment of modelPath.split("/").slice(0, -1)) { |
135 | 155 | ancestor += `${segment}/`; |
136 | 156 | const directory = model.getItem(ancestor); |
137 | 157 | if (directory !== null && "expand" in directory) directory.expand(); |
138 | 158 | } |
139 | 159 | item.select(); |
140 | | - model.scrollToPath(selectedPath, { offset: "nearest" }); |
| 160 | + model.scrollToPath(modelPath, { offset: "nearest" }); |
141 | 161 | queueMicrotask(() => { |
142 | 162 | syncingSelectionRef.current = false; |
143 | 163 | }); |
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]); |
151 | 165 |
|
152 | 166 | return ( |
153 | 167 | <div className={cn("flex min-h-0 flex-1 flex-col bg-background", className)}> |
@@ -203,14 +217,16 @@ export function DiffFileTree({ |
203 | 217 | // Pierre does not emit a selection change for its sole selected row. |
204 | 218 | // Read selection before the row handles the click so new selections reveal only once. |
205 | 219 | 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; |
208 | 224 | const clickedSelectedRow = event.nativeEvent |
209 | 225 | .composedPath() |
210 | 226 | .some( |
211 | 227 | (node) => node instanceof HTMLElement && node.getAttribute("data-item-path") === path, |
212 | 228 | ); |
213 | | - if (clickedSelectedRow) onSelectFile(path); |
| 229 | + if (clickedSelectedRow) onSelectFile(presented.selectionPath(path)); |
214 | 230 | }} |
215 | 231 | className="min-h-0 flex-1 overflow-hidden" |
216 | 232 | style={pierreTreeStyle(resolvedTheme)} |
|