Skip to content

Commit

Permalink
Fix(designer): Adding an action in V3 could create a duplicate named …
Browse files Browse the repository at this point in the history
…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 <joebrown@microsoft.com>
  • Loading branch information
AfroJoe759 and Joe Brown committed Apr 26, 2024
1 parent cbac876 commit 730d34a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libs/designer/src/lib/core/actions/bjsworkflow/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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<string, string> = {}) => {
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++;
}
Expand Down

0 comments on commit 730d34a

Please sign in to comment.