diff --git a/packages/frontend-app-api/src/tree/resolveAppTree.test.ts b/packages/frontend-app-api/src/tree/resolveAppTree.test.ts index 6d84720648..e93d311f8d 100644 --- a/packages/frontend-app-api/src/tree/resolveAppTree.test.ts +++ b/packages/frontend-app-api/src/tree/resolveAppTree.test.ts @@ -143,44 +143,16 @@ describe('buildAppTree', () => { expect(Array.from(tree.nodes.keys())).toEqual(['a', 'b', 'c', 'd']); - expect(JSON.parse(JSON.stringify(tree.root))).toMatchInlineSnapshot(` - { - "attachments": { - "x": [ - { - "attachments": { - "x": [ - { - "id": "c", - }, - { - "id": "d", - }, - ], - }, - "id": "b", - }, - { - "attachments": { - "x": [ - { - "id": "d", - }, - ], - }, - "id": "c", - }, - ], - }, - "id": "a", - } - `); expect(String(tree.root)).toMatchInlineSnapshot(` " x [ x [ - + + x [ + + ] + ] diff --git a/packages/frontend-app-api/src/tree/resolveAppTree.ts b/packages/frontend-app-api/src/tree/resolveAppTree.ts index f346dc50bf..dacd06407a 100644 --- a/packages/frontend-app-api/src/tree/resolveAppTree.ts +++ b/packages/frontend-app-api/src/tree/resolveAppTree.ts @@ -145,6 +145,7 @@ export function resolveAppTree( } const orphans = new Array(); + const clones = new Map>(); // A node with the provided rootNodeId must be found in the tree, and it must not be attached to anything let rootNode: AppNode | undefined = undefined; @@ -167,13 +168,22 @@ export function resolveAppTree( const parent = nodes.get(attachTo.id); if (parent) { + const cloneParents = clones.get(attachTo.id) ?? []; + if (!foundFirstParent) { foundFirstParent = true; node.setParent(parent, attachTo.input); } else { - // TODO(Rugvip): Perhaps makes sense to keep track of these with a `clones` map, similar to `orphans`? + cloneParents.unshift(parent); + } + + for (const extraParent of cloneParents) { const clonedNode = new SerializableAppNode(spec); - clonedNode.setParent(parent, attachTo.input); + clonedNode.setParent(extraParent, attachTo.input); + clones.set( + spec.id, + clones.get(spec.id)?.concat(clonedNode) ?? [clonedNode], + ); } } }