"use client"; import { useMemo, memo, useState } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { geoMercator } from "d3-geo"; import dottedMapData from "../data/dotted-map-data.json"; import { regionMarkers, topCountries, countryRequests } from "../data/country-data"; import { useLanguage } from "@/components/LanguageContext"; const countryColors: Record = { US: "#1e40af", DE: "#FFCE00", GB: "#2563eb", IN: "#f59e0b", BR: "#FF0000", SG: "#f59e0b", JP: "#dc143c", FR: "#1d4ed8", CA: "#b91c1c", SE: "#2563eb", AU: "#3b82f6", KR: "#3b82f6", NL: "#ea580c", CN: "#991b1b", RU: "#FF0000", MX: "#15803d", ES: "#b91c1c", IT: "#15803d", PL: "#dc2626", TR: "#b91c1c", ID: "#1e40af", TH: "#15803d", VN: "#991b1b", PH: "#15803d", EG: "#1e40af", NG: "#2563eb", PK: "#FFCE00", BD: "#991b1b", AR: "#f59e0b", CO: "#FF0000", ZA: "#15803d", SA: "#FFCE00", MY: "#991b1b", CL: "#991b1b", PE: "#1e40af", AE: "#f59e0b", }; const getCountryColor = (iso2: string): string => { return countryColors[iso2] || "#666666"; }; const top10Countries = new Set( topCountries.slice(0, 10).map((c) => c.code) ); const getDotsToShow = (iso2: string): number => { const data = countryRequests[iso2]; if (!data) return 2; const rate = (data.value / 260000) * 1000; if (rate < 400) return 2; if (rate < 1000) return 4; if (rate < 5000) return 8; if (rate < 10000) return 15; if (rate < 50000) return 25; return 35; }; const StaticPixel = memo(({ x, y }: { x: number; y: number }) => ( )); StaticPixel.displayName = "StaticPixel"; const AnimatedPixel = memo( ({ x, y, color, canPulse, cityDistanceRank, }: { x: number; y: number; color: string; canPulse: boolean; cityDistanceRank: number; }) => { const delay = useMemo(() => cityDistanceRank * 0.1, [cityDistanceRank]); const animate = canPulse ? { scale: [1, 1.8, 1], opacity: [0.8, 1, 0.8], } : { scale: 1, opacity: 1 }; const transition = canPulse ? { opacity: { duration: 1, repeat: Infinity, ease: "easeInOut" as const, delay, repeatDelay: delay, }, scale: { duration: 1.5, repeat: Infinity, ease: "easeInOut" as const, delay, repeatDelay: delay, }, } : { type: "spring" as const, stiffness: 260, damping: 20 }; return ( ); } ); AnimatedPixel.displayName = "AnimatedPixel"; // Memoized group rendering components to prevent React virtual DOM diffing overhead for 2,000+ nodes on state updates const StaticPixelsGroup = memo(({ pixels }: { pixels: Array<{ key: string; x: number; y: number }> }) => ( {pixels.map((p) => ( ))} )); StaticPixelsGroup.displayName = "StaticPixelsGroup"; const AnimatedPixelsGroup = memo(({ pixels }: { pixels: Array<{ key: string; x: number; y: number; color: string; canPulse: boolean; cityDistanceRank: number }> }) => ( {pixels.map((p) => ( ))} )); AnimatedPixelsGroup.displayName = "AnimatedPixelsGroup"; const MapMarkers = memo(({ markers, delays, projection, onHover }: { markers: typeof regionMarkers; delays: number[]; projection: (coords: [number, number]) => [number, number] | null; onHover: (marker: (typeof regionMarkers)[0] | null) => void; }) => ( {markers.map((marker, index) => { const coords = projection(marker.coordinates); if (!coords) return null; const [x, y] = coords; return ( onHover(marker)} onMouseLeave={() => onHover(null)} style={{ cursor: "pointer", pointerEvents: "auto" }} > ); })} )); MapMarkers.displayName = "MapMarkers"; interface DottedMapProps { width?: number; height?: number; } export default function DottedMap({ width = 1000, height = 560 }: DottedMapProps) { const { t } = useLanguage(); const [hoveredMarker, setHoveredMarker] = useState<(typeof regionMarkers)[0] | null>(null); const handleMarkerHover = (marker: (typeof regionMarkers)[0] | null) => { setHoveredMarker(marker); }; const projection = useMemo( () => geoMercator() .scale(140) .center([15, 25]) .rotate([0, 0, 0]) .translate([width / 2, height / 2]), [width, height] ); const { staticPixels, animatedPixels } = useMemo(() => { const staticArr: Array<{ key: string; x: number; y: number }> = []; const animatedArr: Array<{ key: string; x: number; y: number; color: string; canPulse: boolean; cityDistanceRank: number; }> = []; Object.entries(dottedMapData as Record>).forEach( ([countryCode, cities]) => { const dotsToShow = getDotsToShow(countryCode); const color = getCountryColor(countryCode); const isTop10 = top10Countries.has(countryCode); cities.forEach((city) => { const coords = projection([city.lon, city.lat]); if (!coords) return; const [x, y] = coords; if (x < 0 || x > width || y < 0 || y > height) return; const key = `${countryCode}-${city.cityDistanceRank}`; const isAnimated = city.cityDistanceRank < dotsToShow; if (isAnimated) { animatedArr.push({ key, x, y, color, canPulse: isTop10 && city.cityDistanceRank < 5, cityDistanceRank: city.cityDistanceRank, }); } else { staticArr.push({ key, x, y }); } }); } ); return { staticPixels: staticArr, animatedPixels: animatedArr }; }, [projection, width, height]); const markerDelays = useMemo( () => regionMarkers.map((_, i) => (i * 0.05) % 1), [] ); return (
{/* Render static pixels inside a memoized group */} {/* Render animated pulsing pixels inside a memoized group */} {/* Render interactive markers directly inside the same SVG for maximum performance (no world-atlas json network fetch!) */} {hoveredMarker && (() => { const coords = projection(hoveredMarker.coordinates); if (!coords) return null; return (
▲ {hoveredMarker.id} · {hoveredMarker.name}
{t("mapHoverTooltip", { region: hoveredMarker.name })}
); })()}
); }