"use client" import { useState, useMemo } from "react" import { motion } from "framer-motion" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" interface IntervalData { label: string leads: number closed: number } interface LeadsPerMonthChartProps { data: IntervalData[] } const BAR_GRADIENT_TOP = "#3B6AB8" const NEW_LEADS = "#1e3c72" const CLOSED = "#457fca" export function LeadsPerMonthChart({ data }: LeadsPerMonthChartProps) { const [hovered, setHovered] = useState(null) const width = 880 const height = 620 const padding = { top: 128, right: 24, bottom: 48, left: 44 } const chartW = width - padding.left - padding.right const chartH = height - padding.top - padding.bottom const maxVal = useMemo(() => { const max = Math.max(...data.map((d) => Math.max(d.leads, d.closed))) return Math.ceil(max / 7) * 7 }, [data]) const ticks = useMemo(() => { const steps = 4 return Array.from({ length: steps + 1 }, (_, i) => Math.round((maxVal / steps) * i)) }, [maxVal]) const groupW = chartW / data.length const barW = groupW * 0.28 const gap = groupW * 0.06 const yScale = (v: number) => chartH - (v / maxVal) * chartH const active = hovered const activeDatum = active !== null ? data[active] : null const totalNew = data.reduce((s, d) => s + d.leads, 0) const totalClosed = data.reduce((s, d) => s + d.closed, 0) const closeRate = totalNew > 0 ? Math.round((totalClosed / totalNew) * 100) : 0 return (
Leads Per Month

{totalNew} new · {totalClosed} closed · {closeRate}% close rate

New Leads
Closed
{/* Grid lines */} {ticks.map((t, i) => ( {t} ))} {/* Bars */} {data.map((d, i) => { const groupX = i * groupW const isActive = active === i const newH = chartH - yScale(d.leads) const closedH = chartH - yScale(d.closed) return ( setHovered(i)} onMouseLeave={() => setHovered(null)} style={{ cursor: "pointer" }} > {/* Hover highlight band */} {/* New Leads bar */} {/* Closed bar */} {/* Month label */} {d.label} ) })} {/* Tooltip */} {activeDatum && (
data.length - 2 ? "translate(-100%, 0)" : "translate(-12%, 0)", transition: "left 0.15s ease", }} >
{activeDatum.label}
New Leads {activeDatum.leads}
Closed {activeDatum.closed}
{activeDatum.leads > 0 ? `${Math.round((activeDatum.closed / activeDatum.leads) * 100)}% close rate` : "No leads"}
)}
) }