From c98061bc3b4a47c8c8b67383f439416c312b19d9 Mon Sep 17 00:00:00 2001 From: Bently Date: Wed, 24 Jul 2024 17:00:59 +0100 Subject: [PATCH] fix(Builder): fixes to copy and pasted blocks (#7591) * fix(Builder): fixes to copy and pasted blocks * clear the blocks state on paste --- rnd/autogpt_builder/src/components/Flow.tsx | 50 ++++++++++++++------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/rnd/autogpt_builder/src/components/Flow.tsx b/rnd/autogpt_builder/src/components/Flow.tsx index d1311c0dea3f..739d954e3e49 100644 --- a/rnd/autogpt_builder/src/components/Flow.tsx +++ b/rnd/autogpt_builder/src/components/Flow.tsx @@ -479,25 +479,43 @@ const FlowEditor: React.FC<{ if (event.key === 'v' || event.key === 'V') { // Paste copied nodes if (copiedNodes.length > 0) { - const newNodes = copiedNodes.map((node, index) => ({ - ...node, - id: (nodeId + index).toString(), - position: { - x: node.position.x + 20, // Offset pasted nodes - y: node.position.y + 20, - }, - selected: true, // Select the new nodes - })); + const newNodes = copiedNodes.map((node, index) => { + const newNodeId = (nodeId + index).toString(); + return { + ...node, + id: newNodeId, + position: { + x: node.position.x + 20, // Offset pasted nodes + y: node.position.y + 20, + }, + data: { + ...node.data, + status: undefined, // Reset status + output_data: undefined, // Clear output data + setHardcodedValues: (values: { [key: string]: any }) => { + setNodes((nds) => nds.map((n) => + n.id === newNodeId + ? { ...n, data: { ...n.data, hardcodedValues: values } } + : n + )); + }, + }, + }; + }); const updatedNodes = nodes.map(node => ({ ...node, selected: false })); // Deselect old nodes setNodes([...updatedNodes, ...newNodes]); setNodeId(prevId => prevId + copiedNodes.length); - // Optionally, you can handle edges similarly - const newEdges = copiedEdges.map(edge => ({ - ...edge, - id: `${edge.source}_${edge.sourceHandle}_${edge.target}_${edge.targetHandle}_${Date.now()}`, - source: (parseInt(edge.source) + copiedNodes.length).toString(), - target: (parseInt(edge.target) + copiedNodes.length).toString(), - })); + + const newEdges = copiedEdges.map(edge => { + const newSourceId = newNodes.find(n => n.data.title === edge.source)?.id || edge.source; + const newTargetId = newNodes.find(n => n.data.title === edge.target)?.id || edge.target; + return { + ...edge, + id: `${newSourceId}_${edge.sourceHandle}_${newTargetId}_${edge.targetHandle}_${Date.now()}`, + source: newSourceId, + target: newTargetId, + }; + }); setEdges([...edges, ...newEdges]); } }