Repository navigation
fix(calm-hub-ui): stop the React Flow nodeTypes warning under StrictMode - #3665
Conversation
React Flow 11 reports error 002 from a useMemo that StrictMode runs twice on mount, even when nodeTypes and edgeTypes are already module constants. Upgrading to @xyflow/react 12 moves that check into useEffect and compares the type components themselves, so the false positive goes away without suppressing the warning. Fixes finos#3506
markscott-ms
left a comment
There was a problem hiding this comment.
Thanks @Mittalnew. Moving to @xyflow/react 12 is a good fix for #3506: it removes the false warning at the source instead of hiding it, and the new StrictMode test shows that a real node type change still reports error 002.
Two changes are necessary before merge. Both come from one change in React Flow 12:
In React Flow 11,
node.widthandnode.heightwere outputs: React Flow measured each node and wrote its size there, so it did not matter if our code set them. In React Flow 12 they are inputs: if they are set, React Flow draws the node at exactly that size (getNodeInlineStyleDimensions). The measured size is now innode.measured.
1. A restored layout fixes the size of each card
applyPositions in node-position-service.tsx sets the stored width and height on every node, including custom cards. A card sizes itself to its text, which the diagram text size and presentation mode change. In v11 this was harmless. In v12 it fixes the card at the stored size.
Example: save a layout at 100% text size. Then increase the diagram text size, or enter presentation mode. The cards do not grow, so the text overflows the box. This is the "stale box" that the comment in applyPositions says the code avoids. Each drag then saves the fixed size again, so it is never corrected.
Fix: do not set width and height on custom nodes in applyPositions. Containers can keep them.
2. Enlarged card sizes are saved for all viewers
When the text size is not 100%, or in presentation mode, useGraphInteractions removes the card sizes before it reflows the containers and saves the layout ({ ...node, width: undefined, height: undefined }). The intent is that the saved layout uses the normal card size, not one viewer's enlarged size. In v12 the spread keeps node.measured, and the new ?? node.measured fallbacks in getNodeWidth, getNodeHeight and toStoredPositions read the enlarged size from it.
Example: at 150% text size, drag a node. The containers are fitted to the enlarged cards, and that layout is saved. All viewers who load it get containers sized for one viewer's text size.
Fix: on that path, remove measured from the cards too, or do not use the measured fallback for cards when the sizes are reflowed and saved. A test at a text scale other than 1 that checks the saved container sizes would keep this from coming back.
Worth including
- The
ExportButtonand floating-edge test fixtures still use the v11 node shape (positionAbsolute,widthandheighton the node). So the tests run only the v11 fallback branches innodeGeometryanddiagramBounds. They pass because the fixture positions are already absolute, so a bug with child-node bounds would not be found. Please move the fixtures to the v12 shape (internals.positionAbsolute,measured). - After that, the v11 branches in
nodeGeometry.tsare needed only for the old fixtures, as its header comment says. They can then be removed, so that the helpers read one node shape. ArchitectureGraph.test.tsxandPatternGraph.test.tsxstill mockdefaultand__esModule.@xyflow/reacthas no default export, so these entries can go.
Minor (optional)
nodeGeometryreadsmeasuredbeforewidth, butgetNodeWidthandtoStoredPositionsreadwidthbeforemeasured. Using one order everywhere would make the size rules easier to follow.
Do not set width/height on custom nodes when restoring a layout, and drop measured as well as width/height before a non-100% or presentation save. Move geometry fixtures to the v12 node shape, remove the v11 fallbacks in nodeGeometry, and drop incidental CLI/calm-server lockfile version bumps that would conflict with releases.
|
Thanks for the review! I've stopped |
markscott-ms
left a comment
There was a problem hiding this comment.
Thanks @Mittalnew, the points from the first review are all addressed. One more change is necessary, and it comes from the fix for finding 1.
A new copy in a saved layout can overlap the card above it
Cards no longer get their stored size in applyPositions, but placeNewChildren still uses getNodeHeight(sibling) to put a new child (for example a new copy of a shared node) below its siblings. Without a stored height on the card, this is now the 100px default. The gap is 80px, so the new card overlaps any sibling card taller than 180px.
Example: a container with one card saved at y=80 with height 300, plus a new copy. On main the copy goes to y=460, below the card. On this branch it goes to y=260, inside the card, and the container does not grow to hold it.
Fix: in placeNewChildren, read a placed sibling's size from positionsById (the stored width and height) before you fall back to getNodeHeight/getNodeWidth. Please add a test with a tall stored card.
placeNewChildren now reads a placed sibling's width and height from positionsById when the node has no size of its own, then falls back to getNodeHeight/getNodeWidth, so a new copy is laid below a tall stored card instead of overlapping it.
|
Thanks @markscott-ms, good catch. placeNewChildren now reads a placed sibling's width and height from positionsById when the node has no size of its own, then falls back to getNodeHeight/getNodeWidth, and I added a test with a tall stored card so a new copy is placed below it instead of overlapping. |
markscott-ms
left a comment
There was a problem hiding this comment.
Thank you @Mittalnew for this careful move to React Flow 12, and for the quick and thorough fixes to each review.
|
Thanks @markscott-ms for the thorough reviews . |
Description
Fixes #3506
React Flow 11 reports error 002 (
new nodeTypes or edgeTypes object) from auseMemothat React StrictMode runs twice on mount.ArchitectureGraph,PatternGraphandDiffGraphalready definenodeTypesandedgeTypesas module constants, so this is a false positive that only shows in development.This upgrades
calm-hub-uifromreactflow11 to@xyflow/react12, which moved the check intouseEffectand compares the type components themselves. Stable module constants no longer warn, and a real change to a node or edge type still reports 002.Type of Change
Affected Components
calm-hub-ui/)Commit Message Format ✅
fix(calm-hub-ui): stop the React Flow nodeTypes warning under StrictModeTesting
Added a StrictMode unit test that renders
ReactFlowwith module-levelnodeTypes/edgeTypesand asserts that error 002 is not reported, plus a test that 002 still fires when a type component actually changes. Lint, the Vite production build, and the fullcalm-hub-uiVitest suite (2230 tests) pass.This also addresses the review feedback on the earlier closed PR #3560: ignoring 002 in
onErrorcould not work, because React Flow 11 raises the warning during the first render beforeonErroris written to the store, and a handler that logged other errors in production would not have matched the default dev-only behaviour. This change fixes the underlying StrictMode interaction instead of suppressing the warning.Checklist
I have signed the FINOS CLA.