Skip to content

Commit e152ca5

Browse files
committed
fix subflow copy/paste
1 parent 97a4b34 commit e152ca5

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

apps/sim/socket/database/operations.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,28 @@ async function handleBlocksOperationTx(
493493

494494
await tx.insert(workflowBlocks).values(blockValues)
495495

496-
// Create subflow entries for loop/parallel blocks
496+
// Create subflow entries for loop/parallel blocks (skip if already in payload)
497+
const loopIds = new Set(loops ? Object.keys(loops) : [])
498+
const parallelIds = new Set(parallels ? Object.keys(parallels) : [])
497499
for (const block of blocks) {
498-
if (block.type === 'loop' || block.type === 'parallel') {
500+
const blockId = block.id as string
501+
if (block.type === 'loop' && !loopIds.has(blockId)) {
499502
await tx.insert(workflowSubflows).values({
500-
id: block.id as string,
503+
id: blockId,
501504
workflowId,
502-
type: block.type as string,
505+
type: 'loop',
503506
config: {
504507
loopType: 'for',
505508
iterations: DEFAULT_LOOP_ITERATIONS,
509+
nodes: [],
510+
},
511+
})
512+
} else if (block.type === 'parallel' && !parallelIds.has(blockId)) {
513+
await tx.insert(workflowSubflows).values({
514+
id: blockId,
515+
workflowId,
516+
type: 'parallel',
517+
config: {
506518
parallelType: 'fixed',
507519
count: DEFAULT_PARALLEL_COUNT,
508520
nodes: [],

apps/sim/stores/workflows/registry/store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,15 @@ export const useWorkflowRegistry = create<WorkflowRegistry>()(
792792
const copiedSubBlockValues: Record<string, Record<string, unknown>> = {}
793793
const blockIdSet = new Set(blockIds)
794794

795+
// Auto-include nested nodes from selected subflows
795796
blockIds.forEach((blockId) => {
797+
const loop = workflowStore.loops[blockId]
798+
if (loop?.nodes) loop.nodes.forEach((n) => blockIdSet.add(n))
799+
const parallel = workflowStore.parallels[blockId]
800+
if (parallel?.nodes) parallel.nodes.forEach((n) => blockIdSet.add(n))
801+
})
802+
803+
blockIdSet.forEach((blockId) => {
796804
const block = workflowStore.blocks[blockId]
797805
if (block) {
798806
copiedBlocks[blockId] = JSON.parse(JSON.stringify(block))

0 commit comments

Comments
 (0)