Skip to content

Commit 6fd4087

Browse files
fix(container): resize heuristic improvement (#2285)
* estimate block height for resize based on subblocks * fix hydration error * make more conservative
1 parent cb6e763 commit 6fd4087

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-node-utilities.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useCallback } from 'react'
22
import { useReactFlow } from 'reactflow'
33
import { createLogger } from '@/lib/logs/console/logger'
44
import { BLOCK_DIMENSIONS, CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
5+
import { getBlock } from '@/blocks/registry'
56

67
const 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
/**

apps/sim/components/icons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4151,7 +4151,7 @@ export function DuckDuckGoIcon(props: SVGProps<SVGSVGElement>) {
41514151
return (
41524152
<svg {...props} xmlns='http://www.w3.org/2000/svg' viewBox='-108 -108 216 216'>
41534153
<circle r='108' fill='#d53' />
4154-
<circle r='96' fill='none' stroke='#ffffff' stroke-width='7' />
4154+
<circle r='96' fill='none' stroke='#ffffff' strokeWidth={7} />
41554155
<path
41564156
d='M-32-55C-62-48-51-6-51-6l19 93 7 3M-39-73h-8l11 4s-11 0-11 7c24-1 35 5 35 5'
41574157
fill='#ddd'

0 commit comments

Comments
 (0)