diff --git a/web/src/tree.test.ts b/web/src/tree.test.ts index 8a1e40c..e3f9642 100644 --- a/web/src/tree.test.ts +++ b/web/src/tree.test.ts @@ -39,6 +39,16 @@ describe("buildTree", () => { expect(names(root)).toEqual(["~/git/naps62/maestro"]); }); + it("folds the sole root folder into each of its own children when it branches", () => { + const root = buildTree( + [session("a", "/home/n/subvisual/bullish"), session("b", "/home/n/tea/maestro")], + "/home/n", + "", + ) as TreeFolder; + + expect(names(root).sort()).toEqual(["~/subvisual/bullish", "~/tea/maestro"]); + }); + it("stops collapsing where a directory branches", () => { const root = buildTree( [session("a", "/home/n/git/one"), session("b", "/home/n/git/two")], diff --git a/web/src/tree.ts b/web/src/tree.ts index 6f47ef7..b403d94 100644 --- a/web/src/tree.ts +++ b/web/src/tree.ts @@ -59,6 +59,15 @@ export function buildTree(sessions: Session[], home: string | undefined, filter: for (const c of n.children) if (c.kind === "folder") sortRec(c); }; sortRec(root); + // The root's sole child (usually `~`) has no sibling to distinguish it + // from, so it folds into every one of its own children, not just a lone + // one — unlike the chain merge below, and only here at the very top. + if (root.children.length === 1 && root.children[0].kind === "folder") { + const only = root.children[0]; + root.children = only.children.map((c) => + c.kind === "folder" ? { ...c, name: `${only.name}/${c.name}` } : c, + ); + } // Collapse linear directory chains so `~/git.naps.pt/naps62/maestro` reads as // one compact folder rather than four nested ones. root.children = root.children.map((c) => (c.kind === "folder" ? mergeLinear(c) : c));