'use client' import { useState } from 'react' import { formatNumber } from '@/lib/utils/format' import * as Flags from 'country-flag-icons/react/3x2' import WorldMap from './WorldMap' interface LocationProps { countries: Array<{ country: string; pageviews: number }> cities: Array<{ city: string; country: string; pageviews: number }> } type Tab = 'countries' | 'cities' export default function Locations({ countries, cities }: LocationProps) { const [activeTab, setActiveTab] = useState('countries') const getFlagComponent = (countryCode: string) => { if (!countryCode || countryCode === 'Unknown') return null // * The API returns 2-letter country codes (e.g. US, DE) // * We cast it to the flag component name const FlagComponent = (Flags as any)[countryCode] return FlagComponent ? : null } const getCountryName = (code: string) => { if (!code || code === 'Unknown') return 'Unknown' try { const regionNames = new Intl.DisplayNames(['en'], { type: 'region' }) return regionNames.of(code) || code } catch (e) { return code } } const renderContent = () => { if (activeTab === 'countries') { if (!countries || countries.length === 0) { return

No data available

} return (
{countries.map((country, index) => (
{getFlagComponent(country.country)} {getCountryName(country.country)}
{formatNumber(country.pageviews)}
))}
) } if (activeTab === 'cities') { if (!cities || cities.length === 0) { return

No data available

} return (
{cities.map((city, index) => (
{getFlagComponent(city.country)} {city.city === 'Unknown' ? 'Unknown' : city.city}
{formatNumber(city.pageviews)}
))}
) } } return (

Locations

{renderContent()}
) }