From 07469f19661eb571fb4f13b31ea01a35f9f51feb Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Thu, 27 Feb 2025 14:20:20 -0800 Subject: [PATCH] fix(sync): fixed disappearing subblock value on sync --- stores/sync-manager.ts | 2 +- stores/workflow/utils.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/stores/sync-manager.ts b/stores/sync-manager.ts index 2171391f40..3b156e82ce 100644 --- a/stores/sync-manager.ts +++ b/stores/sync-manager.ts @@ -41,7 +41,7 @@ async function prepareSyncPayload( if (!savedState) return null const state = JSON.parse(savedState) - const mergedBlocks = mergeSubblockState(state.blocks) + const mergedBlocks = mergeSubblockState(state.blocks, id) return { id, diff --git a/stores/workflow/utils.ts b/stores/workflow/utils.ts index 2e61b3a92c..9e639b5a34 100644 --- a/stores/workflow/utils.ts +++ b/stores/workflow/utils.ts @@ -1,18 +1,22 @@ import { Edge } from 'reactflow' +import { useWorkflowRegistry } from './registry/store' import { useSubBlockStore } from './subblock/store' import { BlockState, SubBlockState } from './types' /** * Merges workflow block states with subblock values while maintaining block structure * @param blocks - Block configurations from workflow store + * @param workflowId - ID of the workflow to merge values for * @param blockId - Optional specific block ID to merge (merges all if not provided) * @returns Merged block states with updated values */ export function mergeSubblockState( blocks: Record, + workflowId?: string, blockId?: string ): Record { const blocksToProcess = blockId ? { [blockId]: blocks[blockId] } : blocks + const subBlockStore = useSubBlockStore.getState() return Object.entries(blocksToProcess).reduce( (acc, [id, block]) => { @@ -30,7 +34,19 @@ export function mergeSubblockState( } // Get the stored value for this subblock - const storedValue = useSubBlockStore.getState().getValue(id, subBlockId) + let storedValue = null + + // If workflowId is provided, use it to get the value + if (workflowId) { + // Try to get the value from the subblock store for this specific workflow + const workflowValues = subBlockStore.workflowValues[workflowId] + if (workflowValues && workflowValues[id]) { + storedValue = workflowValues[id][subBlockId] + } + } else { + // Fall back to the active workflow if no workflowId is provided + storedValue = subBlockStore.getValue(id, subBlockId) + } // Create a new subblock object with the same structure but updated value subAcc[subBlockId] = {