'use client' import { geoEqualEarth } from 'd3-geo' import { ComposableMap, Geographies, Geography, Marker, ZoomableGroup } from 'react-simple-maps' import { cn } from 'ui' import GeographyData from './MapData.json' import { MAPPED_REGIONS } from './Regions.constants' import { RegionTooltip } from './RegionTooltip' /** * The viewport is sized and framed so the land mass fills it edge to edge — the map is * letterboxed inside its container otherwise. At scale 155 the world (Antarctica is not in * MapData.json) projects to 828.3 x 361.5, so MAP_ZOOM shrinks it onto MAP_WIDTH and * MAP_CENTER is the coordinate at the middle of that bounding box. */ const MAP_SCALE = 155 const MAP_WIDTH = 800 const MAP_HEIGHT = 349 const MAP_ZOOM = 0.9658 const MAP_CENTER: [number, number] = [1.08, 6.9] const projection = geoEqualEarth() .scale(MAP_SCALE) .translate([MAP_WIDTH / 2, MAP_HEIGHT / 2]) const [centerX, centerY] = projection(MAP_CENTER) ?? [MAP_WIDTH / 2, MAP_HEIGHT / 2] /** * Where each marker sits as a percentage of the container, mirroring what ZoomableGroup does to * the SVG: `translate(width / 2 - centre * zoom) scale(zoom)`. The tooltip is positioned with * these because anything rendered inside the SVG scales with the viewBox — text ends up at half * size on a phone and twice that on a desktop. */ const REGION_POSITIONS = new Map( MAPPED_REGIONS.map((region) => { const [x, y] = projection(region.coordinates) ?? [centerX, centerY] return [ region.code, { left: ((MAP_WIDTH / 2 + (x - centerX) * MAP_ZOOM) / MAP_WIDTH) * 100, top: ((MAP_HEIGHT / 2 + (y - centerY) * MAP_ZOOM) / MAP_HEIGHT) * 100, }, ] }) ) interface Props { selectedRegionCode: string | null hoveredRegionCode?: string onRegionHover: (code?: string) => void onRegionSelect: (code: string) => void onRegionClear: () => void } export const RegionsMap = ({ selectedRegionCode, hoveredRegionCode, onRegionHover, onRegionSelect, onRegionClear, }: Props) => { // Hovering previews a region, so it wins over the selection const shownRegionCode = hoveredRegionCode ?? selectedRegionCode const shownRegion = MAPPED_REGIONS.find((region) => region.code === shownRegionCode) const shownPosition = shownRegion ? REGION_POSITIONS.get(shownRegion.code) : undefined // Markers in the right half of the map get their tooltip on the left, so it can't run off const isTooltipOnLeft = (shownPosition?.left ?? 0) > 50 return ( <> false} > {({ geographies }) => geographies.map((geo) => ( )) } {MAPPED_REGIONS.map((region) => { const isSelected = region.code === selectedRegionCode const isHighlighted = isSelected || region.code === hoveredRegionCode return ( onRegionHover(region.code)} onMouseLeave={() => onRegionHover(undefined)} onClick={() => onRegionSelect(region.code)} > {isSelected && } ) })} {shownRegion && shownPosition && (
)} ) }