55 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
66 */
77
8- import React , { useMemo , useRef , useState } from 'react' ;
8+ import React , { forwardRef , useImperativeHandle , useMemo , useRef , useState } from 'react' ;
99import PropTypes from 'prop-types' ;
1010
1111import { useSelector } from "react-redux" ;
@@ -28,12 +28,12 @@ const MAPBOX_TOKEN = 'pk.eyJ1IjoiZ2VvZmphbWciLCJhIjoiY2pwbnRwcm8wMDYzMDQ4b2pieXd
2828const SUBSTATION_LAYER_PREFIX = "substationLayer" ;
2929const LINE_LAYER_PREFIX = "lineLayer" ;
3030
31- const NetworkMap = ( props ) => {
31+ const NetworkMap = forwardRef ( ( props , ref ) => {
3232
3333 const [ labelsVisible , setLabelsVisible ] = useState ( false ) ;
3434
3535 const [ deck , setDeck ] = useState ( null ) ;
36- const [ centered , setCentered ] = useState ( { lastCenteredSubstation : null , centered : false } ) ;
36+ const [ centered , setCentered ] = useState ( { lastCenteredSubstation : null , centeredSubstationId : null , centered : false } ) ;
3737 const lastViewStateRef = useRef ( null ) ;
3838
3939 const [ tooltip , setTooltip ] = useState ( { } ) ;
@@ -49,6 +49,12 @@ const NetworkMap = (props) => {
4949
5050 const useName = useSelector ( state => state . useName ) ;
5151
52+ useImperativeHandle ( ref , ( ) => ( {
53+ centerSubstation : ( substationId ) => {
54+ setCentered ( { lastCenteredSubstation : null , centeredSubstationId : substationId , centered : true } ) ;
55+ }
56+ } ) , [ setCentered ] ) ;
57+
5258 // Do this in onAfterRender because when doing it in useEffect (triggered by calling setDeck()),
5359 // it doesn't work in the case of using the browser backward/forward buttons (because in this particular case,
5460 // we get the ref to the deck and it has not yet initialized..)
@@ -57,11 +63,11 @@ const NetworkMap = (props) => {
5763 //TODO, replace the next lines with setProps( { initialViewState } ) when we upgrade to 8.1.0
5864 //see https://github.com/uber/deck.gl/pull/4038
5965 //This is a hack because it accesses the properties of deck directly but for now it works
60- if ( ( ! centered . centered || ( props . centeredSubstationId && props . centeredSubstationId !== centered . lastCenteredSubstation ) )
66+ if ( ( ! centered . centered || ( centered . centeredSubstationId && centered . centeredSubstationId !== centered . lastCenteredSubstation ) )
6167 && deck !== null && deck . viewManager != null && props . geoData !== null ) {
6268 if ( props . geoData . substationPositionsById . size > 0 ) {
63- if ( props . centeredSubstationId ) {
64- const geodata = props . geoData . substationPositionsById . get ( props . centeredSubstationId ) ;
69+ if ( centered . centeredSubstationId ) {
70+ const geodata = props . geoData . substationPositionsById . get ( centered . centeredSubstationId ) ;
6571 const copyViewState = lastViewStateRef . current || deck . viewState ;
6672 const newViewState = {
6773 longitude : geodata . lon ,
@@ -79,7 +85,7 @@ const NetworkMap = (props) => {
7985 deck . viewState = newViewState ;
8086 deck . setProps ( { } ) ;
8187 deck . _onViewStateChange ( { viewState : deck . viewState } ) ;
82- setCentered ( { lastCenteredSubstation : props . centeredSubstationId , centered : true } ) ;
88+ setCentered ( { lastCenteredSubstation : centered . centeredSubstationId , centeredSubstationId : centered . centeredSubstationId , centered : true } ) ;
8389 } else {
8490 const coords = Array . from ( props . geoData . substationPositionsById . entries ( ) ) . map ( x => x [ 1 ] ) ;
8591 const maxlon = Math . max . apply ( null , coords . map ( x => x . lon ) ) ;
@@ -207,7 +213,7 @@ const NetworkMap = (props) => {
207213 } } />
208214 </ div >
209215 </ DeckGL > ;
210- } ;
216+ } ) ;
211217
212218NetworkMap . defaultProps = {
213219 network : null ,
@@ -216,7 +222,6 @@ NetworkMap.defaultProps = {
216222 initialZoom : 5 ,
217223 filteredNominalVoltages : null ,
218224 initialPosition : [ 0 , 0 ] ,
219- centeredSubstationId : null
220225} ;
221226
222227NetworkMap . propTypes = {
@@ -227,7 +232,6 @@ NetworkMap.propTypes = {
227232 filteredNominalVoltages : PropTypes . array ,
228233 initialPosition : PropTypes . arrayOf ( PropTypes . number ) . isRequired ,
229234 onSubstationClick : PropTypes . func ,
230- centeredSubstationId : PropTypes . string
231235} ;
232236
233237export default React . memo ( NetworkMap ) ;
0 commit comments