@@ -8,14 +8,15 @@ import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
88import { useSocket } from '@/app/workspace/providers/socket-provider'
99import { getBlock } from '@/blocks'
1010import { useUndoRedo } from '@/hooks/use-undo-redo'
11+ import { useNotificationStore } from '@/stores/notifications'
1112import { registerEmitFunctions , useOperationQueue } from '@/stores/operation-queue/store'
1213import { usePanelEditorStore } from '@/stores/panel/editor/store'
1314import { useVariablesStore } from '@/stores/panel/variables/store'
1415import { useUndoRedoStore } from '@/stores/undo-redo'
1516import { useWorkflowDiffStore } from '@/stores/workflow-diff/store'
1617import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1718import { useSubBlockStore } from '@/stores/workflows/subblock/store'
18- import { getUniqueBlockName , mergeSubblockState } from '@/stores/workflows/utils'
19+ import { getUniqueBlockName , mergeSubblockState , normalizeName } from '@/stores/workflows/utils'
1920import { useWorkflowStore } from '@/stores/workflows/workflow/store'
2021import type { BlockState , Position } from '@/stores/workflows/workflow/types'
2122
@@ -1016,14 +1017,46 @@ export function useCollaborativeWorkflow() {
10161017 )
10171018
10181019 const collaborativeUpdateBlockName = useCallback (
1019- ( id : string , name : string ) => {
1020- executeQueuedOperation ( 'update-name' , 'block' , { id, name } , ( ) => {
1021- const result = workflowStore . updateBlockName ( id , name )
1020+ ( id : string , name : string ) : { success : boolean ; error ?: string } => {
1021+ const trimmedName = name . trim ( )
1022+ const normalizedNewName = normalizeName ( trimmedName )
1023+
1024+ // Check for empty name
1025+ if ( ! normalizedNewName ) {
1026+ logger . error ( 'Cannot rename block to empty name' )
1027+ useNotificationStore . getState ( ) . addNotification ( {
1028+ level : 'error' ,
1029+ message : 'Block name cannot be empty' ,
1030+ workflowId : activeWorkflowId || undefined ,
1031+ } )
1032+ return { success : false , error : 'Block name cannot be empty' }
1033+ }
1034+
1035+ // Check for duplicate block names before queueing the operation
1036+ const currentBlocks = workflowStore . blocks
1037+ const conflictingBlock = Object . entries ( currentBlocks ) . find (
1038+ ( [ blockId , block ] ) => blockId !== id && normalizeName ( block . name ) === normalizedNewName
1039+ )
1040+
1041+ if ( conflictingBlock ) {
1042+ const conflictName = conflictingBlock [ 1 ] . name
1043+ logger . error ( `Cannot rename block to "${ trimmedName } " - conflicts with "${ conflictName } "` )
1044+ useNotificationStore . getState ( ) . addNotification ( {
1045+ level : 'error' ,
1046+ message : `Block name "${ trimmedName } " already exists` ,
1047+ workflowId : activeWorkflowId || undefined ,
1048+ } )
1049+ return { success : false , error : `Block name "${ trimmedName } " already exists` }
1050+ }
1051+
1052+ // Name is valid, proceed with the operation
1053+ executeQueuedOperation ( 'update-name' , 'block' , { id, name : trimmedName } , ( ) => {
1054+ const result = workflowStore . updateBlockName ( id , trimmedName )
10221055
10231056 if ( result . success && result . changedSubblocks . length > 0 ) {
10241057 logger . info ( 'Emitting cascaded subblock updates from block rename' , {
10251058 blockId : id ,
1026- newName : name ,
1059+ newName : trimmedName ,
10271060 updateCount : result . changedSubblocks . length ,
10281061 } )
10291062
@@ -1043,7 +1076,7 @@ export function useCollaborativeWorkflow() {
10431076 operation : {
10441077 operation : 'subblock-update' ,
10451078 target : 'subblock' ,
1046- payload : { blockId, subBlockId, value : newValue } ,
1079+ payload : { blockId, subblockId : subBlockId , value : newValue } ,
10471080 } ,
10481081 workflowId : activeWorkflowId || '' ,
10491082 userId : session ?. user ?. id || 'unknown' ,
@@ -1052,6 +1085,8 @@ export function useCollaborativeWorkflow() {
10521085 )
10531086 }
10541087 } )
1088+
1089+ return { success : true }
10551090 } ,
10561091 [ executeQueuedOperation , workflowStore , addToQueue , activeWorkflowId , session ?. user ?. id ]
10571092 )
0 commit comments