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); };