-
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.
- Loading branch information
1 parent
310bcd5
commit 0254e22
Showing
4 changed files
with
71 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,19 @@ | ||
import { useEffect } from "react"; | ||
import { MapRef } from "react-map-gl"; | ||
|
||
import { toggleGroupLayers } from "@/utils/map"; | ||
|
||
import useMapBasemap from "./use-map-basemap"; | ||
import useMapLabels from "./use-map-labels"; | ||
|
||
export default function useApplyMapSettings(map: MapRef | null) { | ||
const [basemap] = useMapBasemap(); | ||
const [labels] = useMapLabels(); | ||
|
||
useEffect(() => { | ||
if (map) { | ||
toggleGroupLayers(map, "basemap-", (group) => group === `basemap-${basemap}`); | ||
toggleGroupLayers(map, "labels-", (group) => group === `labels-${labels}`); | ||
} | ||
}, [map, basemap, labels]); | ||
} |
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,35 @@ | ||
import { MapRef } from "react-map-gl"; | ||
|
||
/** | ||
* Toggle on/off the layers of the groups matching the prefix based on the return value of the | ||
* callback | ||
*/ | ||
export const toggleGroupLayers = ( | ||
map: MapRef, | ||
groupPrefix: string, | ||
callback: (groupName: string) => boolean, | ||
) => { | ||
const mapboxMap = map?.getMap(); | ||
const mapboxStyle = map?.getStyle(); | ||
|
||
const { "mapbox:groups": mapboxGroups } = | ||
(mapboxStyle?.metadata as { "mapbox:groups": Record<string, { name: string }> }) ?? {}; | ||
|
||
const mapboxLayers = mapboxStyle?.layers ?? []; | ||
|
||
const groupsIdsMatchingPrefix = Object.entries(mapboxGroups ?? {}) | ||
.filter(([, { name }]) => name.startsWith(groupPrefix)) | ||
.map(([key]) => key); | ||
|
||
mapboxLayers.forEach((layer) => { | ||
const { "mapbox:group": group } = (layer.metadata as { "mapbox:group": string }) ?? {}; | ||
|
||
if (groupsIdsMatchingPrefix.includes(group)) { | ||
mapboxMap?.setLayoutProperty( | ||
layer.id, | ||
"visibility", | ||
callback(mapboxGroups[group].name) ? "visible" : "none", | ||
); | ||
} | ||
}); | ||
}; |