import { Globe, List } from 'lucide-react'; import React, { useCallback, useEffect, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { ToggleGroup, ToggleGroupItem } from '../../components/ui/toggle-group'; import { BlockFillProps, StatsResult } from '../../utils/admin'; import { getCountryName } from '../data/countries'; import BarList from './BarList'; import { Card } from './Card'; import CountryMap from './CountryMap'; interface Props { data?: StatsResult, onAddRule?: BlockFillProps['onAddRule'], } export default function Country( props: Props ) { const { data, onAddRule } = props; const [ view, setView ] = useState( 'map' ); const [ containerEl, setContainerEl ] = useState( null ); const [ width, setWidth ] = useState( 0 ); const containerRef = useCallback( ( node: HTMLDivElement | null ) => { setContainerEl( node ); }, [] ); useEffect( () => { if ( ! containerEl ) { return; } const updateWidth = () => setWidth( containerEl.offsetWidth ); updateWidth(); const observer = new ResizeObserver( updateWidth ); observer.observe( containerEl ); return () => observer.disconnect(); }, [ containerEl ] ); if ( ! data || ! data.stats || ! data.stats.by_country ) { return null; } const countryData = data.stats.by_country; const mapHeight = Math.round( width * 0.5 ); const topCountries = Object.entries( countryData ) .sort( ( [ , a ], [ , b ] ) => b - a ) .slice( 0, 5 ) .map( ( [ code, value ] ) => ( { name: getCountryName( code ), value, } ) ); return (

{ __( 'Country', 'altis' ) }

v && setView( v ) } >
{ view === 'map' ? (
{ width > 0 && ( onAddRule( { field: 'endpoint.Location.Country', value: code, type: 'string', operator: '=', } ) : undefined } /> ) }
) : ( ) }
); }