fix(vcs): keep each repository's branch submenu expansion separate (#4694)

In multi-repository projects every repository's branch submenu read and wrote the same expansion state, so expanding Remote or a folder in one (e.g. to check out a remote branch) expanded it in all others. Each repository's submenu now keeps its own expansion (keys scoped by repository root); the single-repository popup keeps its saved state.
This commit is contained in:
l0ng-ai
2026-11-20 12:57:30 +08:00
committed by GitHub
parent f40bcd747f
commit 2c6329012a
3 changed files with 49 additions and 6 deletions
+20 -6
View File
@@ -26,7 +26,14 @@ import {
} from "@/ui";
import { useRowHeight } from "@/ui/density";
import type { PopupAnchor } from "@/ui/Popup";
import { buildBranchRows, preferredBranchRow, withExpanded, type BranchRow, type PopupAction } from "./branches";
import {
buildBranchRows,
preferredBranchRow,
scopedExpansion,
withExpanded,
type BranchRow,
type PopupAction,
} from "./branches";
import { branchPresentation, extensionPopupActions, useBranchWidgetExtensions } from "./branchWidgetExtensions";
import { branchMenu, commonBranchMenu, inRepo, tagMenu } from "./branchMenu";
import { loadFavoriteBranches, recentBranches, toggleFavoriteBranch, useFavoriteBranches } from "./branchOps";
@@ -262,12 +269,19 @@ function loadExpansion(root: string | null) {
useExpansion.setState(next);
}
function useExpanded(kind: "single" | "multi") {
const expanded = useExpansion((s) => s[kind]);
/**
* The expansion of one popup kind. `repo` scopes it to one repository's branches (a
* repository's submenu in a multi-repository project), so expanding a group there leaves
* the other repositories' submenus as they were.
*/
function useExpanded(kind: "single" | "multi", repo?: string) {
const all = useExpansion((s) => s[kind]);
const prefix = repo === undefined ? "" : `${repo}\u0000`;
const expanded = useMemo(() => scopedExpansion(all, prefix), [all, prefix]);
const setOpen = (key: string, open: boolean) => {
const s = useExpansion.getState();
if (s[kind].get(key) === open) return;
const next = { ...s, [kind]: withExpanded(s[kind], key, open) };
if (s[kind].get(prefix + key) === open) return;
const next = { ...s, [kind]: withExpanded(s[kind], prefix + key, open) };
useExpansion.setState(next);
if (!next.root) return;
try {
@@ -459,7 +473,7 @@ function BranchesPanel({ onClose, actions: baseActions, root, fallback, onBack }
const conflicts = useVcs((s) => s.status.filter((f) => f.unstaged === "conflicted").length);
const actions = useMemo(() => [...operationActions(op, conflicts), ...baseActions], [op, conflicts, baseActions]);
const options = usePopupOptions();
const [expanded, setOpen] = useExpanded("single");
const [expanded, setOpen] = useExpanded("single", root);
const rows = useMemo(
() => buildBranchRows(branches, recent, query, actions, favorites, tags, { ...options, expanded }),
[branches, recent, query, actions, favorites, tags, options, expanded],
@@ -9,6 +9,7 @@ import {
isFavoriteBranch,
preferredBranchRow,
pushRecent,
scopedExpansion,
type BranchRow,
type BranchTreeNode,
} from "../branches";
@@ -323,3 +324,21 @@ describe("remote web URLs", () => {
expect(commitWebUrl("git@bitbucket.org:o/r.git", "abc")).toBe("https://bitbucket.org/o/r/commits/abc");
});
});
describe("scopedExpansion", () => {
it("keeps each repository's submenu expansion apart from the others and the project's", () => {
const all = new Map([
["group:remote", false],
["/r/alpha\u0000group:remote", true],
["/r/alpha\u0000folder:remote:origin/feature", true],
["/r/beta\u0000group:local", false],
]);
expect([...scopedExpansion(all, "/r/alpha\u0000")]).toEqual([
["group:remote", true],
["folder:remote:origin/feature", true],
]);
expect([...scopedExpansion(all, "/r/beta\u0000")]).toEqual([["group:local", false]]);
expect([...scopedExpansion(all, "/r/gamma\u0000")]).toEqual([]);
expect([...scopedExpansion(all, "")]).toEqual([["group:remote", false]]);
});
});
+10
View File
@@ -46,6 +46,16 @@ export interface BranchTreeOptions {
showTags?: boolean;
}
/** The entries of `all` under `prefix` (without it); unprefixed entries when `prefix` is empty. */
export function scopedExpansion(all: ReadonlyMap<string, boolean>, prefix: string): ReadonlyMap<string, boolean> {
const out = new Map<string, boolean>();
for (const [key, open] of all) {
const scoped = key.indexOf("\u0000");
if (prefix ? key.startsWith(prefix) : scoped < 0) out.set(key.slice(prefix.length), open);
}
return out;
}
/** Copy of `expanded` with `key` set to `open`. */
export function withExpanded(expanded: ReadonlyMap<string, boolean>, key: string, open: boolean): Map<string, boolean> {
return new Map(expanded).set(key, open);