@@ -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,43 @@ 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+ if ( ! normalizedNewName ) {
1025+ logger . error ( 'Cannot rename block to empty name' )
1026+ useNotificationStore . getState ( ) . addNotification ( {
1027+ level : 'error' ,
1028+ message : 'Block name cannot be empty' ,
1029+ workflowId : activeWorkflowId || undefined ,
1030+ } )
1031+ return { success : false , error : 'Block name cannot be empty' }
1032+ }
1033+
1034+ const currentBlocks = workflowStore . blocks
1035+ const conflictingBlock = Object . entries ( currentBlocks ) . find (
1036+ ( [ blockId , block ] ) => blockId !== id && normalizeName ( block . name ) === normalizedNewName
1037+ )
1038+
1039+ if ( conflictingBlock ) {
1040+ const conflictName = conflictingBlock [ 1 ] . name
1041+ logger . error ( `Cannot rename block to "${ trimmedName } " - conflicts with "${ conflictName } "` )
1042+ useNotificationStore . getState ( ) . addNotification ( {
1043+ level : 'error' ,
1044+ message : `Block name "${ trimmedName } " already exists` ,
1045+ workflowId : activeWorkflowId || undefined ,
1046+ } )
1047+ return { success : false , error : `Block name "${ trimmedName } " already exists` }
1048+ }
1049+
1050+ executeQueuedOperation ( 'update-name' , 'block' , { id, name : trimmedName } , ( ) => {
1051+ const result = workflowStore . updateBlockName ( id , trimmedName )
10221052
10231053 if ( result . success && result . changedSubblocks . length > 0 ) {
10241054 logger . info ( 'Emitting cascaded subblock updates from block rename' , {
10251055 blockId : id ,
1026- newName : name ,
1056+ newName : trimmedName ,
10271057 updateCount : result . changedSubblocks . length ,
10281058 } )
10291059
@@ -1043,7 +1073,7 @@ export function useCollaborativeWorkflow() {
10431073 operation : {
10441074 operation : 'subblock-update' ,
10451075 target : 'subblock' ,
1046- payload : { blockId, subBlockId, value : newValue } ,
1076+ payload : { blockId, subblockId : subBlockId , value : newValue } ,
10471077 } ,
10481078 workflowId : activeWorkflowId || '' ,
10491079 userId : session ?. user ?. id || 'unknown' ,
@@ -1052,6 +1082,8 @@ export function useCollaborativeWorkflow() {
10521082 )
10531083 }
10541084 } )
1085+
1086+ return { success : true }
10551087 } ,
10561088 [ executeQueuedOperation , workflowStore , addToQueue , activeWorkflowId , session ?. user ?. id ]
10571089 )
0 commit comments