Skip to content

Commit

Permalink
Extract useFeatureState to hooks & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
AmiyaSX committed Oct 10, 2024
1 parent 181b9ed commit c6df1a6
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/components/shared/hooks/useFeatureState.tsx
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])
}

0 comments on commit c6df1a6

Please sign in to comment.