Skip to content

Commit ec0a3b3

Browse files
fix: server-side auto-connection now respects client's autoConnect preference
1 parent dde1356 commit ec0a3b3

8 files changed

Lines changed: 369 additions & 117 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,11 @@ const WorkflowContent = React.memo(
645645
newParentId: u.parentId || null,
646646
newPosition: u.position,
647647
affectedEdges: [],
648-
}))
648+
})),
649+
{ autoConnect: isAutoConnectEnabled }
649650
)
650651
},
651-
[collaborativeBatchUpdateParent]
652+
[collaborativeBatchUpdateParent, isAutoConnectEnabled]
652653
)
653654

654655
/**
@@ -804,7 +805,8 @@ const WorkflowContent = React.memo(
804805
autoConnectEdge ? [autoConnectEdge] : [],
805806
{},
806807
{},
807-
subBlockValues
808+
subBlockValues,
809+
{ autoConnect: isAutoConnectEnabled }
808810
)
809811
usePanelEditorStore.getState().setCurrentBlockId(id)
810812
},
@@ -1106,7 +1108,8 @@ const WorkflowContent = React.memo(
11061108
pasteData.edges,
11071109
pasteData.loops,
11081110
pasteData.parallels,
1109-
pasteData.subBlockValues
1111+
pasteData.subBlockValues,
1112+
{ autoConnect: isAutoConnectEnabled }
11101113
)
11111114

11121115
// Resize container if we pasted into a subflow
@@ -2498,12 +2501,33 @@ const WorkflowContent = React.memo(
24982501
// Preserve existing selection state
24992502
setDisplayNodes((currentNodes) => {
25002503
const selectedIds = new Set(currentNodes.filter((n) => n.selected).map((n) => n.id))
2501-
return derivedNodes.map((node) => ({
2502-
...node,
2503-
selected: selectedIds.has(node.id),
2504-
}))
2504+
const dragStart = getDragStartPosition()
2505+
const multiDrag = multiNodeDragStartRef.current
2506+
2507+
return derivedNodes.map((node) => {
2508+
const isSelected = selectedIds.has(node.id)
2509+
const isBeingDragged = dragStart?.id === node.id || multiDrag.has(node.id)
2510+
2511+
if (isBeingDragged) {
2512+
// Find the current display node to preserve its position
2513+
const currentNode = currentNodes.find((n) => n.id === node.id)
2514+
if (currentNode) {
2515+
return {
2516+
...node,
2517+
position: currentNode.position,
2518+
parentId: currentNode.parentId,
2519+
selected: isSelected,
2520+
}
2521+
}
2522+
}
2523+
2524+
return {
2525+
...node,
2526+
selected: isSelected,
2527+
}
2528+
})
25052529
})
2506-
}, [derivedNodes, blocks, pendingSelection, clearPendingSelection])
2530+
}, [derivedNodes, blocks, pendingSelection, clearPendingSelection, getDragStartPosition])
25072531

25082532
/** Pans viewport to pending blocks once they have valid dimensions. */
25092533
useEffect(() => {
@@ -2608,7 +2632,7 @@ const WorkflowContent = React.memo(
26082632
})
26092633

26102634
// Single atomic batch update (handles edge removal + parent update + undo/redo)
2611-
collaborativeBatchUpdateParent(updates)
2635+
collaborativeBatchUpdateParent(updates, { autoConnect: isAutoConnectEnabled })
26122636

26132637
// Update displayNodes once to prevent React Flow from using stale parent data
26142638
setDisplayNodes((nodes) =>

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ export function useCollaborativeWorkflow() {
477477
)
478478
if (activeWorkflowId) {
479479
useSubBlockStore.getState().setWorkflowValues(activeWorkflowId, subBlockValues)
480+
pruneUndoRedoStacksForWorkflow(activeWorkflowId)
480481
}
481482

482483
logger.info('Successfully applied remote workflow state replacement')
@@ -726,9 +727,9 @@ export function useCollaborativeWorkflow() {
726727
}
727728

728729
const handleOperationConfirmed = (data: any) => {
729-
const { operationId } = data
730+
const { operationId, appliedPayload } = data
730731
logger.debug('Operation confirmed', { operationId })
731-
confirmOperation(operationId)
732+
confirmOperation(operationId, appliedPayload)
732733
}
733734

734735
const handleOperationFailed = (data: any) => {
@@ -1017,7 +1018,8 @@ export function useCollaborativeWorkflow() {
10171018
newParentId: string | null
10181019
newPosition: { x: number; y: number }
10191020
affectedEdges: Edge[]
1020-
}>
1021+
}>,
1022+
options?: { autoConnect?: boolean }
10211023
) => {
10221024
if (isBaselineDiffView) {
10231025
return
@@ -1085,6 +1087,7 @@ export function useCollaborativeWorkflow() {
10851087
parentId: u.newParentId || '',
10861088
position: u.newPosition,
10871089
})),
1090+
autoConnect: options?.autoConnect,
10881091
},
10891092
},
10901093
workflowId: activeWorkflowId || '',
@@ -1705,7 +1708,7 @@ export function useCollaborativeWorkflow() {
17051708
loops: Record<string, Loop> = {},
17061709
parallels: Record<string, Parallel> = {},
17071710
subBlockValues: Record<string, Record<string, unknown>> = {},
1708-
options?: { skipUndoRedo?: boolean }
1711+
options?: { skipUndoRedo?: boolean; autoConnect?: boolean }
17091712
) => {
17101713
if (!activeWorkflowId) {
17111714
logger.debug('Skipping batch add blocks - no active workflow')
@@ -1745,7 +1748,14 @@ export function useCollaborativeWorkflow() {
17451748
operation: {
17461749
operation: BLOCKS_OPERATIONS.BATCH_ADD_BLOCKS,
17471750
target: OPERATION_TARGETS.BLOCKS,
1748-
payload: { blocks, edges: validEdges, loops, parallels, subBlockValues },
1751+
payload: {
1752+
blocks,
1753+
edges: validEdges,
1754+
loops,
1755+
parallels,
1756+
subBlockValues,
1757+
autoConnect: options?.autoConnect,
1758+
},
17491759
},
17501760
workflowId: activeWorkflowId || '',
17511761
userId: session?.user?.id || 'unknown',

apps/sim/socket/database/operations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ async function handleBlockOperationTx(
808808
// Auto-connect: if block is entering a container and has no incoming edges,
809809
// connect it from the container's start handle (mirrors UI tryCreateAutoConnectEdge)
810810
let addedEdges: OperationResult['addedEdges'] = []
811-
if (payload.parentId) {
811+
if (payload.parentId && payload.autoConnect !== false) {
812812
const autoConnect = await autoConnectToContainerStart(
813813
tx,
814814
workflowId,
@@ -1815,8 +1815,9 @@ async function handleBlocksOperationTx(
18151815
)
18161816
)
18171817
allRemovedEdgeIds.push(...removedEdgeIds)
1818+
const canAutoConnect = payload.autoConnect !== false
18181819

1819-
if (parentId) {
1820+
if (parentId && canAutoConnect) {
18201821
const autoConnect = await autoConnectToContainerStart(tx, workflowId, id, parentId)
18211822
allAddedEdges.push(...autoConnect.edges)
18221823
}

0 commit comments

Comments
 (0)