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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
feat: added collapseAll function to CollapsibleTree to handle collapsing of all nested expanded nodes in one call (#4780)
Joaquin Coromina7f6d73b202ac
diff --git a/frontend/src/utils/__tests__/id-tree.test.ts b/frontend/src/utils/__tests__/id-tree.test.tsindex f486c24986..b137a6a028 100644--- a/frontend/src/utils/__tests__/id-tree.test.ts+++ b/frontend/src/utils/__tests__/id-tree.test.ts@@ -115,7 +115,6 @@ describe("CollapsibleTree", () => {).toThrowErrorMatchingInlineSnapshot("[Error: Node one is before node two]",);-expect(() => {tree = tree.collapse("two", undefined);tree = tree.collapse("two", undefined);@@ -124,6 +123,67 @@ describe("CollapsibleTree", () => {);});+ it("collapses all nodes from leaves to root in one call", () => {+ const collapsedTree = tree.collapseAll([+ { id: "one", until: undefined },+ { id: "two", until: undefined },+ { id: "three", until: undefined },+ { id: "four", until: undefined },+ ]);+ expect(collapsedTree.toString()).toMatchInlineSnapshot(`+ "one (collapsed)+ two (collapsed)+ three (collapsed)+ four (collapsed)+ "+ `);+ });++ it("collapses some nodes from leaves to root in one call", () => {+ const collapsedTree = tree.collapseAll([+ { id: "one", until: undefined },+ null,+ { id: "three", until: "four" },+ null,+ ]);+ expect(collapsedTree.toString()).toMatchInlineSnapshot(`+ "one (collapsed)+ two+ three (collapsed)+ four+ "+ `);+ });++ it("failures to collapse all", () => {+ expect(() => tree.collapseAll([])).toThrowErrorMatchingInlineSnapshot(+ "[Error: No collapse ranges provided]",+ );+ expect(() =>+ tree.collapseAll([+ { id: "one", until: undefined },+ { id: "two", until: undefined },+ ]),+ ).toThrowErrorMatchingInlineSnapshot(+ "[Error: Collapse ranges length 2 does not match tree length 4]",+ );+ expect(() =>+ tree.collapseAll([null, { id: "one", until: undefined }, null, null]),+ ).toThrowErrorMatchingInlineSnapshot(+ "[Error: Node two does not match collapse range id one]",+ );+ expect(() =>+ tree.collapseAll([{ id: "one", until: "five" }, null, null, null]),+ ).toThrowErrorMatchingInlineSnapshot(+ "[Error: Node five not found in tree]",+ );+ expect(() =>+ tree.collapseAll([null, { id: "two", until: "one" }, null, null]),+ ).toThrowErrorMatchingInlineSnapshot(+ "[Error: Node one is before node two]",+ );+ });+it("expands nodes correctly", () => {const collapsed = tree.collapse("two", undefined);expect(collapsed.nodes[1].isCollapsed).toBe(true);diff --git a/frontend/src/utils/id-tree.tsx b/frontend/src/utils/id-tree.tsxindex bec59f1d81..d661d729e5 100644--- a/frontend/src/utils/id-tree.tsx+++ b/frontend/src/utils/id-tree.tsx@@ -283,6 +283,64 @@ export class CollapsibleTree<T> {return this.withNodes(nodes);}+ /**+ * Collapse all nodes in the tree, including nested ones+ *+ * Only works for the top-level nodes+ * Does not collapse the children of already collapsed nodes+ */+ collapseAll(+ collapseRanges: ({ id: T; until: T | undefined } | null)[],+ ): CollapsibleTree<T> {+ const nodes = [...this.nodes];+ if (collapseRanges.length === 0) {+ throw new Error("No collapse ranges provided");+ }++ if (collapseRanges.length !== nodes.length) {+ throw new Error(+ `Collapse ranges length ${collapseRanges.length} does not match tree length ${nodes.length}`,+ );+ }++ // Start from the end of the list and collapse nodes children first+ let nodeIndex = nodes.length - 1;+ while (nodeIndex >= 0) {+ const node = nodes[nodeIndex];+ const range = collapseRanges[nodeIndex];++ if (!node.isCollapsed && range) {+ const { id, until } = range;+ if (id !== node.value) {+ throw new Error(+ `Node ${node.value} does not match collapse range id ${id}`,+ );+ }+ const untilIndex =+ until === undefined+ ? nodes.length+ : nodes.findIndex((n) => n.value === until);++ if (untilIndex === -1) {+ throw new Error(`Node ${until} not found in tree`);+ }+ if (untilIndex < nodeIndex) {+ throw new Error(`Node ${until} is before node ${id}`);+ }++ // Fold the next nodes into the current node+ const children = nodes.splice(nodeIndex + 1, untilIndex - nodeIndex);+ nodes[nodeIndex] = new TreeNode(node.value, true, children);+ nodeIndex--;+ } else {+ // Move to the next node+ nodeIndex--;+ }+ }++ return this.withNodes(nodes);+ }+/*** Expand a node and all of its children*/