mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(stores): fixed duplicate block bug to merge with subblock store
This commit is contained in:
@@ -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<WorkflowStoreWithHistory>()(
|
||||
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<WorkflowStoreWithHistory>()(
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user