Skip to content

Commit 568f65c

Browse files
fix: address socket sync review follow-ups
1 parent 39faf7c commit 568f65c

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

apps/sim/hooks/use-collaborative-workflow.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ import { findAllDescendantNodes, isBlockProtected } from '@/stores/workflows/wor
3232

3333
const logger = createLogger('CollaborativeWorkflow')
3434

35+
function parseUndoRedoStackKey(key: string): { workflowId: string; userId: string } | null {
36+
const separatorIndex = key.indexOf(':')
37+
38+
if (separatorIndex <= 0 || separatorIndex === key.length - 1) {
39+
return null
40+
}
41+
42+
return {
43+
workflowId: key.slice(0, separatorIndex),
44+
userId: key.slice(separatorIndex + 1),
45+
}
46+
}
47+
3548
export function useCollaborativeWorkflow() {
3649
const undoRedo = useUndoRedo()
3750
const isUndoRedoInProgress = useRef(false)
@@ -239,9 +252,9 @@ export function useCollaborativeWorkflow() {
239252
const undoRedoStore = useUndoRedoStore.getState()
240253
const stackKeys = Object.keys(undoRedoStore.stacks)
241254
stackKeys.forEach((key) => {
242-
const [wfId, uId] = key.split(':')
243-
if (wfId === activeWorkflowId) {
244-
undoRedoStore.pruneInvalidEntries(wfId, uId, graph)
255+
const parsedKey = parseUndoRedoStackKey(key)
256+
if (parsedKey?.workflowId === activeWorkflowId) {
257+
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
245258
}
246259
})
247260
}
@@ -281,9 +294,9 @@ export function useCollaborativeWorkflow() {
281294
const undoRedoStore = useUndoRedoStore.getState()
282295
const stackKeys = Object.keys(undoRedoStore.stacks)
283296
stackKeys.forEach((key) => {
284-
const [wfId, uId] = key.split(':')
285-
if (wfId === activeWorkflowId) {
286-
undoRedoStore.pruneInvalidEntries(wfId, uId, graph)
297+
const parsedKey = parseUndoRedoStackKey(key)
298+
if (parsedKey?.workflowId === activeWorkflowId) {
299+
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
287300
}
288301
})
289302

@@ -646,10 +659,12 @@ export function useCollaborativeWorkflow() {
646659
const undoRedoStore = useUndoRedoStore.getState()
647660
const stackKeys = Object.keys(undoRedoStore.stacks)
648661
stackKeys.forEach((key) => {
649-
const [wfId, userId] = key.split(':')
650-
if (wfId === workflowId) {
651-
undoRedoStore.pruneInvalidEntries(wfId, userId, graph)
662+
const parsedKey = parseUndoRedoStackKey(key)
663+
if (!parsedKey || parsedKey.workflowId !== workflowId) {
664+
return
652665
}
666+
667+
undoRedoStore.pruneInvalidEntries(parsedKey.workflowId, parsedKey.userId, graph)
653668
})
654669
} finally {
655670
isApplyingRemoteChange.current = false

apps/sim/socket/database/operations.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,6 @@ async function handleBlocksOperationTx(
16301630
)
16311631

16321632
const allRemovedEdgeIds: string[] = []
1633-
const allAddedEdges: NonNullable<OperationResult['addedEdges']> = []
16341633
const applicableUpdates: Array<(typeof updates)[number]> = []
16351634

16361635
for (const update of updates) {
@@ -1649,11 +1648,8 @@ async function handleBlocksOperationTx(
16491648
continue
16501649
}
16511650

1652-
// Fetch current parent to update subflow node lists
1651+
// Confirm the block exists before applying the batch parent update
16531652
const existing = blocksById[id]
1654-
const existingParentId = (existing?.data as Record<string, unknown> | null)?.parentId as
1655-
| string
1656-
| undefined
16571653

16581654
if (!existing) {
16591655
logger.warn(`Block ${id} not found for batch-update-parent`)
@@ -1672,6 +1668,10 @@ async function handleBlocksOperationTx(
16721668
if (!id) continue
16731669

16741670
const isRemovingFromParent = !parentId
1671+
const existing = blocksById[id]
1672+
const existingParentId = (existing?.data as Record<string, unknown> | null)?.parentId as
1673+
| string
1674+
| undefined
16751675

16761676
// Get current data and position
16771677
const [currentBlock] = await tx
@@ -1715,13 +1715,6 @@ async function handleBlocksOperationTx(
17151715
)
17161716
allRemovedEdgeIds.push(...removedEdgeIds)
17171717

1718-
// Auto-connect: if block is entering a container and has no incoming edges,
1719-
// connect it from the container's start handle
1720-
if (parentId) {
1721-
const autoConnect = await autoConnectToContainerStart(tx, workflowId, id, parentId)
1722-
allAddedEdges.push(...autoConnect.edges)
1723-
}
1724-
17251718
// If the block now has a parent, update the new parent's subflow node list
17261719
if (parentId) {
17271720
await updateSubflowNodeList(tx, workflowId, parentId)
@@ -1733,7 +1726,7 @@ async function handleBlocksOperationTx(
17331726
}
17341727

17351728
logger.debug(`Batch updated parent for ${updates.length} blocks`)
1736-
return { removedEdgeIds: allRemovedEdgeIds, addedEdges: allAddedEdges }
1729+
return { removedEdgeIds: allRemovedEdgeIds }
17371730
}
17381731

17391732
default:

0 commit comments

Comments
 (0)