From f4793940b01d956c5f43e8618bbc0cd41ff77828 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Wed, 26 Jul 2023 22:17:48 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(tabsContext.tsx):=20add=20mi?= =?UTF-8?q?ssing=20import=20statement=20for=20addVersionToDuplicates=20fun?= =?UTF-8?q?ction=20=E2=9C=A8=20feat(tabsContext.tsx):=20add=20version=20nu?= =?UTF-8?q?mber=20to=20duplicate=20flow=20names=20to=20ensure=20uniqueness?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(reactflowUtils.ts):=20add=20missing=20new?= =?UTF-8?q?line=20at=20end=20of=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/contexts/tabsContext.tsx | 6 +++++- src/frontend/src/utils/reactflowUtils.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/frontend/src/contexts/tabsContext.tsx b/src/frontend/src/contexts/tabsContext.tsx index d12fa18ba..30df2a8bd 100644 --- a/src/frontend/src/contexts/tabsContext.tsx +++ b/src/frontend/src/contexts/tabsContext.tsx @@ -20,7 +20,7 @@ import { import { APIClassType, APITemplateType } from "../types/api"; import { FlowType, NodeType } from "../types/flow"; import { TabsContextType, TabsState } from "../types/tabs"; -import { updateIds, updateTemplate } from "../utils/reactflowUtils"; +import { addVersionToDuplicates, updateIds, updateTemplate } from "../utils/reactflowUtils"; import { getRandomDescription, getRandomName } from "../utils/utils"; import { alertContext } from "./alertContext"; import { typesContext } from "./typesContext"; @@ -448,6 +448,10 @@ export function TabsProvider({ children }: { children: ReactNode }) { processFlowEdges(newFlow); processFlowNodes(newFlow); + const flowName = addVersionToDuplicates(newFlow, flows); + + newFlow.name = flowName; + try { const { id } = await saveFlowToDatabase(newFlow); // Change the id to the new id. diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index c4bec22c9..583526167 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -219,3 +219,16 @@ export function validateNodes(reactFlowInstance: ReactFlowInstance) { .getNodes() .flatMap((n: NodeType) => validateNode(n, reactFlowInstance)); } + +export function addVersionToDuplicates(flow: FlowType, flows: FlowType[]) { + const existingNames = flows.map((item) => item.name); + let newName = flow.name; + let count = 1; + + while (existingNames.includes(newName)) { + newName = `${flow.name} (${count})`; + count++; + } + + return newName; +} \ No newline at end of file