|
| 1 | +import { MapContainer, TileLayer, CircleMarker, Tooltip } from "react-leaflet"; |
| 2 | +import { useNavigate } from "react-router-dom"; |
| 3 | +import type { WorldHeritageVm } from "../../../../domain/types.ts"; |
| 4 | +import "leaflet/dist/leaflet.css"; |
| 5 | + |
| 6 | +type Props = { |
| 7 | + items: WorldHeritageVm[]; |
| 8 | +}; |
| 9 | + |
| 10 | +const CATEGORY_COLOR: Record<string, string> = { |
| 11 | + Cultural: "#f59e0b", |
| 12 | + Natural: "#22c55e", |
| 13 | + Mixed: "#ef4444", |
| 14 | +}; |
| 15 | + |
| 16 | +const getColor = (category: string): string => CATEGORY_COLOR[category] ?? "#6366f1"; |
| 17 | + |
| 18 | +const isValidCoordinate = (lat: number | null, lng: number | null): lat is number => |
| 19 | + lat !== null && lng !== null && lat !== 0 && lng !== 0; |
| 20 | + |
| 21 | +export function SearchResultMapComponent({ items }: Props) { |
| 22 | + const navigate = useNavigate(); |
| 23 | + |
| 24 | + const validItems = items.filter((item) => isValidCoordinate(item.latitude, item.longitude)); |
| 25 | + |
| 26 | + if (validItems.length === 0) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <MapContainer |
| 32 | + center={[20, 0]} |
| 33 | + zoom={2} |
| 34 | + style={{ height: "360px", width: "100%" }} |
| 35 | + scrollWheelZoom={false} |
| 36 | + > |
| 37 | + <TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> |
| 38 | + {validItems.map((item) => ( |
| 39 | + <CircleMarker |
| 40 | + key={item.id} |
| 41 | + center={[item.latitude as number, item.longitude as number]} |
| 42 | + radius={7} |
| 43 | + pathOptions={{ |
| 44 | + color: getColor(item.category), |
| 45 | + fillColor: getColor(item.category), |
| 46 | + fillOpacity: 0.8, |
| 47 | + }} |
| 48 | + eventHandlers={{ |
| 49 | + click: () => navigate(`/heritages/${item.id}`), |
| 50 | + }} |
| 51 | + > |
| 52 | + <Tooltip> |
| 53 | + <div className="text-xs font-semibold">{item.name}</div> |
| 54 | + <div className="text-xs text-zinc-500">{item.category}</div> |
| 55 | + </Tooltip> |
| 56 | + </CircleMarker> |
| 57 | + ))} |
| 58 | + </MapContainer> |
| 59 | + ); |
| 60 | +} |
0 commit comments