@@ -41,6 +41,7 @@ export default function CamPanel(props) {
4141
4242 // Props
4343 const { camFeature, isCamDetail, showRouteObjs } = props ;
44+ const newCam = camFeature . id ? camFeature : camFeature . getProperties ( ) ;
4445
4546 // Context
4647 const { authContext, setAuthContext } = useContext ( AuthContext ) ;
@@ -59,10 +60,9 @@ export default function CamPanel(props) {
5960 // Refs
6061 const isInitialMount = useRef ( true ) ;
6162 const camPanelRef = useRef ( null ) ;
63+ const rootCamRef = useRef ( newCam ) ;
6264
6365 // States
64- const newCam = camFeature . id ? camFeature : camFeature . getProperties ( ) ;
65- const [ rootCam , setRootCam ] = useState ( newCam ) ;
6666 const [ camera , setCamera ] = useState ( newCam ) ;
6767 const [ camIndex , setCamIndex ] = useState ( 0 ) ;
6868 const [ show , setShow ] = useState ( true ) ;
@@ -72,7 +72,7 @@ export default function CamPanel(props) {
7272 // Effects
7373 useEffect ( ( ) => {
7474 const newCam = camFeature . id ? camFeature : camFeature . getProperties ( ) ;
75- setRootCam ( newCam ) ;
75+ rootCamRef . current = newCam ;
7676 setCamera ( newCam ) ;
7777 const params = new URLSearchParams ( window . location . search ) ;
7878 const savedCamIndex = params . get ( 'camIndex' ) === null ? 0 : params . get ( 'camIndex' ) ;
@@ -89,7 +89,7 @@ export default function CamPanel(props) {
8989 isInitialMount . current = false ;
9090 return ;
9191 }
92- setCamera ( rootCam . camGroup [ camIndex ] ) ;
92+ setCamera ( rootCamRef . current . camGroup [ camIndex ] ) ;
9393
9494 searchParams . set ( "camIndex" , camIndex ) ;
9595 setSearchParams ( searchParams , { replace : true } ) ;
@@ -110,7 +110,7 @@ export default function CamPanel(props) {
110110 } ;
111111
112112 const updateCamera = ( ) => {
113- rootCam . camGroup . forEach ( ( cam , ii ) => {
113+ rootCamRef . current . camGroup . forEach ( ( cam , ii ) => {
114114 getCameras (
115115 null ,
116116 `${ window . API_HOST } /api/webcams/${ cam . id } /` ,
@@ -119,9 +119,9 @@ export default function CamPanel(props) {
119119 // using data attribute avoids lexical binding of camIndex state that
120120 // locks it at the initial value
121121 const currentCamIndex = camPanelRef . current . getAttribute ( 'data-current' ) ;
122- rootCam . camGroup [ ii ] = update ;
122+ rootCamRef . current . camGroup [ ii ] = update ;
123123 if ( ii == currentCamIndex ) {
124- setCamera ( rootCam . camGroup [ ii ] ) ;
124+ setCamera ( rootCamRef . current . camGroup [ ii ] ) ;
125125 setIsUpdated ( true ) ;
126126 }
127127 }
@@ -191,7 +191,7 @@ export default function CamPanel(props) {
191191 setCamIndex ( i ) ; // Trigger re-render
192192 } ;
193193
194- const res = Object . entries ( rootCam . camGroup ) . map ( ( [ index , cam ] ) => {
194+ const res = Object . entries ( rootCamRef . current . camGroup ) . map ( ( [ index , cam ] ) => {
195195 return (
196196 < Button
197197 aria-label = { getCameraOrientation ( cam . orientation ) }
0 commit comments