-
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.
feat(client): Zoom the map onto the selected location
- Loading branch information
1 parent
524d6df
commit 1847dd8
Showing
5 changed files
with
89 additions
and
12 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
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,39 @@ | ||
import { bbox } from "@turf/bbox"; | ||
import { useEffect, useMemo, useRef } from "react"; | ||
import { MapRef } from "react-map-gl"; | ||
|
||
import useLocation from "@/hooks/use-location"; | ||
import { useLocationByCode } from "@/hooks/use-location-by-code"; | ||
import usePrevious from "@/hooks/use-previous"; | ||
|
||
export default function useApplyMapLocation(map: MapRef | null) { | ||
const [location] = useLocation(); | ||
const previousLocation = usePrevious(location); | ||
|
||
// This flag indicates when to zoom the map on the location | ||
const triggerFitBoundsRef = useRef(false); | ||
|
||
const { data, isLoading } = useLocationByCode(location.code.slice(-1)[0], ["geometry"]); | ||
|
||
const bounds = useMemo(() => { | ||
if (isLoading || data?.geometry === undefined || data?.geometry === null) { | ||
return undefined; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return bbox(data.geometry) as [number, number, number, number]; | ||
}, [data, isLoading]); | ||
|
||
useEffect(() => { | ||
const hasChangedLocation = location !== previousLocation; | ||
if (hasChangedLocation) { | ||
triggerFitBoundsRef.current = true; | ||
} | ||
|
||
if (map && !!bounds && triggerFitBoundsRef.current) { | ||
map.fitBounds(bounds); | ||
triggerFitBoundsRef.current = false; | ||
} | ||
}, [map, location, previousLocation, data, isLoading]); | ||
} |
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