Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: added expandAll function to CollapsibleTree to handle expansion of all nested nodes (#4758)

Joaquin Corominab82ace74a1ce
diff --git a/frontend/src/utils/__tests__/id-tree.test.ts b/frontend/src/utils/__tests__/id-tree.test.ts
index ca3462735e..f486c24986 100644
--- a/frontend/src/utils/__tests__/id-tree.test.ts
+++ b/frontend/src/utils/__tests__/id-tree.test.ts
@@ -138,6 +138,22 @@ describe("CollapsibleTree", () => {
`);
});
+ it("expands all nested collapsed nodes correctly in one call", () => {
+ let collapsed = tree.collapse("three", undefined);
+ expect(collapsed.nodes[2].isCollapsed).toBe(true);
+ collapsed = collapsed.collapse("two", undefined);
+ expect(collapsed.nodes[1].isCollapsed).toBe(true);
+ const expandedTree = collapsed.expandAll();
+ expect(expandedTree.nodes[1].isCollapsed).toBe(false);
+ expect(expandedTree.toString()).toMatchInlineSnapshot(`
+ "one
+ two
+ three
+ four
+ "
+ `);
+ });
+
it("fails to expand", () => {
expect(() => tree.expand("five")).toThrowErrorMatchingInlineSnapshot(
"[Error: Node five not found in tree. Valid ids: one,two,three,four]",
diff --git a/frontend/src/utils/id-tree.tsx b/frontend/src/utils/id-tree.tsx
index 7ec9f3bfaa..bec59f1d81 100644
--- a/frontend/src/utils/id-tree.tsx
+++ b/frontend/src/utils/id-tree.tsx
@@ -306,6 +306,30 @@ export class CollapsibleTree<T> {
return this.withNodes(nodes);
}
+ /**
+ * Expand all collapsed nodes in the tree, including nested ones
+ */
+ expandAll(): CollapsibleTree<T> {
+ let nodes = [...this.nodes];
+ let nodeIndex = 0;
+
+ while (nodeIndex < nodes.length) {
+ const node = nodes[nodeIndex];
+ if (node.isCollapsed) {
+ // Replace the collapsed node with an expanded one
+ nodes[nodeIndex] = new TreeNode(node.value, false, []);
+ // Add the children of the collapsed node to the list
+ nodes = arrayInsertMany(nodes, nodeIndex + 1, node.children);
+ nodeIndex++;
+ } else {
+ // Move to the next node
+ nodeIndex++;
+ }
+ }
+
+ return this.withNodes(nodes);
+ }
+
/**
* Move a node from one index to another
*/