fix(github): open the selected file with Show Diff in the pull request trees (#7623)

Show Diff (Ctrl+D) on a file in the pull request Changes tree said "Select a changed file to show its diff": the diff actions ask the focused view for its selection through useDiffSource, and neither the pull request details tree nor the New Pull Request changes tree registered one. Both now do: the first selected file, else the tree's first file (a folder selection opens its first file), with the New Pull Request tree keeping its file chain for F7.

Closes #7614

Co-authored-by: og-orca <[email protected]>
Цей коміт міститься в:
Orca authored and GitHub committed 2026-12-03 10:32:59 +08:00
1 джерело 2f3f08efde
коміт 98a6de6ff0
5 files changed
+256 -62

No files matched your search

+43 -33
Переглянути файл
@@ -7,6 +7,8 @@
import { useMemo, useRef, useState, type MouseEvent, type ReactNode } from "react";
import { buildFileTree, dirKeys, type FileTreeNode } from "@/features/vcs/fileTree";
import { openDiff, withChain } from "@/features/vcs/diffInput";
import { useDiffSource } from "@/features/vcs/diffSource";
import { filesUnder } from "@/features/vcs/logChanges";
import { KIND_COLOR, fileCount, renameFrom, type EffectiveKind } from "@/features/vcs/model";
import type { Commit } from "@/platform/ipc";
import { tx } from "@/platform/i18n";
@@ -160,16 +162,18 @@ function CommitDetails({ commit }: { commit: Commit }) {
const kindOf = (c: CreateChange): EffectiveKind => (c.kind === "untracked" ? "added" : c.kind);
/** Show Diff: `first` with every file of the tree in the chain (F7 moves along). */
function showDiff(all: readonly CreateChange[], first: CreateChange) {
void openDiff(
withChain(
all.map((c) => c.diff),
Math.max(0, all.indexOf(first)),
),
/** `first`'s diff with every file of the tree in the chain (F7 moves along). */
function chainedDiff(all: readonly CreateChange[], first: CreateChange) {
return withChain(
all.map((c) => c.diff),
Math.max(0, all.indexOf(first)),
);
}
function showDiff(all: readonly CreateChange[], first: CreateChange) {
void openDiff(chainedDiff(all, first));
}
function ChangesTree() {
const rowHeight = useRowHeight(22);
const changes = useCreatePr((s) => s.changes);
@@ -184,6 +188,10 @@ function ChangesTree() {
return out;
}, [roots]);
const [selection, setSelection] = useState<ReadonlySet<string>>(new Set());
// Show Diff on the first selected file, else on the tree's first file.
const content = useRef<HTMLDivElement>(null);
const lead = filesUnder(roots, selection)[0] ?? ordered[0];
useDiffSource(content, lead ? { diff: () => chainedDiff(ordered, lead), show: () => showDiff(ordered, lead) } : null);
if (!changes || changes.state === "loading")
return (
<Centered>
@@ -209,32 +217,34 @@ function ChangesTree() {
showContextMenu(e, items);
};
return (
<Tree<FileTreeNode<CreateChange>>
key={changes.value.length ? "files" : "empty"}
className={styles.tree}
roots={roots}
getKey={(n) => n.key}
getLabel={(n) => n.name}
getChildren={(n) => n.children}
isExpandable={(n) => !n.file}
defaultExpanded={expanded}
selected={selection}
onSelectionChange={setSelection}
multiSelect
rowHeight={rowHeight}
aria-label="Changes"
speedSearch
onActivate={(n) => n.file && showDiff(ordered, n.file)}
onContextMenu={menu}
empty={
<Centered>
<span className={styles.hint}>
<T>Commit does not contain any changes</T>
</span>
</Centered>
}
renderNode={(n) => <ChangeRow node={n} />}
/>
<div ref={content} className={styles.treePane}>
<Tree<FileTreeNode<CreateChange>>
key={changes.value.length ? "files" : "empty"}
className={styles.tree}
roots={roots}
getKey={(n) => n.key}
getLabel={(n) => n.name}
getChildren={(n) => n.children}
isExpandable={(n) => !n.file}
defaultExpanded={expanded}
selected={selection}
onSelectionChange={setSelection}
multiSelect
rowHeight={rowHeight}
aria-label="Changes"
speedSearch
onActivate={(n) => n.file && showDiff(ordered, n.file)}
onContextMenu={menu}
empty={
<Centered>
<span className={styles.hint}>
<T>Commit does not contain any changes</T>
</span>
</Centered>
}
renderNode={(n) => <ChangeRow node={n} />}
/>
</div>
);
}
+3
Переглянути файл
@@ -205,6 +205,9 @@
.commitMeta {
color: var(--fg-muted);
}
.treePane {
display: contents;
}
.tree {
flex: 1;
min-height: 0;