@@ -4,10 +4,10 @@ import { Button, Copy, Tooltip, Trash2 } from '@/components/emcn'
44import { cn } from '@/lib/core/utils/cn'
55import { isInputDefinitionTrigger } from '@/lib/workflows/triggers/input-definition-triggers'
66import { useUserPermissionsContext } from '@/app/workspace/[workspaceId]/providers/workspace-permissions-provider'
7+ import { validateTriggerPaste } from '@/app/workspace/[workspaceId]/w/[workflowId]/utils'
78import { useCollaborativeWorkflow } from '@/hooks/use-collaborative-workflow'
9+ import { useNotificationStore } from '@/stores/notifications'
810import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
9- import { useSubBlockStore } from '@/stores/workflows/subblock/store'
10- import { getUniqueBlockName , prepareDuplicateBlockState } from '@/stores/workflows/utils'
1111import { useWorkflowStore } from '@/stores/workflows/workflow/store'
1212
1313const DEFAULT_DUPLICATE_OFFSET = { x : 50 , y : 50 }
@@ -48,29 +48,38 @@ export const ActionBar = memo(
4848 collaborativeBatchToggleBlockEnabled,
4949 collaborativeBatchToggleBlockHandles,
5050 } = useCollaborativeWorkflow ( )
51- const { activeWorkflowId, setPendingSelection } = useWorkflowRegistry ( )
51+ const { setPendingSelection } = useWorkflowRegistry ( )
52+
53+ const addNotification = useNotificationStore ( ( s ) => s . addNotification )
5254
5355 const handleDuplicateBlock = useCallback ( ( ) => {
54- const blocks = useWorkflowStore . getState ( ) . blocks
55- const sourceBlock = blocks [ blockId ]
56- if ( ! sourceBlock ) return
56+ const { copyBlocks , preparePasteData , activeWorkflowId } = useWorkflowRegistry . getState ( )
57+ const existingBlocks = useWorkflowStore . getState ( ) . blocks
58+ copyBlocks ( [ blockId ] )
5759
58- const newId = crypto . randomUUID ( )
59- const newName = getUniqueBlockName ( sourceBlock . name , blocks )
60- const subBlockValues =
61- useSubBlockStore . getState ( ) . workflowValues [ activeWorkflowId || '' ] ?. [ blockId ] || { }
60+ const pasteData = preparePasteData ( DEFAULT_DUPLICATE_OFFSET )
61+ if ( ! pasteData ) return
6262
63- const { block, subBlockValues : filteredValues } = prepareDuplicateBlockState ( {
64- sourceBlock,
65- newId,
66- newName,
67- positionOffset : DEFAULT_DUPLICATE_OFFSET ,
68- subBlockValues,
69- } )
63+ const blocks = Object . values ( pasteData . blocks )
64+ const validation = validateTriggerPaste ( blocks , existingBlocks , 'duplicate' )
65+ if ( ! validation . isValid ) {
66+ addNotification ( {
67+ level : 'error' ,
68+ message : validation . message ! ,
69+ workflowId : activeWorkflowId || undefined ,
70+ } )
71+ return
72+ }
7073
71- setPendingSelection ( [ newId ] )
72- collaborativeBatchAddBlocks ( [ block ] , [ ] , { } , { } , { [ newId ] : filteredValues } )
73- } , [ blockId , activeWorkflowId , collaborativeBatchAddBlocks , setPendingSelection ] )
74+ setPendingSelection ( blocks . map ( ( b ) => b . id ) )
75+ collaborativeBatchAddBlocks (
76+ blocks ,
77+ pasteData . edges ,
78+ pasteData . loops ,
79+ pasteData . parallels ,
80+ pasteData . subBlockValues
81+ )
82+ } , [ blockId , addNotification , collaborativeBatchAddBlocks , setPendingSelection ] )
7483
7584 const { isEnabled, horizontalHandles, parentId, parentType } = useWorkflowStore (
7685 useCallback (
0 commit comments