File size: 9,914 Bytes
d8804ab
 
b50322f
d8804ab
 
 
 
587459d
 
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587459d
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587459d
d8804ab
 
 
 
 
 
 
 
 
 
 
 
 
587459d
 
 
 
 
 
 
 
b50322f
587459d
d8804ab
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"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<string, string> = {
  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 }) => (
  <rect x={x} y={y} width={3} height={3} fill="var(--ds-gray-400)" fillOpacity={0.5} />
));
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 (
      <motion.rect
        x={x}
        y={y}
        width={3}
        height={3}
        fill={color}
        initial={{ scale: 0, opacity: 0 }}
        animate={animate}
        transition={transition}
        style={{
          willChange: canPulse && cityDistanceRank < 10 ? "transform, opacity" : undefined,
        }}
      />
    );
  }
);
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 }> }) => (
  <g>
    {pixels.map((p) => (
      <StaticPixel key={p.key} x={p.x} y={p.y} />
    ))}
  </g>
));
StaticPixelsGroup.displayName = "StaticPixelsGroup";

const AnimatedPixelsGroup = memo(({ pixels }: { pixels: Array<{ key: string; x: number; y: number; color: string; canPulse: boolean; cityDistanceRank: number }> }) => (
  <g>
    {pixels.map((p) => (
      <AnimatedPixel
        key={p.key}
        x={p.x}
        y={p.y}
        color={p.color}
        canPulse={p.canPulse}
        cityDistanceRank={p.cityDistanceRank}
      />
    ))}
  </g>
));
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;
}) => (
  <g className="pointer-events-auto">
    {markers.map((marker, index) => {
      const coords = projection(marker.coordinates);
      if (!coords) return null;
      const [x, y] = coords;
      return (
        <g
          key={marker.id}
          transform={`translate(${x}, ${y})`}
        >
          <motion.g
            initial={{ scale: 0, opacity: 0 }}
            animate={{ scale: 1.5, opacity: 1 }}
            transition={{
              type: "spring",
              stiffness: 260,
              damping: 20,
              delay: delays[index],
            }}
            onMouseEnter={() => onHover(marker)}
            onMouseLeave={() => onHover(null)}
            style={{ cursor: "pointer", pointerEvents: "auto" }}
          >
            <polygon
              data-edge={marker.id}
              data-lat={marker.coordinates[1]}
              data-lng={marker.coordinates[0]}
              fill="var(--ds-gray-1000)"
              stroke="var(--background)"
              strokeWidth={1}
              strokeOpacity={0.5}
              style={{ paintOrder: "stroke" }}
              points="0,-2.3 -2,1.2 2,1.2"
            />
          </motion.g>
        </g>
      );
    })}
  </g>
));
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<string, Array<{ lon: number; lat: number; cityDistanceRank: number }>>).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 (
    <div className="relative w-full">
      <svg
        viewBox={`0 0 ${width} ${height}`}
        className="w-full h-auto bg-transparent"
      >
        {/* Render static pixels inside a memoized group */}
        <StaticPixelsGroup pixels={staticPixels} />

        {/* Render animated pulsing pixels inside a memoized group */}
        <AnimatedPixelsGroup pixels={animatedPixels} />

        {/* Render interactive markers directly inside the same SVG for maximum performance (no world-atlas json network fetch!) */}
        <MapMarkers 
          markers={regionMarkers} 
          delays={markerDelays} 
          projection={projection} 
          onHover={handleMarkerHover} 
        />
      </svg>

      <AnimatePresence>
        {hoveredMarker && (() => {
          const coords = projection(hoveredMarker.coordinates);
          if (!coords) return null;
          
          return (
            <motion.div
              initial={{ opacity: 0, y: 5 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: 5 }}
              transition={{ duration: 0.15 }}
              className="absolute pointer-events-none z-10 bg-[var(--ds-background-200)] border border-[var(--ds-gray-200)] rounded px-2.5 py-1.5 text-xs font-mono shadow-lg whitespace-nowrap"
              style={{
                left: `${(coords[0] / width) * 100}%`,
                top: `${(coords[1] / height) * 100}%`,
                transform: "translate(-50%, -140%)",
              }}
            >
              <div className="flex flex-col gap-1">
                <div className="flex items-center gap-1.5 border-b border-border pb-1">
                  <span className="text-primary font-bold">▲</span>
                  <span className="text-[var(--ds-gray-1000)] font-bold uppercase">{hoveredMarker.id}</span>
                  <span className="text-[var(--ds-gray-500)]">·</span>
                  <span className="text-[var(--ds-gray-900)] font-semibold">{hoveredMarker.name}</span>
                </div>
                <div className="text-[10px] text-muted-foreground">
                  {t("mapHoverTooltip", { region: hoveredMarker.name })}
                </div>
              </div>
            </motion.div>
          );
        })()}
      </AnimatePresence>
    </div>
  );
}