-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switching to zustand for state management; adding router component fo…
…r tracking zoom and map center; moving summary stats component to charts directory; cleaning up imports in several components
- Loading branch information
Showing
21 changed files
with
701 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useRouter } from 'next/router' | ||
import { useMapbox } from '@carbonplan/maps' | ||
|
||
import useStore from '../store/index' | ||
|
||
const Router = () => { | ||
const { map } = useMapbox() | ||
const router = useRouter() | ||
const setZoom = useStore((state) => state.setZoom) | ||
const setCenter = useStore((state) => state.setCenter) | ||
const [zoomToBox, setZoomToBox] = useState(null) | ||
const [zoomInitialized, setZoomInitialized] = useState(false) | ||
|
||
{/* | ||
* The following three methods are modified from their original source: | ||
* https://github.com/carbonplan/forest-offsets-web/blob/ee51781bcbeb35172e29e051dc6387a1ec5b34cb/components/viewer.js#L129 | ||
*/} | ||
useEffect(() => { | ||
const { center, zoom } = router.query | ||
|
||
if (map && center && zoom && !zoomInitialized) { | ||
setZoomToBox({ | ||
center: center.split(',').map((d) => parseFloat(d)), | ||
zoom: parseFloat(zoom), | ||
}) | ||
} | ||
}, [map, router]) | ||
|
||
useEffect(() => { | ||
if (map && zoomToBox) { | ||
const { center, zoom } = zoomToBox | ||
map.easeTo({ | ||
center: center, | ||
zoom: zoom, | ||
duration: 0, | ||
}) | ||
setZoomInitialized(true) | ||
setZoomToBox(null) | ||
} | ||
}, [zoomToBox]) | ||
|
||
useEffect(() => { | ||
map.on('moveend', () => { | ||
const { pathname } = router | ||
|
||
let zoom = map.getZoom().toFixed(2) | ||
let center = [parseFloat(map.getCenter().lng.toFixed(2)), parseFloat(map.getCenter().lat.toFixed(2))] | ||
setZoom(zoom) | ||
setCenter(center) | ||
|
||
let suffix = `?center=${center[0]},${center[1]}&zoom=${zoom}` | ||
router.replace(pathname + suffix, null, { shallow: true }) | ||
}) | ||
}, [map]) | ||
|
||
return null | ||
} | ||
|
||
export default Router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.