"use client"; import { useState } from 'react'; import { GeoData } from '@/app/api/types'; interface GeoAnalyticsProps { data: GeoData[]; } export default function GeoAnalytics({ data }: GeoAnalyticsProps) { const [viewMode, setViewMode] = useState<'country' | 'city' | 'region' | 'continent'>('country'); // 安全地格式化数字 const formatNumber = (value: number | undefined | null): string => { if (value === undefined || value === null) return '0'; return value.toLocaleString(); }; // 安全地格式化百分比 const formatPercent = (value: number | undefined | null): string => { if (value === undefined || value === null) return '0'; return value.toFixed(1); }; const sortedData = [...data].sort((a, b) => (b.visits || 0) - (a.visits || 0)); // Handle tab selection - only change local view mode const handleViewModeChange = (mode: 'country' | 'city' | 'region' | 'continent') => { setViewMode(mode); // Only change the local view mode, no callback to parent }; return (
{/* Tabs for geographic levels */}
{/* Table with added area column */}
{sortedData.length > 0 ? ( sortedData.map((item, index) => ( )) ) : ( )}
{viewMode === 'country' ? 'Country' : viewMode === 'city' ? 'City' : viewMode === 'region' ? 'Region' : 'Continent'} {viewMode === 'country' ? 'Countries' : viewMode === 'city' ? 'Cities' : viewMode === 'region' ? 'Regions' : 'Continents'} Visits Unique Visitors % of Total
{item.location || 'Unknown'} {item.area || ''} {formatNumber(item.visits)} {formatNumber(item.visitors)}
{formatPercent(item.percentage)}%
No location data available
); }