From 96ab0a8a605c02f29af49524e3e0f81ff29f3403 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Wed, 28 Oct 2026 10:44:22 -0700 Subject: [PATCH] Give empty sidebar nodes a collapsed flag too The shared tree treats every node with a children list as a folder and collapses an empty one before the left arrow moves to its parent. Left out of the collapsed map, a leaf path never read as collapsed, so the left arrow kept trying to collapse it and never moved up. Co-Authored-By: Claude Opus 5.5 --- apps/yaak-proxy/lib/sidebarCollapse.test.ts | 7 +++++-- apps/yaak-proxy/lib/sidebarCollapse.ts | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/yaak-proxy/lib/sidebarCollapse.test.ts b/apps/yaak-proxy/lib/sidebarCollapse.test.ts index 371ded2f5..61072307a 100644 --- a/apps/yaak-proxy/lib/sidebarCollapse.test.ts +++ b/apps/yaak-proxy/lib/sidebarCollapse.test.ts @@ -2,7 +2,7 @@ import type { TreeNode } from "@yaakapp-internal/ui"; import { describe, expect, test } from "vite-plus/test"; import { applyCollapsed, collapsedMap, folderIds } from "./sidebarCollapse"; -/** A node with these children, which `folderIds` counts as a folder when there are any */ +/** A node with a children list, empty or not, as the sidebar builds every node */ function node(id: string, children: TreeNode<{ id: string }>[] = []): TreeNode<{ id: string }> { return { item: { id }, parent: null, depth: 0, children }; } @@ -13,7 +13,6 @@ const tree = node("root", [ node("path:api.example.com/v1", [node("path:api.example.com/v1/users")]), ]), node("domain:cdn.example.com", [node("path:cdn.example.com/app.js")]), - // A host only ever hit at its root has no paths, so nothing to collapse. node("domain:empty.example.com"), ]), ]); @@ -21,10 +20,14 @@ const tree = node("root", [ describe("collapsedMap", () => { test("collapses every domain and path, and leaves the root and Domains open", () => { const map = collapsedMap(folderIds(tree), {}, {}); + // Empty ones too: the tree collapses one before the left arrow moves to its parent. expect(map).toEqual({ "domain:api.example.com": true, "path:api.example.com/v1": true, + "path:api.example.com/v1/users": true, "domain:cdn.example.com": true, + "path:cdn.example.com/app.js": true, + "domain:empty.example.com": true, }); expect(map.root).toBeUndefined(); expect(map.domains).toBeUndefined(); diff --git a/apps/yaak-proxy/lib/sidebarCollapse.ts b/apps/yaak-proxy/lib/sidebarCollapse.ts index 0fd788142..648bc42e8 100644 --- a/apps/yaak-proxy/lib/sidebarCollapse.ts +++ b/apps/yaak-proxy/lib/sidebarCollapse.ts @@ -8,11 +8,14 @@ export function startsCollapsed(id: string): boolean { return id.startsWith("domain:") || id.startsWith("path:"); } -/** Ids of the nodes with children, the only ones that collapse */ +/** + * Ids of the nodes the tree treats as folders, those with a children list, empty or not. The + * tree collapses an empty one before moving to its parent on the left arrow, so it needs a flag. + */ export function folderIds(root: TreeNode<{ id: string }>): string[] { const ids: string[] = []; const walk = (node: TreeNode<{ id: string }>) => { - if ((node.children?.length ?? 0) === 0) return; + if (node.children == null) return; ids.push(node.item.id); for (const child of node.children ?? []) walk(child); };