Skip to content

Commit

Permalink
feat(client): Zoom the map onto the selected location
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprdhomme committed Nov 18, 2024
1 parent 524d6df commit 1847dd8
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@radix-ui/react-tooltip": "1.1.3",
"@t3-oss/env-nextjs": "0.11.1",
"@tanstack/react-query": "5.59.16",
"@turf/bbox": "7.1.0",
"@types/mapbox-gl": "3.4.0",
"apng-js": "1.1.4",
"axios": "1.7.7",
Expand Down
4 changes: 4 additions & 0 deletions client/src/components/map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import DeckglMapboxProvider from "@/components/map/deckgl-mapbox-provider";
import LayerManager from "@/components/map/layer-manager";
import { SIDEBAR_WIDTH } from "@/components/ui/sidebar";
import { env } from "@/env";
import useApplyMapLocation from "@/hooks/use-apply-map-location";
import useApplyMapSettings from "@/hooks/use-apply-map-settings";
import useBreakpoint from "@/hooks/use-breakpoint";
import useIsSidebarExpanded from "@/hooks/use-is-sidebar-expanded";
Expand Down Expand Up @@ -82,6 +83,9 @@ const Map = () => {
// Apply the basemap and labels
useApplyMapSettings(map);

// Zoom the map on the selected location
useApplyMapLocation(map);

return (
<ReactMapGL
ref={mapRef}
Expand Down
39 changes: 39 additions & 0 deletions client/src/hooks/use-apply-map-location.ts
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]);
}
22 changes: 11 additions & 11 deletions client/src/hooks/use-location-by-code.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { useGetLocations } from "@/types/generated/location";
import { Location } from "@/types/generated/strapi.schemas";

type LocationByCode = {
id: number;
name: string;
code: string;
};

export function useLocationByCode(code: string | undefined) {
export function useLocationByCode(code: string | undefined, fields = ["name", "code"]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error
const { data, isLoading } = useGetLocations<LocationByCode>(
const { data, isLoading } = useGetLocations<Location | undefined>(
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore-error
fields: ["name", "code"],
fields,
filters: {
code: {
$eq: code,
Expand All @@ -33,8 +28,13 @@ export function useLocationByCode(code: string | undefined) {

return {
id: data.data[0].id,
name: data.data[0].attributes!.name!,
code: data.data[0].attributes!.code!,
...fields.reduce(
(res, field) => ({
...res,
[field]: data.data![0].attributes![field as keyof Location],
}),
{},
),
};
},
},
Expand Down
35 changes: 34 additions & 1 deletion client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4238,6 +4238,18 @@ __metadata:
languageName: node
linkType: hard

"@turf/bbox@npm:7.1.0":
version: 7.1.0
resolution: "@turf/bbox@npm:7.1.0"
dependencies:
"@turf/helpers": "npm:^7.1.0"
"@turf/meta": "npm:^7.1.0"
"@types/geojson": "npm:^7946.0.10"
tslib: "npm:^2.6.2"
checksum: 10c0/901ed437ad1241b1c7cf76ee3f1dd998b32a59647074216d076a62080281693cc3f1d66d1dedd02fd5617ea57434ec059843bcc275d20f667019f3e1f378b05d
languageName: node
linkType: hard

"@turf/boolean-clockwise@npm:^5.1.5":
version: 5.1.5
resolution: "@turf/boolean-clockwise@npm:5.1.5"
Expand All @@ -4264,6 +4276,16 @@ __metadata:
languageName: node
linkType: hard

"@turf/helpers@npm:^7.1.0":
version: 7.1.0
resolution: "@turf/helpers@npm:7.1.0"
dependencies:
"@types/geojson": "npm:^7946.0.10"
tslib: "npm:^2.6.2"
checksum: 10c0/0b07c01584d8bee977edec8752109b4f79ab5b149e55a7dbe051e412e150c0a96f2464c9647676a092b7ab4429271eee4a31400ea45e9b55095ae53ad22f43d6
languageName: node
linkType: hard

"@turf/invariant@npm:^5.1.5":
version: 5.2.0
resolution: "@turf/invariant@npm:5.2.0"
Expand All @@ -4282,6 +4304,16 @@ __metadata:
languageName: node
linkType: hard

"@turf/meta@npm:^7.1.0":
version: 7.1.0
resolution: "@turf/meta@npm:7.1.0"
dependencies:
"@turf/helpers": "npm:^7.1.0"
"@types/geojson": "npm:^7946.0.10"
checksum: 10c0/c7aa77ddb28ef5068b031c1b422d2d5dc1df51975f727be42e2d8d500a026a2e667242d6aa06453f757cbd5ead2db0ba6b9a5d2fcf5ab496574cd4c0ae4fe325
languageName: node
linkType: hard

"@turf/rewind@npm:^5.1.5":
version: 5.1.5
resolution: "@turf/rewind@npm:5.1.5"
Expand Down Expand Up @@ -4329,7 +4361,7 @@ __metadata:
languageName: node
linkType: hard

"@types/geojson@npm:*, @types/geojson@npm:^7946.0.14, @types/geojson@npm:^7946.0.7, @types/geojson@npm:^7946.0.8":
"@types/geojson@npm:*, @types/geojson@npm:^7946.0.10, @types/geojson@npm:^7946.0.14, @types/geojson@npm:^7946.0.7, @types/geojson@npm:^7946.0.8":
version: 7946.0.14
resolution: "@types/geojson@npm:7946.0.14"
checksum: 10c0/54f3997708fa2970c03eeb31f7e4540a0eb6387b15e9f8a60513a1409c23cafec8d618525404573468b59c6fecbfd053724b3327f7fca416729c26271d799f55
Expand Down Expand Up @@ -5484,6 +5516,7 @@ __metadata:
"@t3-oss/env-nextjs": "npm:0.11.1"
"@tanstack/eslint-plugin-query": "npm:5.59.7"
"@tanstack/react-query": "npm:5.59.16"
"@turf/bbox": "npm:7.1.0"
"@types/mapbox-gl": "npm:3.4.0"
"@types/node": "npm:22.7.6"
"@types/react": "npm:18.3.1"
Expand Down

0 comments on commit 1847dd8

Please sign in to comment.