"use client" import { useState } from "react" import { motion } from "framer-motion" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" interface StatusData { name: string value: number color: string } interface LeadStatusChartProps { data: StatusData[] } function polar(cx: number, cy: number, r: number, deg: number) { const rad = ((deg - 90) * Math.PI) / 180 return { x: cx + r * Math.cos(rad), y: cy + r * Math.sin(rad) } } function arcPath(cx: number, cy: number, oR: number, iR: number, start: number, end: number) { const gap = 3 const s = start + gap / 2 const e = end - gap / 2 const o1 = polar(cx, cy, oR, s) const o2 = polar(cx, cy, oR, e) const i1 = polar(cx, cy, iR, e) const i2 = polar(cx, cy, iR, s) const lg = e - s > 180 ? 1 : 0 return `M${o1.x} ${o1.y} A${oR} ${oR} 0 ${lg} 1 ${o2.x} ${o2.y} L${i1.x} ${i1.y} A${iR} ${iR} 0 ${lg} 0 ${i2.x} ${i2.y}Z` } export function LeadStatusChart({ data }: LeadStatusChartProps) { const [hov, setHov] = useState(null) const total = data.reduce((s, d) => s + d.value, 0) if (total === 0) { return ( Lead Status
No data for this period
) } let cum = 0 const segs = data.map((d) => { const start = cum const deg = (d.value / total) * 360 cum += deg return { ...d, start, end: cum, deg, pct: Math.round((d.value / total) * 100) } }) const active = hov !== null ? segs[hov] : null return ( {/* Spider watermark */}
Lead Status
{/* Donut */} {segs.map((s, i) => ( ))} {/* Background ring */} {/* Segments */} {segs.map((s, i) => { const isH = hov === i const oR = isH ? 137 : 130 const iR = isH ? 73 : 80 return ( setHov(i)} onMouseLeave={() => setHov(null)} /> ) })} {/* Center circle */} {/* Center text */} {active ? ( <> {active.value} {active.name} {active.pct}% ) : ( <> {total} Total Leads )} {/* Legend */}
{segs.map((s, i) => (
setHov(i)} onMouseLeave={() => setHov(null)} className="flex cursor-pointer items-center gap-2.5 rounded-xl p-2.5 transition-all duration-200" style={{ background: hov === i ? `${s.color}18` : "hsl(var(--muted) / 0.3)", border: `1px solid ${hov === i ? s.color + "50" : "hsl(var(--border))"}`, gridColumn: i === segs.length - 1 && segs.length % 2 !== 0 ? "1 / -1" : undefined, maxWidth: i === segs.length - 1 && segs.length % 2 !== 0 ? "50%" : undefined, margin: i === segs.length - 1 && segs.length % 2 !== 0 ? "0 auto" : undefined, width: i === segs.length - 1 && segs.length % 2 !== 0 ? "100%" : undefined, }} >
{s.name}
{s.value} · {s.pct}%
{s.pct}%
))}
) }