diff --git a/src/features/vcs/BranchWidget.tsx b/src/features/vcs/BranchWidget.tsx index 384cb940f..50f89051c 100644 --- a/src/features/vcs/BranchWidget.tsx +++ b/src/features/vcs/BranchWidget.tsx @@ -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], diff --git a/src/features/vcs/__tests__/branches.test.ts b/src/features/vcs/__tests__/branches.test.ts index 9940ea87d..27d3e8cdb 100644 --- a/src/features/vcs/__tests__/branches.test.ts +++ b/src/features/vcs/__tests__/branches.test.ts @@ -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]]); + }); +}); diff --git a/src/features/vcs/branches.ts b/src/features/vcs/branches.ts index 2e20299cd..798a8562e 100644 --- a/src/features/vcs/branches.ts +++ b/src/features/vcs/branches.ts @@ -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, prefix: string): ReadonlyMap { + const out = new Map(); + 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, key: string, open: boolean): Map { return new Map(expanded).set(key, open);