-
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): Highlight the selected location on the map
- Loading branch information
1 parent
c6aecc9
commit c9490c8
Showing
2 changed files
with
85 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
client/src/components/map/layer-manager/selected-location-layer.tsx
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,72 @@ | ||
import { PathStyleExtension } from "@deck.gl/extensions"; | ||
import { GeoJsonLayer } from "@deck.gl/layers"; | ||
import { useContext, useEffect, useMemo } from "react"; | ||
|
||
import useLocation from "@/hooks/use-location"; | ||
import { useLocationGeometry } from "@/hooks/use-location-geometry"; | ||
|
||
import { DeckGLMapboxOverlayContext } from "../deckgl-mapbox-provider"; | ||
|
||
interface SelectedLocationLayerProps { | ||
beforeId: string; | ||
} | ||
|
||
const SelectedLocationLayer = ({ beforeId }: SelectedLocationLayerProps) => { | ||
const [location] = useLocation(); | ||
const { data, isLoading } = useLocationGeometry(location.code.slice(-1)[0]); | ||
const geometry = useMemo(() => { | ||
if (isLoading || data === undefined || data === null || location.code[0] === "SS") { | ||
// We return an empty feature collection so that while the geometry is loading, we don't show | ||
// anything | ||
return { | ||
type: "FeatureCollection", | ||
features: [], | ||
}; | ||
} | ||
|
||
return data; | ||
}, [data, isLoading, location.code]); | ||
|
||
const { addLayer, removeLayer } = useContext(DeckGLMapboxOverlayContext); | ||
|
||
useEffect(() => { | ||
const layers = [ | ||
new GeoJsonLayer({ | ||
id: "selected-location-background-stroke", | ||
beforeId, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
data: geometry, | ||
stroked: true, | ||
filled: false, | ||
getLineColor: [255, 255, 255], | ||
getLineWidth: 3, | ||
lineWidthUnits: "pixels", | ||
}), | ||
new GeoJsonLayer({ | ||
id: "selected-location-top-stroke", | ||
beforeId, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
data: geometry, | ||
stroked: true, | ||
filled: false, | ||
getLineColor: [255, 204, 21], // supernova-yellow-400 | ||
getLineWidth: 2, | ||
lineWidthUnits: "pixels", | ||
getDashArray: [4, 2], | ||
extensions: [new PathStyleExtension({ dash: true, highPrecisionDash: true })], | ||
}), | ||
]; | ||
|
||
layers.forEach((layer) => addLayer(layer)); | ||
|
||
return () => { | ||
layers.forEach((layer) => removeLayer(layer.id)); | ||
}; | ||
}, [addLayer, beforeId, geometry, removeLayer]); | ||
|
||
return null; | ||
}; | ||
|
||
export default SelectedLocationLayer; |