-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32df026
commit f043f5e
Showing
6 changed files
with
129 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"use client"; | ||
|
||
import { countryCodeMapping } from "@/lib/geo"; | ||
import { geoEqualEarth, geoPath } from "d3-geo"; | ||
import { useTranslations } from "next-intl"; | ||
import { useState } from "react"; | ||
|
||
interface InteractiveMapProps { | ||
countries: string[]; | ||
serverCounts: { [key: string]: number }; | ||
width: number; | ||
height: number; | ||
filteredFeatures: any[]; | ||
} | ||
|
||
export function InteractiveMap({ | ||
countries, | ||
serverCounts, | ||
width, | ||
height, | ||
filteredFeatures, | ||
}: InteractiveMapProps) { | ||
const t = useTranslations("Global"); | ||
|
||
const [tooltipData, setTooltipData] = useState<{ | ||
centroid: [number, number]; | ||
country: string; | ||
count: number; | ||
} | null>(null); | ||
|
||
const countries_alpha3 = countries | ||
.map((code) => countryCodeMapping[code]) | ||
.filter((code) => code !== undefined); | ||
|
||
const projection = geoEqualEarth() | ||
.scale(180) | ||
.translate([width / 2, height / 2]); | ||
|
||
const path = geoPath().projection(projection); | ||
|
||
return ( | ||
<div className="relative w-full aspect-[2/1]"> | ||
<svg | ||
width={width} | ||
height={height} | ||
viewBox={`0 0 ${width} ${height}`} | ||
xmlns="http://www.w3.org/2000/svg" | ||
className="w-full h-auto" | ||
> | ||
<defs> | ||
<pattern id="dots" width="2" height="2" patternUnits="userSpaceOnUse"> | ||
<circle cx="1" cy="1" r="0.5" fill="currentColor" /> | ||
</pattern> | ||
</defs> | ||
<g> | ||
{filteredFeatures.map((feature, index) => { | ||
const isHighlighted = countries_alpha3.includes( | ||
feature.properties.iso_a3, | ||
); | ||
const countryCode = Object.entries(countryCodeMapping).find( | ||
([_, alpha3]) => alpha3 === feature.properties.iso_a3, | ||
)?.[0]; | ||
const serverCount = countryCode | ||
? serverCounts[countryCode] || 0 | ||
: 0; | ||
|
||
return ( | ||
<path | ||
key={index} | ||
d={path(feature) || ""} | ||
className={ | ||
isHighlighted | ||
? "fill-orange-500 hover:fill-orange-400 stroke-orange-500 dark:stroke-amber-900 dark:fill-amber-900 dark:hover:fill-amber-800 transition-all cursor-pointer" | ||
: "fill-neutral-200/50 dark:fill-neutral-800 stroke-neutral-300/40 dark:stroke-neutral-700 stroke-[0.5]" | ||
} | ||
onMouseEnter={() => { | ||
if (isHighlighted && path.centroid(feature)) { | ||
setTooltipData({ | ||
centroid: path.centroid(feature), | ||
country: feature.properties.name, | ||
count: serverCount, | ||
}); | ||
} | ||
}} | ||
onMouseLeave={() => setTooltipData(null)} | ||
/> | ||
); | ||
})} | ||
</g> | ||
</svg> | ||
{tooltipData && ( | ||
<div | ||
className="absolute pointer-events-none bg-white dark:bg-neutral-800 px-2 py-1 rounded shadow-lg text-sm" | ||
style={{ | ||
left: tooltipData.centroid[0], | ||
top: tooltipData.centroid[1], | ||
transform: "translate(-50%, -50%)", | ||
}} | ||
> | ||
<p className="font-medium">{tooltipData.country}</p> | ||
<p className="text-neutral-600 dark:text-neutral-400"> | ||
{tooltipData.count} {t("Servers")} | ||
</p> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters