diff --git a/website/src/components/explorer/index.jsx b/website/src/components/explorer/index.jsx index b1b190bc6..6311dded6 100644 --- a/website/src/components/explorer/index.jsx +++ b/website/src/components/explorer/index.jsx @@ -1,6 +1,6 @@ // Contains code adapted from https://observablehq.com/@nrabinowitz/h3-index-inspector under the ISC license -import React, { useCallback, useMemo, useState } from "react"; +import React, { useCallback, useMemo } from "react"; import { isValidCell, latLngToCell, getResolution } from "h3-js"; import { Banner, @@ -11,6 +11,7 @@ import { import { useQueryState } from "use-location-state"; import { SelectedHexDetails } from "./details"; import { ExplorerMap } from "./map"; +import { WhereAmIButton } from "./where-am-i"; function fullyTrim(str) { if (!str) { @@ -83,31 +84,6 @@ function zoomToResolution(zoom) { export default function HomeExporer({ children }) { const [userInput, setUserInput] = useQueryState("hex", ""); - const [geolocationStatus, setGeolocationStatus] = useState(""); - - const doGeoLocation = useCallback(async () => { - if ("geolocation" in navigator) { - setGeolocationStatus("Locating..."); - navigator.geolocation.getCurrentPosition( - (position) => { - // TODO: Consider using the accuracy to select resolution - setUserInput( - latLngToCell( - position.coords.latitude, - position.coords.longitude, - 11, - ), - ); - setGeolocationStatus(""); - }, - () => { - setGeolocationStatus("Error"); - }, - ); - } else { - setGeolocationStatus("No location services"); - } - }, [setUserInput]); const splitUserInput = useMemo(() => { return doSplitUserInput(userInput); @@ -184,13 +160,6 @@ export default function HomeExporer({ children }) { resize: "vertical", }} /> - - {geolocationStatus} {splitUserInput.length ? ( )} + {children} diff --git a/website/src/components/explorer/map.jsx b/website/src/components/explorer/map.jsx index 653f04cbd..cad9b9240 100644 --- a/website/src/components/explorer/map.jsx +++ b/website/src/components/explorer/map.jsx @@ -35,6 +35,7 @@ export function ExplorerMap({ }) { const [currentInitialViewState, setCurrentInitialViewState] = useState(initialViewState); + const [deckLoaded, setDeckLoaded] = useState(false); const deckRef = useRef(); const res0Cells = useMemo(() => getRes0Cells().map((hex) => ({ hex })), []); const res1Cells = useMemo( @@ -99,7 +100,7 @@ export function ExplorerMap({ }); } } - }, [userInput, userValidHex]); + }, [userInput, userValidHex, deckLoaded]); const layers = userValidHex ? [ @@ -209,6 +210,7 @@ export function ExplorerMap({ getTooltip={getTooltip} getCursor={getCursor} onClick={onClick} + onLoad={() => setDeckLoaded(true)} > diff --git a/website/src/components/explorer/where-am-i.jsx b/website/src/components/explorer/where-am-i.jsx new file mode 100644 index 000000000..e1b9e840b --- /dev/null +++ b/website/src/components/explorer/where-am-i.jsx @@ -0,0 +1,56 @@ +import { latLngToCell } from "h3-js"; +import React, { useCallback, useState } from "react"; + +export const WhereAmIButton = ({ setUserInput }) => { + const [geolocationStatus, setGeolocationStatus] = useState(""); + + const doGeoLocation = useCallback(async () => { + if ("geolocation" in navigator) { + setGeolocationStatus("Locating..."); + navigator.geolocation.getCurrentPosition( + (position) => { + // TODO: Consider using the accuracy to select resolution + setUserInput( + latLngToCell( + position.coords.latitude, + position.coords.longitude, + 11, + ), + ); + setGeolocationStatus(""); + }, + () => { + setGeolocationStatus("Error"); + }, + ); + } else { + setGeolocationStatus("No location services"); + } + }, [setUserInput]); + + return ( +
+ +
+ ); +};