import { useMemo } from 'react'; import { cx } from '@/utils/cx'; import type { Lead } from '@/types/entities'; interface LeadFunnelProps { leads: Lead[]; } type FunnelStage = { label: string; count: number; color: string; }; export const LeadFunnel = ({ leads }: LeadFunnelProps) => { const stages = useMemo((): FunnelStage[] => { const total = leads.length; const contacted = leads.filter((lead) => lead.leadStatus === 'CONTACTED' || lead.leadStatus === 'QUALIFIED' || lead.leadStatus === 'NURTURING' || lead.leadStatus === 'APPOINTMENT_SET' || lead.leadStatus === 'CONVERTED', ).length; const appointmentSet = leads.filter((lead) => lead.leadStatus === 'APPOINTMENT_SET' || lead.leadStatus === 'CONVERTED', ).length; const converted = leads.filter((lead) => lead.leadStatus === 'CONVERTED', ).length; return [ { label: 'Generated', count: total, color: 'bg-brand-600' }, { label: 'Contacted', count: contacted, color: 'bg-brand-500' }, { label: 'Appointment Set', count: appointmentSet, color: 'bg-brand-400' }, { label: 'Converted', count: converted, color: 'bg-success-500' }, ]; }, [leads]); const maxCount = stages[0]?.count || 1; return (

Lead Funnel · This Week

{stages.map((stage, index) => { const widthPercent = maxCount > 0 ? (stage.count / maxCount) * 100 : 0; const previousCount = index > 0 ? stages[index - 1].count : null; const conversionRate = previousCount !== null && previousCount > 0 ? ((stage.count / previousCount) * 100).toFixed(0) : null; return (
{stage.label}
{stage.count} {conversionRate !== null && ( ({conversionRate}%) )}
); })}
); };