'use client'; import { Cell, Legend, Pie, PieChart, ResponsiveContainer, Tooltip } from 'recharts'; import { CardSkeleton } from '@/components/shared/loading-skeleton'; import { EmptyState } from '@/components/shared/empty-state'; import { ChartCard } from './chart-card'; import { useLeadSource } from './use-analytics'; import type { DateRange } from '@/lib/services/analytics.service'; interface Props { range: DateRange; } const COLORS = [ 'hsl(var(--chart-1))', 'hsl(var(--chart-2))', 'hsl(var(--chart-3))', 'hsl(var(--chart-4))', 'hsl(var(--chart-5))', ]; const SOURCE_LABELS: Record = { website: 'Website', referral: 'Referral', manual: 'Manual', social: 'Social', unspecified: 'Unspecified', }; export function LeadSourceChart({ range }: Props) { const { data, isLoading } = useLeadSource(range); const slices = data?.slices ?? []; function toCsv(): string | null { if (!slices.length) return null; const header = 'source,count'; const rows = slices.map((s) => `${s.source},${s.count}`); return [header, ...rows].join('\n'); } const chartData = slices.map((s) => ({ name: SOURCE_LABELS[s.source] ?? s.source, value: s.count, })); return ( {isLoading ? ( ) : !slices.length ? ( ) : ( // Percentage radii + center-anchored chart so the pie scales with // the container instead of being clipped to a constant 90px ring at // narrow widths. Legend is reserved a fixed footer height. {chartData.map((_, i) => ( ))} )} ); }