From 730d34aef29fbb8a5baf4823164211e6e22cfea4 Mon Sep 17 00:00:00 2001 From: AfroJoe759 Date: Fri, 26 Apr 2024 15:44:42 -0400 Subject: [PATCH] Fix(designer): Adding an action in V3 could create a duplicate named action resulting in data loss. (#4721) * Committing a fix for duplicated naming upon action creatinn * Removed console.log statements and added comments for context of the change and when it should be removed * Updated a comment --------- Co-authored-by: Joe Brown --- .../designer/src/lib/core/actions/bjsworkflow/add.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libs/designer/src/lib/core/actions/bjsworkflow/add.ts b/libs/designer/src/lib/core/actions/bjsworkflow/add.ts index 6f4fcc26517..8e627b65ee7 100644 --- a/libs/designer/src/lib/core/actions/bjsworkflow/add.ts +++ b/libs/designer/src/lib/core/actions/bjsworkflow/add.ts @@ -76,8 +76,9 @@ export const addOperation = createAsyncThunk('addOperation', async (payload: Add if (!operation) { throw new Error('Operation does not exist'); // Just an optional catch, should never happen } - const nodeId = getNonDuplicateNodeId((getState() as RootState).workflow.nodesMetadata, actionId); + const workflowState = (getState() as RootState).workflow; + const nodeId = getNonDuplicateNodeId(workflowState.nodesMetadata, actionId, workflowState.idReplacements); const newPayload = { ...payload, nodeId }; dispatch(addNode(newPayload as any)); @@ -403,10 +404,15 @@ export const getTriggerNodeManifest = async ( return undefined; }; -export const getNonDuplicateNodeId = (nodesMetadata: NodesMetadata, actionId: string) => { +export const getNonDuplicateNodeId = (nodesMetadata: NodesMetadata, actionId: string, idReplacements: Record = {}) => { let count = 1; let nodeId = actionId; - while (getRecordEntry(nodesMetadata, nodeId)) { + + // Note: This is a temporary fix for the issue where the node id is not unique + // Because the workflow state isn't always up to date with action name changes unless flow is reloaded after saving + // To account for this we use the idReplacements to check for duplicates/changes in the same session + // This check should be once the workflow state is properly updated for all action name changes + while (getRecordEntry(nodesMetadata, nodeId) || Object.values(idReplacements).includes(nodeId)) { nodeId = `${actionId}_${count}`; count++; }