Skip to content

Commit 79591b5

Browse files
committed
refactor(workflows): type the block rename rewrite by its input, dropping the casts at its callers
1 parent 9b7be91 commit 79591b5

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎apps/sim/lib/workflows/editing/operations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function renameBlock(blocks: Record<string, BlockState>, blockId: string, name:
8181
if (id === blockId) continue
8282
for (const subBlock of Object.values(block.subBlocks ?? {})) {
8383
if (subBlock.value === undefined || subBlock.value === null) continue
84-
subBlock.value = rewriteBlockNameReferences(subBlock.value, nameMap) as typeof subBlock.value
84+
subBlock.value = rewriteBlockNameReferences(subBlock.value, nameMap)
8585
}
8686
const data = block.data
8787
if (!data || (block.type !== 'loop' && block.type !== 'parallel')) continue

‎apps/sim/lib/workflows/references/block-rename.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import { escapeRegExp } from '@sim/utils/string'
22

33
/**
44
* `value` with every `<oldName.` reference token rewritten to `<newName.` for each entry of
5-
* `nameMap` (normalized block names), recursing through arrays and objects. The editor's rename,
6-
* an operations-batch rename, and a workflow import all rewrite block references through this.
5+
* `nameMap` (normalized block names), recursing through arrays and objects; the result has the
6+
* shape `value` had. The editor's rename, an operations-batch rename, and a workflow import all
7+
* rewrite block references through this.
78
*/
8-
export function rewriteBlockNameReferences(
9-
value: unknown,
10-
nameMap: ReadonlyMap<string, string>
11-
): unknown {
9+
export function rewriteBlockNameReferences<T>(value: T, nameMap: ReadonlyMap<string, string>): T {
10+
return rewrite(value, nameMap) as T
11+
}
12+
13+
function rewrite(value: unknown, nameMap: ReadonlyMap<string, string>): unknown {
1214
if (typeof value === 'string') {
1315
let updatedValue = value
1416
nameMap.forEach((newName, oldName) => {
@@ -34,12 +36,12 @@ export function rewriteBlockNameReferences(
3436
return updatedValue
3537
}
3638
if (Array.isArray(value)) {
37-
return value.map((item) => rewriteBlockNameReferences(item, nameMap))
39+
return value.map((item) => rewrite(item, nameMap))
3840
}
3941
if (value && typeof value === 'object') {
4042
const result: Record<string, unknown> = {}
4143
for (const [key, val] of Object.entries(value)) {
42-
result[key] = rewriteBlockNameReferences(val, nameMap)
44+
result[key] = rewrite(val, nameMap)
4345
}
4446
return result
4547
}

‎apps/sim/stores/workflows/utils.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ function updateBlockReferences(
161161
}
162162

163163
if (subBlock.value !== undefined && subBlock.value !== null) {
164-
const updatedValue = rewriteBlockNameReferences(
165-
subBlock.value,
166-
nameMap
167-
) as SubBlockState['value']
164+
const updatedValue = rewriteBlockNameReferences(subBlock.value, nameMap)
168165
block.subBlocks[subBlockId] = { ...subBlock, value: updatedValue }
169166
}
170167
})

0 commit comments

Comments
 (0)