1
- import { useState , useEffect , useContext } from 'react' ;
1
+ import { useState , useEffect , useContext , FunctionComponent , MouseEvent as ReactMouseEvent } from 'react' ;
2
2
import { Tab , Tabs , TabTitleText } from '@patternfly/react-core' ;
3
3
import {
4
4
GRAPH_POSITION_CHANGE_EVENT ,
@@ -34,12 +34,13 @@ interface TopologyViewComponentProps {
34
34
sideBarResizable ?: boolean ;
35
35
}
36
36
37
- const TopologyViewComponent : React . FunctionComponent < TopologyViewComponentProps > = observer (
37
+ const TopologyViewComponent : FunctionComponent < TopologyViewComponentProps > = observer (
38
38
( { useSidebar, sideBarResizable = false } ) => {
39
39
const [ selectedIds , setSelectedIds ] = useState < string [ ] > ( [ ] ) ;
40
40
const [ showAreaDragHint , setShowAreaDragHint ] = useState < boolean > ( false ) ;
41
41
const controller = useVisualizationController ( ) ;
42
42
const options = useContext ( DemoContext ) ;
43
+ const hasGraph = controller . hasGraph ( ) ;
43
44
44
45
useEffect ( ( ) => {
45
46
const dataModel = generateDataModel (
@@ -61,6 +62,14 @@ const TopologyViewComponent: React.FunctionComponent<TopologyViewComponentProps>
61
62
controller . fromModel ( model , true ) ;
62
63
} , [ controller , options . creationCounts , options . layout ] ) ;
63
64
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
+
64
73
useEventListener < SelectionEventListener > ( SELECTION_EVENT , ( ids ) => {
65
74
setSelectedIds ( ids ) ;
66
75
} ) ;
@@ -149,7 +158,7 @@ const TopologyViewComponent: React.FunctionComponent<TopologyViewComponentProps>
149
158
}
150
159
) ;
151
160
152
- export const Topology : React . FC < { useSidebar ?: boolean ; sideBarResizable ?: boolean } > = ( {
161
+ export const Topology : FunctionComponent < { useSidebar ?: boolean ; sideBarResizable ?: boolean } > = ( {
153
162
useSidebar = false ,
154
163
sideBarResizable = false
155
164
} ) => {
@@ -165,10 +174,10 @@ export const Topology: React.FC<{ useSidebar?: boolean; sideBarResizable?: boole
165
174
) ;
166
175
} ;
167
176
168
- export const TopologyPackage : React . FunctionComponent = ( ) => {
177
+ export const TopologyPackage : FunctionComponent = ( ) => {
169
178
const [ activeKey , setActiveKey ] = useState < string | number > ( 0 ) ;
170
179
171
- const handleTabClick = ( _event : React . MouseEvent < HTMLElement , MouseEvent > , tabIndex : string | number ) => {
180
+ const handleTabClick = ( _event : ReactMouseEvent < HTMLElement , MouseEvent > , tabIndex : string | number ) => {
172
181
setActiveKey ( tabIndex ) ;
173
182
} ;
174
183
0 commit comments