import { useEffect, useState } from 'react'; interface CountryData { code: string; name: string; count: number; percentage: number; } interface CountryAnalysisProps { ip?: string; // Si fourni, affiche stats relatives à cette IP asn?: string; // Si fourni, affiche stats relatives à cet ASN } interface CountryAnalysisData { ip_country?: { code: string; name: string }; asn_countries: CountryData[]; } export function CountryAnalysis({ ip, asn }: CountryAnalysisProps) { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { const fetchCountryAnalysis = async () => { setLoading(true); try { if (ip) { // Mode Investigation IP: Récupérer le pays de l'IP + répartition ASN const response = await fetch(`/api/analysis/${encodeURIComponent(ip)}/country`); if (!response.ok) throw new Error('Erreur chargement pays'); const result = await response.json(); setData(result); } else if (asn) { // Mode Investigation ASN const response = await fetch(`/api/analysis/asn/${encodeURIComponent(asn)}/country`); if (!response.ok) throw new Error('Erreur chargement pays'); const result = await response.json(); setData(result); } else { // Mode Global (stats générales) const response = await fetch('/api/analysis/country?days=1'); if (!response.ok) throw new Error('Erreur chargement pays'); const result = await response.json(); setData({ ip_country: undefined, asn_countries: result.top_countries || [] }); } } catch (err) { setError(err instanceof Error ? err.message : 'Erreur inconnue'); } finally { setLoading(false); } }; fetchCountryAnalysis(); }, [ip, asn]); if (loading) { return (
Chargement...
); } if (error || !data) { return (
Erreur: {error || 'Données non disponibles'}
); } const getFlag = (code: string) => { return code.toUpperCase().replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397)); }; // Mode Investigation IP avec pays unique if (ip && data.ip_country) { return (

2. PAYS DE L'IP

{/* Pays de l'IP */}
{getFlag(data.ip_country.code)}
{data.ip_country.name} ({data.ip_country.code})
Pays de l'IP
{/* Répartition ASN par pays */} {data.asn_countries.length > 0 && (
Autres pays du même ASN (24h)
{data.asn_countries.slice(0, 5).map((country, idx) => (
{getFlag(country.code)} {country.name}
{country.count}
{country.percentage.toFixed(1)}%
))}
)}
); } // Mode Global ou ASN const getThreatColor = (percentage: number, baseline: number) => { if (baseline > 0 && percentage > baseline * 2) return 'bg-threat-high'; if (percentage > 30) return 'bg-threat-medium'; return 'bg-accent-primary'; }; return (

{asn ? '2. TOP Pays (ASN)' : '2. TOP Pays (Global)'}

{data.asn_countries.map((country, idx) => { const baselinePct = 0; // Pas de baseline en mode ASN return (
{getFlag(country.code)}
{country.name} ({country.code})
{country.count}
{country.percentage.toFixed(1)}%
); })}
); }