import { useContext } from 'react'; import { PieChart } from '@/components/charts/PieChart'; import { useMessages } from '@/components/hooks'; import { GridRow } from '@/components/common/GridRow'; import { ListTable } from '@/components/metrics/ListTable'; import { MetricCard } from '@/components/metrics/MetricCard'; import { MetricsBar } from '@/components/metrics/MetricsBar'; import { CHART_COLORS } from '@/lib/constants'; import { formatLongNumber } from '@/lib/format'; import { ReportContext } from '../[reportId]/Report'; export interface AttributionViewProps { isLoading?: boolean; } export function AttributionView({ isLoading }: AttributionViewProps) { const { formatMessage, labels } = useMessages(); const { report } = useContext(ReportContext); const { data, parameters: { currency }, } = report || {}; const ATTRIBUTION_PARAMS = [ { value: 'referrer', label: formatMessage(labels.referrers) }, { value: 'paidAds', label: formatMessage(labels.paidAds) }, ]; if (!data) { return null; } const { pageviews, visitors, visits } = data.total; const metrics = data ? [ { value: pageviews, label: formatMessage(labels.views), formatValue: formatLongNumber, }, { value: visits, label: formatMessage(labels.visits), formatValue: formatLongNumber, }, { value: visitors, label: formatMessage(labels.visitors), formatValue: formatLongNumber, }, ] : []; function UTMTable(UTMTableProps: { data: any; title: string; utm: string }) { const { data, title, utm } = UTMTableProps; const total = data[utm].reduce((sum, { value }) => { return +sum + +value; }, 0); return ( ({ x: name, y: Number(value), z: (value / total) * 100, }))} /> ); } return (
{metrics?.map(({ label, value, formatValue }) => { return ; })} {ATTRIBUTION_PARAMS.map(({ value, label }) => { const items = data[value]; const total = items.reduce((sum, { value }) => { return +sum + +value; }, 0); const chartData = { labels: items.map(({ name }) => name), datasets: [ { data: items.map(({ value }) => value), backgroundColor: CHART_COLORS, borderWidth: 0, }, ], }; return (
{label}
({ x: name, y: Number(value), z: (value / total) * 100, }))} />
); })} <>
); } export default AttributionView;