-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNearbyStations.tsx
More file actions
78 lines (71 loc) · 2.11 KB
/
NearbyStations.tsx
File metadata and controls
78 lines (71 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { useRef, useCallback, useMemo } from "react";
import {
NeapsProvider,
StationsMap,
NearbyStations,
createQueryClient,
type StationSummary,
} from "@neaps/react";
import { hydrate, type DehydratedState } from "@tanstack/react-query";
import type { MapRef } from "react-map-gl/maplibre";
import "@neaps/react/styles.css";
import { API_HOST } from "../../utils/constants";
import { useMapStyle } from "../../utils/useMapStyle";
interface Props {
stationId: string;
latitude: number;
longitude: number;
dehydratedState?: DehydratedState;
}
export function NearbyStationsIsland({
stationId,
latitude,
longitude,
dehydratedState,
}: Props) {
const queryClient = useMemo(() => {
const client = createQueryClient();
if (dehydratedState) {
hydrate(client, dehydratedState);
}
return client;
}, [dehydratedState]);
const mapStyle = useMapStyle();
const mapRef = useRef<MapRef>(null);
const handleStationSelect = useCallback((station: StationSummary) => {
window.location.href = `/tides/stations/${station.id}`;
}, []);
const handleHover = useCallback((station: StationSummary) => {
mapRef.current?.panTo([station.longitude, station.latitude]);
}, []);
const handleHoverEnd = useCallback(() => {
mapRef.current?.panTo([longitude, latitude]);
}, [longitude, latitude]);
return (
<NeapsProvider baseUrl={API_HOST} queryClient={queryClient}>
<div className="flex flex-col gap-4">
<StationsMap
ref={mapRef}
mapStyle={mapStyle}
initialViewState={{
longitude,
latitude,
zoom: 11,
}}
focusStation={stationId}
clustering={false}
showGeolocation={false}
popupContent="simple"
onStationSelect={handleStationSelect}
className="aspect-video overflow-hidden rounded-lg border border-(--border)"
/>
<NearbyStations
stationId={stationId}
onStationSelect={handleStationSelect}
onHover={handleHover}
onHoverEnd={handleHoverEnd}
/>
</div>
</NeapsProvider>
);
}