Skip to content

Commit 59a5160

Browse files
committed
consolidate merge subblock
1 parent aca3b8b commit 59a5160

File tree

3 files changed

+57
-55
lines changed

3 files changed

+57
-55
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const DEFAULT_SUBBLOCK_TYPE = 'short-input'
2+
3+
/**
4+
* Merges subblock values into the provided subblock structures.
5+
* Falls back to a default subblock shape when a value has no structure.
6+
* @param subBlocks - Existing subblock definitions from the workflow
7+
* @param values - Stored subblock values keyed by subblock id
8+
* @returns Merged subblock structures with updated values
9+
*/
10+
export function mergeSubBlockValues(
11+
subBlocks: Record<string, unknown> | undefined,
12+
values: Record<string, unknown> | undefined
13+
): Record<string, unknown> {
14+
const merged = { ...(subBlocks || {}) } as Record<string, any>
15+
16+
if (!values) return merged
17+
18+
Object.entries(values).forEach(([subBlockId, value]) => {
19+
if (merged[subBlockId] && typeof merged[subBlockId] === 'object') {
20+
merged[subBlockId] = {
21+
...(merged[subBlockId] as Record<string, unknown>),
22+
value,
23+
}
24+
return
25+
}
26+
27+
merged[subBlockId] = {
28+
id: subBlockId,
29+
type: DEFAULT_SUBBLOCK_TYPE,
30+
value,
31+
}
32+
})
33+
34+
return merged
35+
}

apps/sim/socket/database/operations.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import postgres from 'postgres'
77
import { env } from '@/lib/core/config/env'
88
import { cleanupExternalWebhook } from '@/lib/webhooks/provider-subscriptions'
99
import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/persistence/utils'
10+
import { mergeSubBlockValues } from '@/lib/workflows/subblocks'
1011
import {
1112
BLOCK_OPERATIONS,
1213
BLOCKS_OPERATIONS,
@@ -74,33 +75,6 @@ function isSubflowBlockType(blockType: string): blockType is SubflowType {
7475
return Object.values(SubflowType).includes(blockType as SubflowType)
7576
}
7677

77-
function mergeSubBlockValues(
78-
subBlocks: Record<string, unknown> | undefined,
79-
values: Record<string, unknown> | undefined
80-
): Record<string, unknown> {
81-
const merged = { ...(subBlocks || {}) } as Record<string, any>
82-
83-
if (!values) return merged
84-
85-
Object.entries(values).forEach(([subBlockId, value]) => {
86-
if (merged[subBlockId] && typeof merged[subBlockId] === 'object') {
87-
merged[subBlockId] = {
88-
...(merged[subBlockId] as Record<string, unknown>),
89-
value,
90-
}
91-
return
92-
}
93-
94-
merged[subBlockId] = {
95-
id: subBlockId,
96-
type: 'short-input',
97-
value,
98-
}
99-
})
100-
101-
return merged
102-
}
103-
10478
export async function updateSubflowNodeList(dbOrTx: any, workflowId: string, parentId: string) {
10579
try {
10680
// Get all child blocks of this parent

apps/sim/stores/workflows/utils.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
import type { Edge } from 'reactflow'
22
import { v4 as uuidv4 } from 'uuid'
3-
4-
export function filterNewEdges(edgesToAdd: Edge[], currentEdges: Edge[]): Edge[] {
5-
return edgesToAdd.filter((edge) => {
6-
if (edge.source === edge.target) return false
7-
return !currentEdges.some(
8-
(e) =>
9-
e.source === edge.source &&
10-
e.sourceHandle === edge.sourceHandle &&
11-
e.target === edge.target &&
12-
e.targetHandle === edge.targetHandle
13-
)
14-
})
15-
}
16-
173
import { getBlockOutputs } from '@/lib/workflows/blocks/block-outputs'
4+
import { mergeSubBlockValues } from '@/lib/workflows/subblocks'
185
import { getBlock } from '@/blocks'
196
import { normalizeName } from '@/executor/constants'
207
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
@@ -32,6 +19,19 @@ const WEBHOOK_SUBBLOCK_FIELDS = ['webhookId', 'triggerPath']
3219

3320
export { normalizeName }
3421

22+
export function filterNewEdges(edgesToAdd: Edge[], currentEdges: Edge[]): Edge[] {
23+
return edgesToAdd.filter((edge) => {
24+
if (edge.source === edge.target) return false
25+
return !currentEdges.some(
26+
(e) =>
27+
e.source === edge.source &&
28+
e.sourceHandle === edge.sourceHandle &&
29+
e.target === edge.target &&
30+
e.targetHandle === edge.targetHandle
31+
)
32+
})
33+
}
34+
3535
export interface RegeneratedState {
3636
blocks: Record<string, BlockState>
3737
edges: Edge[]
@@ -201,27 +201,20 @@ export function prepareDuplicateBlockState(options: PrepareDuplicateBlockStateOp
201201
Object.entries(subBlockValues).filter(([key]) => !WEBHOOK_SUBBLOCK_FIELDS.includes(key))
202202
)
203203

204-
const mergedSubBlocks: Record<string, SubBlockState> = sourceBlock.subBlocks
204+
const baseSubBlocks: Record<string, SubBlockState> = sourceBlock.subBlocks
205205
? JSON.parse(JSON.stringify(sourceBlock.subBlocks))
206206
: {}
207207

208208
WEBHOOK_SUBBLOCK_FIELDS.forEach((field) => {
209-
if (field in mergedSubBlocks) {
210-
delete mergedSubBlocks[field]
209+
if (field in baseSubBlocks) {
210+
delete baseSubBlocks[field]
211211
}
212212
})
213213

214-
Object.entries(filteredSubBlockValues).forEach(([subblockId, value]) => {
215-
if (mergedSubBlocks[subblockId]) {
216-
mergedSubBlocks[subblockId].value = value as SubBlockState['value']
217-
} else {
218-
mergedSubBlocks[subblockId] = {
219-
id: subblockId,
220-
type: 'short-input',
221-
value: value as SubBlockState['value'],
222-
}
223-
}
224-
})
214+
const mergedSubBlocks = mergeSubBlockValues(baseSubBlocks, filteredSubBlockValues) as Record<
215+
string,
216+
SubBlockState
217+
>
225218

226219
const block: BlockState = {
227220
id: newId,

0 commit comments

Comments
 (0)