@@ -2,6 +2,7 @@ import { useCallback } from 'react'
22import { useReactFlow } from 'reactflow'
33import { createLogger } from '@/lib/logs/console/logger'
44import { BLOCK_DIMENSIONS , CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
5+ import { getBlock } from '@/blocks/registry'
56
67const logger = createLogger ( 'NodeUtilities' )
78
@@ -20,7 +21,7 @@ export function useNodeUtilities(blocks: Record<string, any>) {
2021
2122 /**
2223 * Get the dimensions of a block.
23- * For regular blocks, estimates height if not yet measured by ResizeObserver .
24+ * For regular blocks, estimates height based on block config if not yet measured.
2425 */
2526 const getBlockDimensions = useCallback (
2627 ( blockId : string ) : { width : number ; height : number } => {
@@ -47,10 +48,13 @@ export function useNodeUtilities(blocks: Record<string, any>) {
4748 let height = block . height
4849
4950 if ( ! height ) {
50- // Estimate height for workflow blocks before ResizeObserver measures them
51- // Block structure: header + content area with subblocks
52- // Each subblock row is approximately 29px (14px text + 8px gap + padding)
53- const estimatedRows = 3 // Conservative estimate for typical blocks
51+ // Estimate height based on block config's subblock count for more accurate initial sizing
52+ // This is critical for subflow containers to size correctly before child blocks are measured
53+ const blockConfig = getBlock ( block . type )
54+ const subBlockCount = blockConfig ?. subBlocks ?. length ?? 3
55+ // Many subblocks are conditionally rendered (advanced mode, provider-specific, etc.)
56+ // Use roughly half the config count as a reasonable estimate, capped between 3-7 rows
57+ const estimatedRows = Math . max ( 3 , Math . min ( Math . ceil ( subBlockCount / 2 ) , 7 ) )
5458 const hasErrorRow = block . type !== 'starter' && block . type !== 'response' ? 1 : 0
5559
5660 height =
@@ -305,17 +309,23 @@ export function useNodeUtilities(blocks: Record<string, any>) {
305309 ...node ,
306310 depth : getNodeDepth ( node . id ) ,
307311 } ) )
308- . sort ( ( a , b ) => a . depth - b . depth )
312+ // Sort by depth descending - process innermost containers first
313+ // so their dimensions are correct when outer containers calculate sizes
314+ . sort ( ( a , b ) => b . depth - a . depth )
309315
310316 containerNodes . forEach ( ( node ) => {
311317 const dimensions = calculateLoopDimensions ( node . id )
318+ // Get current dimensions from the blocks store rather than React Flow's potentially stale state
319+ const currentWidth = blocks [ node . id ] ?. data ?. width
320+ const currentHeight = blocks [ node . id ] ?. data ?. height
312321
313- if ( dimensions . width !== node . data ?. width || dimensions . height !== node . data ?. height ) {
322+ // Only update if dimensions actually changed to avoid unnecessary re-renders
323+ if ( dimensions . width !== currentWidth || dimensions . height !== currentHeight ) {
314324 updateNodeDimensions ( node . id , dimensions )
315325 }
316326 } )
317327 } ,
318- [ getNodes , isContainerType , getNodeDepth , calculateLoopDimensions ]
328+ [ getNodes , isContainerType , getNodeDepth , calculateLoopDimensions , blocks ]
319329 )
320330
321331 /**
0 commit comments