1- import { useState , useEffect , useContext } from 'react' ;
1+ import { useState , useEffect , useContext , FunctionComponent , MouseEvent as ReactMouseEvent } from 'react' ;
22import { Tab , Tabs , TabTitleText } from '@patternfly/react-core' ;
33import {
44 GRAPH_POSITION_CHANGE_EVENT ,
@@ -34,12 +34,13 @@ interface TopologyViewComponentProps {
3434 sideBarResizable ?: boolean ;
3535}
3636
37- const TopologyViewComponent : React . FunctionComponent < TopologyViewComponentProps > = observer (
37+ const TopologyViewComponent : FunctionComponent < TopologyViewComponentProps > = observer (
3838 ( { useSidebar, sideBarResizable = false } ) => {
3939 const [ selectedIds , setSelectedIds ] = useState < string [ ] > ( [ ] ) ;
4040 const [ showAreaDragHint , setShowAreaDragHint ] = useState < boolean > ( false ) ;
4141 const controller = useVisualizationController ( ) ;
4242 const options = useContext ( DemoContext ) ;
43+ const hasGraph = controller . hasGraph ( ) ;
4344
4445 useEffect ( ( ) => {
4546 const dataModel = generateDataModel (
@@ -61,6 +62,14 @@ const TopologyViewComponent: React.FunctionComponent<TopologyViewComponentProps>
6162 controller . fromModel ( model , true ) ;
6263 } , [ controller , options . creationCounts , options . layout ] ) ;
6364
65+ // Once we have the graph, run the layout. This ensures the graph size is set (by the initial size observation in VisualizationSurface)
66+ // and the graph is centered by the layout.
67+ useEffect ( ( ) => {
68+ if ( hasGraph ) {
69+ controller . getGraph ( ) . layout ( ) ;
70+ }
71+ } , [ hasGraph , controller ] ) ;
72+
6473 useEventListener < SelectionEventListener > ( SELECTION_EVENT , ( ids ) => {
6574 setSelectedIds ( ids ) ;
6675 } ) ;
@@ -149,7 +158,7 @@ const TopologyViewComponent: React.FunctionComponent<TopologyViewComponentProps>
149158 }
150159) ;
151160
152- export const Topology : React . FC < { useSidebar ?: boolean ; sideBarResizable ?: boolean } > = ( {
161+ export const Topology : FunctionComponent < { useSidebar ?: boolean ; sideBarResizable ?: boolean } > = ( {
153162 useSidebar = false ,
154163 sideBarResizable = false
155164} ) => {
@@ -165,10 +174,10 @@ export const Topology: React.FC<{ useSidebar?: boolean; sideBarResizable?: boole
165174 ) ;
166175} ;
167176
168- export const TopologyPackage : React . FunctionComponent = ( ) => {
177+ export const TopologyPackage : FunctionComponent = ( ) => {
169178 const [ activeKey , setActiveKey ] = useState < string | number > ( 0 ) ;
170179
171- const handleTabClick = ( _event : React . MouseEvent < HTMLElement , MouseEvent > , tabIndex : string | number ) => {
180+ const handleTabClick = ( _event : ReactMouseEvent < HTMLElement , MouseEvent > , tabIndex : string | number ) => {
172181 setActiveKey ( tabIndex ) ;
173182 } ;
174183
0 commit comments