From feb8ba3b5b843779ef1659bef26dfdd9632c4c52 Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Thu, 20 Feb 2025 16:07:59 -0800 Subject: [PATCH] fix(stores): fixed duplicate block bug to merge with subblock store --- stores/workflow/store.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/stores/workflow/store.ts b/stores/workflow/store.ts index 6dfcf517fe..880dfe0195 100644 --- a/stores/workflow/store.ts +++ b/stores/workflow/store.ts @@ -8,6 +8,7 @@ import { useWorkflowRegistry } from './registry/store' import { useSubBlockStore } from './subblock/store' import { Loop, Position, SubBlockState } from './types' import { detectCycle } from './utils' +import { mergeSubblockState } from './utils' const initialState = { blocks: {}, @@ -290,7 +291,11 @@ export const useWorkflowStore = create()( const newName = match && match[2] ? `${match[1]}${parseInt(match[2]) + 1}` : `${block.name} 1` - const newSubBlocks = Object.entries(block.subBlocks).reduce( + // Get merged state to capture current subblock values + const mergedBlock = mergeSubblockState(get().blocks, id)[id] + + // Create new subblocks with merged values + const newSubBlocks = Object.entries(mergedBlock.subBlocks).reduce( (acc, [subId, subBlock]) => ({ ...acc, [subId]: { @@ -316,6 +321,22 @@ export const useWorkflowStore = create()( loops: { ...get().loops }, } + // Update the subblock store with the duplicated values + const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId + if (activeWorkflowId) { + const subBlockValues = + useSubBlockStore.getState().workflowValues[activeWorkflowId]?.[id] || {} + useSubBlockStore.setState((state) => ({ + workflowValues: { + ...state.workflowValues, + [activeWorkflowId]: { + ...state.workflowValues[activeWorkflowId], + [newId]: JSON.parse(JSON.stringify(subBlockValues)), + }, + }, + })) + } + set(newState) pushHistory(set, get, newState, `Duplicate ${block.type} block`) get().updateLastSaved()