|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useMemo } from "react"; |
| 4 | +import type { MouseEvent as ReactMouseEvent } from "react"; |
| 5 | +import Supercluster from "supercluster"; |
| 6 | +import { Marker, type MarkerEvent } from "react-map-gl/maplibre"; |
| 7 | +import type { FeatureCollection, Point } from "geojson"; |
| 8 | + |
| 9 | +import MapPin from "@/components/MapPin"; |
| 10 | +import type { ListingCoordinates, ListingMarker } from "@/types/listing"; |
| 11 | + |
| 12 | +import ClusterPinEnter, { ClusterPinEnterProvider } from "./ClusterPinEnter"; |
| 13 | +import { ListingMapPinMarker } from "./MapPinLayer"; |
| 14 | + |
| 15 | +type MapMirroredClusterLayerProps = { |
| 16 | + geoJson: FeatureCollection<Point>; |
| 17 | + listingsById: Map<number, ListingMarker>; |
| 18 | + bounds: [number, number, number, number] | null; |
| 19 | + zoom: number; |
| 20 | + selectedListingId: number | null; |
| 21 | + excludedListingId: number | null; |
| 22 | + markerLabel: string; |
| 23 | + onClusterClick: ( |
| 24 | + longitude: number, |
| 25 | + latitude: number, |
| 26 | + expansionZoom: number |
| 27 | + ) => void; |
| 28 | + onMarkerClick: (listing: ListingMarker) => void; |
| 29 | + clusterMaxZoom?: number; |
| 30 | + clusterRadius?: number; |
| 31 | +}; |
| 32 | + |
| 33 | +type VisibleMirroredPin = { |
| 34 | + key: string; |
| 35 | + longitude: number; |
| 36 | + latitude: number; |
| 37 | + isCluster: boolean; |
| 38 | + clusterId?: number; |
| 39 | + listingId?: number; |
| 40 | +}; |
| 41 | + |
| 42 | +function resolveVisibleMirroredPins( |
| 43 | + features: ReturnType<Supercluster["getClusters"]>, |
| 44 | + excludedListingId: number | null |
| 45 | +): VisibleMirroredPin[] { |
| 46 | + const visiblePins: VisibleMirroredPin[] = []; |
| 47 | + |
| 48 | + for (const feature of features) { |
| 49 | + const [longitude, latitude] = feature.geometry.coordinates as [ |
| 50 | + number, |
| 51 | + number, |
| 52 | + ]; |
| 53 | + const isCluster = Boolean(feature.properties?.cluster); |
| 54 | + |
| 55 | + if (isCluster) { |
| 56 | + const clusterId = feature.properties.cluster_id as number; |
| 57 | + visiblePins.push({ |
| 58 | + key: `mirrored-cluster-${clusterId}`, |
| 59 | + longitude, |
| 60 | + latitude, |
| 61 | + isCluster: true, |
| 62 | + clusterId, |
| 63 | + }); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + const listingId = feature.properties?.id as number; |
| 68 | + if (listingId === excludedListingId) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + visiblePins.push({ |
| 73 | + key: `mirrored-${listingId}`, |
| 74 | + longitude, |
| 75 | + latitude, |
| 76 | + isCluster: false, |
| 77 | + listingId, |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + return visiblePins; |
| 82 | +} |
| 83 | + |
| 84 | +export default function MapMirroredClusterLayer({ |
| 85 | + geoJson, |
| 86 | + listingsById, |
| 87 | + bounds, |
| 88 | + zoom, |
| 89 | + selectedListingId, |
| 90 | + excludedListingId, |
| 91 | + markerLabel, |
| 92 | + onClusterClick, |
| 93 | + onMarkerClick, |
| 94 | + clusterMaxZoom = 15, |
| 95 | + clusterRadius = 80, |
| 96 | +}: MapMirroredClusterLayerProps) { |
| 97 | + const clusterIndex = useMemo(() => { |
| 98 | + const index = new Supercluster({ |
| 99 | + radius: clusterRadius, |
| 100 | + maxZoom: clusterMaxZoom, |
| 101 | + }); |
| 102 | + |
| 103 | + index.load( |
| 104 | + geoJson.features.map((feature) => ({ |
| 105 | + type: "Feature" as const, |
| 106 | + properties: { ...feature.properties }, |
| 107 | + geometry: feature.geometry, |
| 108 | + })) |
| 109 | + ); |
| 110 | + |
| 111 | + return index; |
| 112 | + }, [clusterMaxZoom, clusterRadius, geoJson]); |
| 113 | + |
| 114 | + const visibleClusters = useMemo(() => { |
| 115 | + if (!bounds) return []; |
| 116 | + return clusterIndex.getClusters(bounds, Math.floor(zoom)); |
| 117 | + }, [bounds, clusterIndex, zoom]); |
| 118 | + |
| 119 | + const visiblePins = useMemo( |
| 120 | + () => resolveVisibleMirroredPins(visibleClusters, excludedListingId), |
| 121 | + [excludedListingId, visibleClusters] |
| 122 | + ); |
| 123 | + |
| 124 | + return ( |
| 125 | + <ClusterPinEnterProvider> |
| 126 | + {visiblePins.map((pin) => { |
| 127 | + if (pin.isCluster && pin.clusterId !== undefined) { |
| 128 | + const activateCluster = () => { |
| 129 | + onClusterClick( |
| 130 | + pin.longitude, |
| 131 | + pin.latitude, |
| 132 | + clusterIndex.getClusterExpansionZoom(pin.clusterId!) |
| 133 | + ); |
| 134 | + }; |
| 135 | + |
| 136 | + const handlePinClick = (event: ReactMouseEvent<HTMLDivElement>) => { |
| 137 | + event.preventDefault(); |
| 138 | + event.stopPropagation(); |
| 139 | + event.nativeEvent.stopPropagation(); |
| 140 | + activateCluster(); |
| 141 | + }; |
| 142 | + |
| 143 | + const handleMarkerClick = ( |
| 144 | + event: MarkerEvent<globalThis.MouseEvent> |
| 145 | + ) => { |
| 146 | + event.originalEvent.stopPropagation(); |
| 147 | + activateCluster(); |
| 148 | + }; |
| 149 | + |
| 150 | + return ( |
| 151 | + <Marker |
| 152 | + key={pin.key} |
| 153 | + longitude={pin.longitude} |
| 154 | + latitude={pin.latitude} |
| 155 | + anchor="center" |
| 156 | + onClick={handleMarkerClick} |
| 157 | + > |
| 158 | + <ClusterPinEnter> |
| 159 | + <MapPin |
| 160 | + markerId={pin.key} |
| 161 | + type="community" |
| 162 | + onClick={handlePinClick} |
| 163 | + /> |
| 164 | + </ClusterPinEnter> |
| 165 | + </Marker> |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + const listing = |
| 170 | + pin.listingId !== undefined |
| 171 | + ? listingsById.get(pin.listingId) |
| 172 | + : undefined; |
| 173 | + if (!listing) return null; |
| 174 | + |
| 175 | + const coords = listing.coordinates as ListingCoordinates; |
| 176 | + const isSelected = selectedListingId === listing.id; |
| 177 | + |
| 178 | + return ( |
| 179 | + <ListingMapPinMarker |
| 180 | + key={pin.key} |
| 181 | + listing={listing} |
| 182 | + coords={coords} |
| 183 | + isSelected={isSelected} |
| 184 | + markerLabel={markerLabel} |
| 185 | + onMarkerClick={onMarkerClick} |
| 186 | + withClusterEnterAnimation |
| 187 | + /> |
| 188 | + ); |
| 189 | + })} |
| 190 | + </ClusterPinEnterProvider> |
| 191 | + ); |
| 192 | +} |
0 commit comments