mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-18 06:28:25 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
974b9de673
commit
96ab0a8a60
2 files changed
+10
-4
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user