import { recordTracksEvent } from '@automattic/calypso-analytics';
import config from '@automattic/calypso-config';
import { SimplifiedSegmentedControl, StatsCard } from '@automattic/components';
import { mobile } from '@wordpress/icons';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { useState } from 'react';
import InlineSupportLink from 'calypso/components/inline-support-link';
import PieChart from 'calypso/components/pie-chart';
import PieChartLegend from 'calypso/components/pie-chart/legend';
import StatsInfoArea from 'calypso/my-sites/stats/features/modules/shared/stats-info-area';
import { useSelector } from 'calypso/state';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { getSelectedSiteId } from 'calypso/state/ui/selectors';
import EmptyModuleCard from '../../../components/empty-module-card/empty-module-card';
import useModuleDevicesQuery, { StatsDevicesData } from '../../../hooks/use-modeule-devices-query';
import useStatsStrings from '../../../hooks/use-stats-strings';
import { QueryStatsParams } from '../../../hooks/utils';
import StatsListCard from '../../../stats-list/stats-list-card';
import StatsModulePlaceholder from '../../../stats-module/placeholder';
import StatsCardSkeleton from '../shared/stats-card-skeleton';
import type { StatsPeriodType } from '../types';
import './stats-module-devices.scss';
const OPTION_KEYS = {
SIZE: 'screensize',
BROWSER: 'browser',
OS: 'platform',
};
type SelectOptionType = {
label: string;
value: string;
};
interface StatsDevicesChartData {
id: string;
value: number;
icon: string;
name: string;
descrtipion?: string;
className?: string;
}
interface StatsModuleDevicesProps {
path: string;
className?: string;
period?: StatsPeriodType;
postId?: number;
query: QueryStatsParams;
summary?: boolean;
isLoading?: boolean;
}
const prepareChartData = (
data: Array< StatsDevicesData > | undefined
): Array< StatsDevicesChartData > => {
if ( ! data ) {
return [];
}
return data.map( ( device: StatsDevicesData ) => {
let icon;
switch ( device.key ) {
case 'desktop':
icon =
'';
break;
case 'mobile':
icon =
'';
break;
case 'tablet':
icon =
'';
break;
default:
icon =
'';
}
return {
id: device.key,
value: device.value,
icon,
name: device.label,
descrtipion: device.label,
className: `donut-${ device.key }`,
};
} );
};
const StatsModuleDevices: React.FC< StatsModuleDevicesProps > = ( {
path,
className,
isLoading,
query,
} ) => {
const { devices: devicesStrings } = useStatsStrings();
const siteId = useSelector( getSelectedSiteId ) as number;
const translate = useTranslate();
const isOdysseyStats = config.isEnabled( 'is_running_in_jetpack_site' );
const optionLabels = {
[ OPTION_KEYS.SIZE ]: {
selectLabel: translate( 'Size' ),
headerLabel: translate( 'Visitors share per screen size' ),
analyticsId: 'screensize',
},
[ OPTION_KEYS.BROWSER ]: {
selectLabel: translate( 'Browser' ),
headerLabel: translate( 'Browser' ),
analyticsId: 'browser',
},
[ OPTION_KEYS.OS ]: {
selectLabel: translate( 'OS' ),
headerLabel: translate( 'Operating System' ),
analyticsId: 'os',
},
};
const [ selectedOption, setSelectedOption ] = useState( OPTION_KEYS.SIZE );
const { isFetching, data } = useModuleDevicesQuery( siteId, selectedOption, query );
const showLoader = isLoading || isFetching;
const changeViewButton = ( selection: SelectOptionType ) => {
const filter = selection.value;
// publish an event
const event_from = isOdysseyStats ? 'jetpack_odyssey' : 'calypso';
recordTracksEvent( `${ event_from }_devices_module_menu_clicked`, {
button: optionLabels[ filter ]?.analyticsId ?? filter,
} );
setSelectedOption( filter );
};
const toggleControlComponent = (
( {
value: entry[ 0 ], // optionLabels key
label: entry[ 1 ].selectLabel, // optionLabels object value
} ) ) }
initialSelected={ selectedOption }
onSelect={ changeViewButton }
/>
);
const isSiteJetpackNotAtomic = useSelector( ( state ) =>
isJetpackSite( state, siteId, { treatAtomicAsJetpackSite: false } )
);
const supportContext = isSiteJetpackNotAtomic ? 'stats-devices-jetpack' : 'stats-devices';
const titleNodes = (
{ translate(
'The {{link}}devices and browsers{{/link}} your visitors use to access your website.',
{
comment: '{{link}} links to support documentation.',
components: {
link: ,
},
context: 'Stats: Info popover content when the Devices module has data.',
}
) }
);
const chartData = OPTION_KEYS.SIZE === selectedOption ? prepareChartData( data ) : null;
return (
<>
{ showLoader && (
) }
{ ! showLoader &&
! data?.length && ( // no data and new empty state enabled
}
isEmpty
emptyMessage={
),
},
context: 'Stats: Info box label when the Devices module is empty',
}
) }
/>
}
/>
) }
{ ! showLoader && !! data?.length && (
<>
{
// Use dedicated StatsCard for the screen size chart section.
OPTION_KEYS.SIZE === selectedOption ? (
) : (
// @ts-expect-error TODO: Refactor StatsListCard with TypeScript.
}
splitHeader
useShortNumber
mainItemLabel={ optionLabels[ selectedOption ]?.headerLabel }
toggleControl={ toggleControlComponent }
/>
)
}
>
) }
>
);
};
export { StatsModuleDevices as default, StatsModuleDevices, OPTION_KEYS };