feat(页面): 添加区域探索与国家边界功能

This commit is contained in:
richarjiang
2026-03-13 16:25:35 +08:00
parent e79d721615
commit 6c2a45b257
15 changed files with 338 additions and 338 deletions

View File

@@ -2,8 +2,9 @@
import { useEffect, useRef, useState, useCallback, useMemo } from "react";
import dynamic from "next/dynamic";
import { useTranslations } from "next-intl";
import { useTranslations, useLocale } from "next-intl";
import { useHeatmapData, type HeatmapPoint } from "@/hooks/use-heatmap-data";
import { useCountryData, type LabelData } from "./use-country-data";
import { GlobeControls } from "./globe-controls";
function GlobeLoading() {
@@ -34,6 +35,7 @@ interface ArcData {
export function GlobeView() {
const t = useTranslations("globe");
const tPopup = useTranslations("clawPopup");
const locale = useLocale();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const globeRef = useRef<any>(undefined);
const containerRef = useRef<HTMLDivElement>(null);
@@ -41,6 +43,7 @@ export function GlobeView() {
const [hoveredPoint, setHoveredPoint] = useState<HeatmapPoint | null>(null);
const [selectedPoint, setSelectedPoint] = useState<HeatmapPoint | null>(null);
const { points } = useHeatmapData(30000);
const { countries, labels } = useCountryData(locale);
// Generate arcs only between online points
const arcs = useMemo((): ArcData[] => {
@@ -117,6 +120,32 @@ export function GlobeView() {
globeImageUrl="//unpkg.com/three-globe/example/img/earth-dark.jpg"
atmosphereColor="#00f0ff"
atmosphereAltitude={0.15}
// Country polygons
polygonsData={countries}
polygonCapColor={() => "rgba(26, 31, 46, 0.6)"}
polygonSideColor={() => "rgba(0, 240, 255, 0.05)"}
polygonStrokeColor={() => "rgba(0, 240, 255, 0.15)"}
polygonAltitude={0.005}
// Country name labels (use HTML elements for CJK support)
htmlElementsData={labels}
htmlLat={(d: object) => (d as LabelData).lat}
htmlLng={(d: object) => (d as LabelData).lng}
htmlAltitude={0.01}
htmlElement={(d: object) => {
const label = d as LabelData;
const el = document.createElement("div");
el.textContent = label.name;
el.style.color = "rgba(0, 240, 255, 0.5)";
el.style.fontSize = "10px";
el.style.fontFamily = "system-ui, -apple-system, sans-serif";
el.style.pointerEvents = "none";
el.style.userSelect = "none";
el.style.whiteSpace = "nowrap";
el.style.textShadow = "0 0 4px rgba(0, 240, 255, 0.3)";
return el;
}}
// Graticules
showGraticules={true}
// Points
pointsData={points}
pointLat={(d: object) => (d as HeatmapPoint).lat}

View File

@@ -0,0 +1,53 @@
import { useState, useEffect, useMemo } from "react";
import { feature } from "topojson-client";
import type { Topology, GeometryCollection } from "topojson-specification";
import type { FeatureCollection, Feature, Geometry } from "geojson";
import { COUNTRY_LABELS } from "@/lib/geo/country-labels";
const TOPOJSON_URL =
"https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json";
export interface LabelData {
lat: number;
lng: number;
name: string;
}
export function useCountryData(locale: string) {
const [countries, setCountries] = useState<Feature<Geometry>[]>([]);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
let cancelled = false;
fetch(TOPOJSON_URL)
.then((res) => res.json())
.then((topo: Topology) => {
if (cancelled) return;
const geo = feature(
topo,
topo.objects.countries as GeometryCollection
) as FeatureCollection;
setCountries(geo.features);
setIsLoading(false);
})
.catch(() => {
if (!cancelled) setIsLoading(false);
});
return () => {
cancelled = true;
};
}, []);
const labels: LabelData[] = useMemo(() => {
const lang = locale === "zh" ? "zh" : "en";
return COUNTRY_LABELS.map((c) => ({
lat: c.lat,
lng: c.lng,
name: c[lang],
}));
}, [locale]);
return { countries, labels, isLoading };
}

View File

@@ -1,16 +1,11 @@
"use client";
import { Activity, Globe2, Map } from "lucide-react";
import { Activity } from "lucide-react";
import { useTranslations } from "next-intl";
import { Link } from "@/i18n/navigation";
import { cn } from "@/lib/utils";
import { LanguageSwitcher } from "./language-switcher";
interface NavbarProps {
activeView?: "globe" | "map";
}
export function Navbar({ activeView = "globe" }: NavbarProps) {
export function Navbar() {
const t = useTranslations("navbar");
return (
@@ -26,33 +21,6 @@ export function Navbar({ activeView = "globe" }: NavbarProps) {
</span>
</Link>
<div className="flex items-center gap-1 rounded-lg border border-white/5 bg-white/5 p-1">
<Link
href="/"
className={cn(
"flex items-center gap-1.5 rounded-md px-3 py-1.5 text-xs font-medium transition-all",
activeView === "globe"
? "bg-[var(--accent-cyan)]/10 text-[var(--accent-cyan)]"
: "text-[var(--text-muted)] hover:text-[var(--text-secondary)]"
)}
>
<Globe2 className="h-3.5 w-3.5" />
{t("globe")}
</Link>
<Link
href="/continent/asia"
className={cn(
"flex items-center gap-1.5 rounded-md px-3 py-1.5 text-xs font-medium transition-all",
activeView === "map"
? "bg-[var(--accent-cyan)]/10 text-[var(--accent-cyan)]"
: "text-[var(--text-muted)] hover:text-[var(--text-secondary)]"
)}
>
<Map className="h-3.5 w-3.5" />
{t("map")}
</Link>
</div>
<div className="flex items-center gap-3">
<div className="flex items-center gap-1.5">
<Activity className="h-3.5 w-3.5 text-[var(--accent-green)]" />

View File

@@ -1,49 +0,0 @@
"use client";
import { Globe2, Map } from "lucide-react";
import { useTranslations } from "next-intl";
import { Link } from "@/i18n/navigation";
import { cn } from "@/lib/utils";
const continentSlugs = ["asia", "europe", "americas", "africa", "oceania"] as const;
interface ViewSwitcherProps {
activeContinent?: string;
}
export function ViewSwitcher({ activeContinent }: ViewSwitcherProps) {
const tSwitcher = useTranslations("viewSwitcher");
const tContinents = useTranslations("continents");
return (
<div className="flex flex-wrap items-center gap-2">
<Link
href="/"
className={cn(
"flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-xs font-medium transition-all",
!activeContinent
? "border-[var(--accent-cyan)]/30 bg-[var(--accent-cyan)]/10 text-[var(--accent-cyan)]"
: "border-white/5 text-[var(--text-muted)] hover:border-white/10 hover:text-[var(--text-secondary)]"
)}
>
<Globe2 className="h-3 w-3" />
{tSwitcher("global")}
</Link>
{continentSlugs.map((slug) => (
<Link
key={slug}
href={`/continent/${slug}`}
className={cn(
"flex items-center gap-1.5 rounded-lg border px-3 py-1.5 text-xs font-medium transition-all",
activeContinent === slug
? "border-[var(--accent-purple)]/30 bg-[var(--accent-purple)]/10 text-[var(--accent-purple)]"
: "border-white/5 text-[var(--text-muted)] hover:border-white/10 hover:text-[var(--text-secondary)]"
)}
>
<Map className="h-3 w-3" />
{tContinents(slug)}
</Link>
))}
</div>
);
}

View File

@@ -7,6 +7,7 @@ import type { MapLayerMouseEvent, LngLatLike, MapRef } from "react-map-gl/maplib
import type { LayerSpecification } from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import { useHeatmapData, type HeatmapPoint } from "@/hooks/use-heatmap-data";
import { cn } from "@/lib/utils";
import { MapPopup } from "./map-popup";
const CARTO_STYLE =
@@ -88,9 +89,10 @@ const circleLayer: LayerSpecification = {
interface ContinentMapProps {
slug: string;
className?: string;
}
export function ContinentMap({ slug }: ContinentMapProps) {
export function ContinentMap({ slug, className }: ContinentMapProps) {
const locale = useLocale();
const config = continentConfigs[slug] ?? continentConfigs.asia;
const { points } = useHeatmapData(30000);
@@ -162,10 +164,10 @@ export function ContinentMap({ slug }: ContinentMapProps) {
);
return (
<div className="overflow-hidden rounded-xl border border-white/5 bg-[var(--bg-secondary)]">
<div className={cn("overflow-hidden rounded-xl border border-white/5 bg-[var(--bg-secondary)]", className)}>
<Map
initialViewState={config}
style={{ width: "100%", height: "calc(100vh - 12rem)" }}
style={{ width: "100%", height: "100%" }}
mapStyle={CARTO_STYLE}
interactiveLayerIds={["claw-circles"]}
onClick={handleClick}