From cc21fb66a7cf5d06361ab615344f698e6eff11d6 Mon Sep 17 00:00:00 2001 From: Lucas Oliveira Date: Mon, 29 May 2023 19:20:47 -0300 Subject: [PATCH] Import flows nodes id fixed --- src/frontend/src/contexts/tabsContext.tsx | 9 ++-- src/frontend/src/pages/FlowPage/index.tsx | 1 - src/frontend/src/utils.ts | 57 ++++++++++++++--------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index a373ad9a7..f41682b38 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -8,7 +8,7 @@ import { } from "react"; import { FlowType, NodeType } from "../types/flow"; import { LangFlowState, TabsContextType } from "../types/tabs"; -import { normalCaseToSnakeCase, updateObject, updateTemplate } from "../utils"; +import { normalCaseToSnakeCase, updateIds, updateObject, updateTemplate } from "../utils"; import { alertContext } from "./alertContext"; import { typesContext } from "./typesContext"; import { APITemplateType, TemplateVariableType } from "../types/api"; @@ -58,11 +58,8 @@ export function TabsProvider({ children }: { children: ReactNode }) { Saveflows.forEach((flow) => { if (flow.data && flow.data?.nodes) flow.data?.nodes.forEach((node) => { - console.log(node.data.type); Object.keys(node.data.node.template).forEach((key) => { - console.log(node.data.node.template[key].type); if (node.data.node.template[key].type === "file") { - console.log(node.data.node.template[key]); // ! Commenting this out for now, as it is causing issues with the file upload // node.data.node.template[key].content = ""; } @@ -283,7 +280,8 @@ export function TabsProvider({ children }: { children: ReactNode }) { function addFlow(flow?: FlowType) { // Get data from the flow or set it to null if there's no flow provided. - const data = flow?.data ? flow.data : null; + + let data = flow?.data ? flow.data : null; const description = flow?.description ? flow.description : ""; if (data) { @@ -307,6 +305,7 @@ export function TabsProvider({ children }: { children: ReactNode }) { ); } }); + updateIds(data, getNodeId); } // Create a new flow with a default name if no flow is provided. let newFlow: FlowType = { diff --git a/src/frontend/src/pages/FlowPage/index.tsx b/src/frontend/src/pages/FlowPage/index.tsx index f1a1f3b74..d8112410d 100644 --- a/src/frontend/src/pages/FlowPage/index.tsx +++ b/src/frontend/src/pages/FlowPage/index.tsx @@ -318,7 +318,6 @@ export default function FlowPage({ flow }: { flow: FlowType }) { setDisableCopyPaste(false); }} onPaneMouseLeave={() => { - console.log("saiu o mouse"); setDisableCopyPaste(true); }} onNodesChange={onNodesChange} diff --git a/src/frontend/src/utils.ts b/src/frontend/src/utils.ts index 2c2732962..9b7e32a92 100644 --- a/src/frontend/src/utils.ts +++ b/src/frontend/src/utils.ts @@ -16,8 +16,8 @@ import { CircleStackIcon, Squares2X2Icon, } from "@heroicons/react/24/outline"; -import { Connection, Edge, Node, ReactFlowInstance } from "reactflow"; -import { FlowType } from "./types/flow"; +import { Connection, Edge, Node, ReactFlowInstance, addEdge } from "reactflow"; +import { FlowType, NodeType } from "./types/flow"; import { APITemplateType, TemplateVariableType } from "./types/api"; import _ from "lodash"; import { v4 as uuidv4 } from "uuid"; @@ -536,26 +536,37 @@ export function checkUpperWords(str: string) { return words.join(" "); } -export function updateIds(newFLow: FlowType, baseFlow: FlowType) { - newFLow.data.nodes.forEach((node) => { - while (baseFlow.data.nodes.some((n) => n.id === node.id)) { - const newId = uuidv4(); - newFLow.data.edges.forEach((edge) => { - if (edge.source === node.id) { - edge.source = newId; - } - if (edge.target === node.id) { - edge.target = newId; - } - const index = edge.id.split("|").findIndex((e) => e === node.id); - if (index != -1) { - let tempList = edge.id.split("|"); - tempList[index] = newId; - edge.id = tempList.concat(newId).join("|"); - } - node.id = newId; - }); - } +export function updateIds(newFlow, getNodeId) { + let idsMap = {}; + + newFlow.nodes.forEach((n) => { + // Generate a unique node ID + let newId = getNodeId(); + idsMap[n.id] = newId; + n.id = newId; + n.data.id = newId; + // Add the new node to the list of nodes in state + }); + + newFlow.edges.forEach((e) => { + e.source = idsMap[e.source]; + e.target = idsMap[e.target]; + let sourceHandleSplitted = e.sourceHandle.split("|"); + e.sourceHandle = + sourceHandleSplitted[0] + + "|" + + e.source + + "|" + + sourceHandleSplitted.slice(2).join("|"); + let targetHandleSplitted = e.targetHandle.split("|"); + e.targetHandle = + targetHandleSplitted.slice(0, -1).join("|") + "|" + e.target; + e.id = + "reactflow__edge-" + + e.source + + e.sourceHandle + + "-" + + e.target + + e.targetHandle; }); - return newFLow; }