From 82df3174397baaa20eedbab9d165592e2ad76274 Mon Sep 17 00:00:00 2001 From: "Ricardo M." Date: Thu, 8 Aug 2024 07:15:40 -0400 Subject: [PATCH] fix(Canvas): Remove mobx warnings from @patternfly/react-topology When opening and closing the configuration tab, mobx warnings are thrown, coming from the @patternfly/react-topology library. The fix is to wrap any operation to the canvas inside of an `action` callback, so mobx can notify the consumers that something changed. fix: https://github.com/KaotoIO/kaoto/issues/1294 --- .../Visualization/Canvas/Canvas.tsx | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/packages/ui/src/components/Visualization/Canvas/Canvas.tsx b/packages/ui/src/components/Visualization/Canvas/Canvas.tsx index a708927a7..ecb49b20b 100644 --- a/packages/ui/src/components/Visualization/Canvas/Canvas.tsx +++ b/packages/ui/src/components/Visualization/Canvas/Canvas.tsx @@ -41,7 +41,7 @@ interface CanvasProps { entities: BaseVisualCamelEntity[]; } -export const Canvas: FunctionComponent> = (props) => { +export const Canvas: FunctionComponent> = ({ entities, contextToolbar }) => { /** State for @patternfly/react-topology */ const [selectedIds, setSelectedIds] = useState([]); const [selectedNode, setSelectedNode] = useState(undefined); @@ -54,10 +54,10 @@ export const Canvas: FunctionComponent> = (props) const controller = useMemo(() => CanvasService.createController(), []); const { visibleFlows } = useContext(VisibleFlowsContext)!; const shouldShowEmptyState = useMemo(() => { - const areNoFlows = props.entities.length === 0; + const areNoFlows = entities.length === 0; const areAllFlowsHidden = Object.values(visibleFlows).every((visible) => !visible); return areNoFlows || areAllFlowsHidden; - }, [props.entities.length, visibleFlows]); + }, [entities.length, visibleFlows]); const controlButtons = useMemo(() => { const customButtons: TopologyControlButton[] = catalogModalContext @@ -96,9 +96,9 @@ export const Canvas: FunctionComponent> = (props) id: 'topology-control-bar-catalog-button', icon: , tooltip: 'Open Catalog', - callback: () => { + callback: action(() => { catalogModalContext.setIsModalOpen(true); - }, + }), }, ] : []; @@ -121,7 +121,7 @@ export const Canvas: FunctionComponent> = (props) legend: false, customButtons, }); - }, [catalogModalContext, controller]); + }, [catalogModalContext, controller, setActiveLayout]); const handleSelection = useCallback( (selectedIds: string[]) => { @@ -137,9 +137,9 @@ export const Canvas: FunctionComponent> = (props) /** Set up the controller one time */ useEffect(() => { const localController = controller; - const graphLayoutEndFn = () => { + const graphLayoutEndFn = action(() => { localController.getGraph().fit(80); - }; + }); localController.addEventListener(SELECTION_EVENT, handleSelection); localController.addEventListener(GRAPH_LAYOUT_END_EVENT, graphLayoutEndFn); @@ -154,13 +154,13 @@ export const Canvas: FunctionComponent> = (props) /** Draw graph */ useEffect(() => { - if (!Array.isArray(props.entities)) return; + if (!Array.isArray(entities)) return; setSelectedNode(undefined); const nodes: CanvasNode[] = []; const edges: CanvasEdge[] = []; - props.entities.forEach((entity) => { + entities.forEach((entity) => { if (visibleFlows[entity.id]) { const { nodes: childNodes, edges: childEdges } = CanvasService.getFlowDiagram(entity.toVizNode()); nodes.push(...childNodes); @@ -181,12 +181,16 @@ export const Canvas: FunctionComponent> = (props) }; controller.fromModel(model, false); - }, [controller, props.entities, visibleFlows]); + }, [activeLayout, controller, entities, visibleFlows]); useEffect(() => { - const timeoutId = setTimeout(() => { - controller.getGraph().fit(80); - }, 500); + const timeoutId = setTimeout( + action(() => { + controller.getGraph().fit(80); + }), + 500, + ); + return () => { clearTimeout(timeoutId); }; @@ -201,15 +205,15 @@ export const Canvas: FunctionComponent> = (props) return ( } - contextToolbar={props.contextToolbar} + contextToolbar={contextToolbar} controlBar={} > {shouldShowEmptyState ? ( - + ) : ( )}