@@ -17,6 +17,7 @@ import {
1717 getSpacerNodes ,
1818 Graph ,
1919 GraphComponent ,
20+ GRAPH_LAYOUT_END_EVENT ,
2021 Layout ,
2122 ModelKind ,
2223 Node ,
@@ -32,6 +33,7 @@ import {
3233 WhenDecorator
3334} from '@patternfly/react-topology' ;
3435
36+ import { getResizeObserver } from '@patternfly/react-core' ;
3537import { t } from 'i18next' ;
3638import _ from 'lodash' ;
3739import * as React from 'react' ;
@@ -90,8 +92,17 @@ export type FlowCollectorPipelineProps = {
9092} ;
9193
9294export const Pipeline : React . FC < FlowCollectorPipelineProps > = ( { existing } ) => {
95+ const containerRef = React . createRef < HTMLDivElement > ( ) ;
9396 const [ controller , setController ] = React . useState < Visualization > ( ) ;
9497
98+ const fit = React . useCallback ( ( ) => {
99+ if ( controller && controller . hasGraph ( ) ) {
100+ controller . getGraph ( ) . fit ( ) ;
101+ } else {
102+ console . error ( 'onResize called before controller graph' ) ;
103+ }
104+ } , [ controller ] ) ;
105+
95106 const getStatus = React . useCallback (
96107 ( types : string [ ] , status : string ) => {
97108 for ( let i = 0 ; i < types . length ; i ++ ) {
@@ -199,11 +210,26 @@ export const Pipeline: React.FC<FlowCollectorPipelineProps> = ({ existing }) =>
199210 } , [ existing , getStatus ] ) ;
200211
201212 React . useEffect ( ( ) => {
213+ if ( containerRef . current ) {
214+ getResizeObserver (
215+ containerRef . current ,
216+ ( ) => {
217+ fit ( ) ;
218+ } ,
219+ true
220+ ) ;
221+ }
222+ } , [ containerRef , controller , fit ] ) ;
223+
224+ React . useEffect ( ( ) => {
225+ if ( ! controller ) {
226+ return ;
227+ }
202228 const steps = getSteps ( ) ;
203229 const spacerNodes = getSpacerNodes ( steps ) ;
204230 const nodes = [ ...steps , ...spacerNodes ] ;
205231 const edges = getEdgesFromNodes ( steps ) ;
206- controller ? .fromModel (
232+ controller . fromModel (
207233 {
208234 nodes,
209235 edges,
@@ -222,15 +248,17 @@ export const Pipeline: React.FC<FlowCollectorPipelineProps> = ({ existing }) =>
222248 const c = new Visualization ( ) ;
223249 c . registerComponentFactory ( pipelineComponentFactory ) ;
224250 c . registerLayoutFactory ( ( type : string , graph : Graph ) : Layout | undefined => new PipelineDagreLayout ( graph ) ) ;
225- c . setFitToScreenOnLayout ( true ) ;
251+ c . addEventListener ( GRAPH_LAYOUT_END_EVENT , fit ) ;
226252 setController ( c ) ;
227253 // eslint-disable-next-line react-hooks/exhaustive-deps
228254 } , [ ] ) ;
229255
230256 return (
231- < VisualizationProvider controller = { controller } >
232- < VisualizationSurface />
233- </ VisualizationProvider >
257+ < div id = "pipeline-container-div" style = { { width : '100%' , height : '100%' } } ref = { containerRef } >
258+ < VisualizationProvider controller = { controller } >
259+ < VisualizationSurface />
260+ </ VisualizationProvider >
261+ </ div >
234262 ) ;
235263} ;
236264
0 commit comments