-
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.
Extract useFeatureState to hooks & clean up
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Keep mapbox feature state in sync with component state | ||
|
||
import { BoothID } from "@/app/student/map/lib/booths" | ||
import { MutableRefObject, useEffect } from "react" | ||
import { MapRef } from "react-map-gl/dist/esm/exports-maplibre" | ||
|
||
// to allow for styling of the features | ||
export function useFeatureState( | ||
mapRef: MutableRefObject<MapRef | null>, | ||
boothIds: BoothID[], | ||
stateKey: "active" | "hover" | "filtered" | ||
) { | ||
useEffect(() => { | ||
const map = mapRef.current | ||
if (map == null || boothIds.length === 0) return | ||
|
||
for (const boothId of boothIds) { | ||
map.setFeatureState( | ||
{ source: "booths", id: boothId }, | ||
{ [stateKey]: true } | ||
) | ||
} | ||
|
||
return () => { | ||
for (const boothId of boothIds) { | ||
map.setFeatureState( | ||
{ source: "booths", id: boothId }, | ||
{ [stateKey]: false } | ||
) | ||
} | ||
} | ||
}, [boothIds, stateKey]) | ||
} |