@@ -78,21 +78,15 @@ const IconComponent = ({ icon: Icon, className }: { icon: any; className?: strin
7878 * @returns Editor panel content
7979 */
8080export function Editor ( ) {
81- const {
82- currentBlockId,
83- connectionsHeight,
84- toggleConnectionsCollapsed,
85- shouldFocusRename,
86- setShouldFocusRename,
87- } = usePanelEditorStore (
88- useShallow ( ( state ) => ( {
89- currentBlockId : state . currentBlockId ,
90- connectionsHeight : state . connectionsHeight ,
91- toggleConnectionsCollapsed : state . toggleConnectionsCollapsed ,
92- shouldFocusRename : state . shouldFocusRename ,
93- setShouldFocusRename : state . setShouldFocusRename ,
94- } ) )
95- )
81+ const { currentBlockId, connectionsHeight, toggleConnectionsCollapsed, registerRenameCallback } =
82+ usePanelEditorStore (
83+ useShallow ( ( state ) => ( {
84+ currentBlockId : state . currentBlockId ,
85+ connectionsHeight : state . connectionsHeight ,
86+ toggleConnectionsCollapsed : state . toggleConnectionsCollapsed ,
87+ registerRenameCallback : state . registerRenameCallback ,
88+ } ) )
89+ )
9690 const currentWorkflow = useCurrentWorkflow ( )
9791 const currentBlock = currentBlockId ? currentWorkflow . getBlockById ( currentBlockId ) : null
9892 const blockConfig = currentBlock ? getBlock ( currentBlock . type ) : null
@@ -229,6 +223,7 @@ export function Editor() {
229223
230224 const [ isRenaming , setIsRenaming ] = useState ( false )
231225 const [ editedName , setEditedName ] = useState ( '' )
226+ const renamingBlockIdRef = useRef < string | null > ( null )
232227
233228 /**
234229 * Ref callback that auto-selects the input text when mounted.
@@ -240,44 +235,62 @@ export function Editor() {
240235 } , [ ] )
241236
242237 /**
243- * Handles starting the rename process.
238+ * Starts the rename process for the current block.
239+ * Reads from stores directly to avoid stale closures when called via registered callback.
240+ * Captures the block ID in a ref to ensure the correct block is renamed even if selection changes.
244241 */
245242 const handleStartRename = useCallback ( ( ) => {
246- if ( ! canEditBlock || ! currentBlock ) return
247- setEditedName ( currentBlock . name || '' )
243+ const blockId = usePanelEditorStore . getState ( ) . currentBlockId
244+ if ( ! blockId ) return
245+
246+ const blocks = useWorkflowStore . getState ( ) . blocks
247+ const block = blocks [ blockId ]
248+ if ( ! block ) return
249+
250+ const parentId = block . data ?. parentId as string | undefined
251+ const isParentLocked = parentId ? ( blocks [ parentId ] ?. locked ?? false ) : false
252+ const isLocked = ( block . locked ?? false ) || isParentLocked
253+ if ( ! userPermissions . canEdit || isLocked ) return
254+
255+ renamingBlockIdRef . current = blockId
256+ setEditedName ( block . name || '' )
248257 setIsRenaming ( true )
249- } , [ canEditBlock , currentBlock ] )
258+ } , [ userPermissions . canEdit ] )
250259
251260 /**
252- * Handles saving the renamed block.
261+ * Saves the renamed block using the captured block ID from when rename started .
253262 */
254263 const handleSaveRename = useCallback ( ( ) => {
255- if ( ! currentBlockId || ! isRenaming ) return
264+ const blockIdToRename = renamingBlockIdRef . current
265+ if ( ! blockIdToRename || ! isRenaming ) return
266+
267+ const blocks = useWorkflowStore . getState ( ) . blocks
268+ const blockToRename = blocks [ blockIdToRename ]
256269
257270 const trimmedName = editedName . trim ( )
258- if ( trimmedName && trimmedName !== currentBlock ? .name ) {
259- const result = collaborativeUpdateBlockName ( currentBlockId , trimmedName )
271+ if ( trimmedName && blockToRename && trimmedName !== blockToRename . name ) {
272+ const result = collaborativeUpdateBlockName ( blockIdToRename , trimmedName )
260273 if ( ! result . success ) {
261274 return
262275 }
263276 }
277+ renamingBlockIdRef . current = null
264278 setIsRenaming ( false )
265- } , [ currentBlockId , isRenaming , editedName , currentBlock ?. name , collaborativeUpdateBlockName ] )
279+ } , [ isRenaming , editedName , collaborativeUpdateBlockName ] )
266280
267281 /**
268282 * Handles canceling the rename process.
269283 */
270284 const handleCancelRename = useCallback ( ( ) => {
285+ renamingBlockIdRef . current = null
271286 setIsRenaming ( false )
272287 setEditedName ( '' )
273288 } , [ ] )
274289
275290 useEffect ( ( ) => {
276- if ( shouldFocusRename && currentBlock ) {
277- handleStartRename ( )
278- setShouldFocusRename ( false )
279- }
280- } , [ shouldFocusRename , currentBlock , handleStartRename , setShouldFocusRename ] )
291+ registerRenameCallback ( handleStartRename )
292+ return ( ) => registerRenameCallback ( null )
293+ } , [ registerRenameCallback , handleStartRename ] )
281294
282295 /**
283296 * Handles opening documentation link in a new secure tab.
0 commit comments