Bugfix/Node ID Replacing Existing One (#3643)

fix node is replacing
This commit is contained in:
Henry Heng
2024-12-05 21:42:45 +00:00
committed by GitHub
parent 20e093884b
commit 371da23986
2 changed files with 45 additions and 35 deletions
+10 -17
View File
@@ -2,25 +2,18 @@ import { uniq } from 'lodash'
import moment from 'moment'
export const getUniqueNodeId = (nodeData, nodes) => {
// Get amount of same nodes
let totalSameNodes = 0
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i]
if (node.data.name === nodeData.name) {
totalSameNodes += 1
}
let suffix = 0
// Construct base ID
let baseId = `${nodeData.name}_${suffix}`
// Increment suffix until a unique ID is found
while (nodes.some((node) => node.id === baseId)) {
suffix += 1
baseId = `${nodeData.name}_${suffix}`
}
// Get unique id
let nodeId = `${nodeData.name}_${totalSameNodes}`
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i]
if (node.id === nodeId) {
totalSameNodes += 1
nodeId = `${nodeData.name}_${totalSameNodes}`
}
}
return nodeId
return baseId
}
export const initializeDefaultNodeData = (nodeParams) => {