{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "donut-chart",
  "title": "Donut chart",
  "description": "Ledge’s collection doughnut paired with exact amounts and percentages on the right, plus linked slice and legend highlights.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/items/donut-chart/donut-chart.tsx",
      "content": "\"use client\";\n\nimport { motion, useReducedMotionConfig } from \"motion/react\";\nimport { useState } from \"react\";\n\n// Where every naira collected ends up. Three slices that sum to gross:\n//   landlord net + Ledge's cut + Paystack's fee = gross collected.\n// Paystack's fee is carved out of Ledge's platform fee (platform_fee − paystack_fee),\n// so it reads as a slice of \"our\" money, not an extra charge on the tenant.\nexport type DonutChartProps = {\n  title?: string;\n  platformLabel?: string;\n  processingLabel?: string;\n  animated?: boolean;\n  grossKobo: number;\n  landlordNetKobo: number;\n  ledgeKobo: number; // platform fee kept by Ledge, after Paystack's cut\n  paystackKobo: number; // Paystack's processing fee\n};\n\n// Ledge’s emerald, gold and clay series colours, supplied by the active theme.\nconst SERIES = [\n  { key: \"landlord\", label: \"Paid to landlord\", color: \"var(--collection-landlord)\" },\n  { key: \"ledge\", label: \"Ledge's money\", color: \"var(--collection-platform)\" },\n  { key: \"paystack\", label: \"Paystack fees\", color: \"var(--collection-processing)\" },\n] as const;\n\nconst CX = 110;\nconst CY = 110;\nconst R = 78;\nconst W = 30;\nconst GAP_DEG = 1.5; // surface gap between segments\n\nfunction polar(angleDeg: number, r = R) {\n  const rad = ((angleDeg - 90) * Math.PI) / 180;\n  return { x: CX + r * Math.cos(rad), y: CY + r * Math.sin(rad) };\n}\n\nfunction arc(startDeg: number, endDeg: number) {\n  const span = endDeg - startDeg;\n  // Only inset for the surface gap when the slice is wide enough to keep a sliver.\n  const inset = span > GAP_DEG * 2 ? GAP_DEG / 2 : 0;\n  const a0 = startDeg + inset;\n  const a1 = endDeg - inset;\n  const p0 = polar(a0);\n  const p1 = polar(a1);\n  const large = a1 - a0 > 180 ? 1 : 0;\n  return `M ${p0.x} ${p0.y} A ${R} ${R} 0 ${large} 1 ${p1.x} ${p1.y}`;\n}\n\nexport function DonutChart({\n  title = \"Where the money goes\",\n  platformLabel = \"Ledge’s money\",\n  processingLabel = \"Paystack fees\",\n  animated = true,\n  grossKobo,\n  landlordNetKobo,\n  ledgeKobo,\n  paystackKobo,\n}: DonutChartProps) {\n  const reducedMotion = useReducedMotionConfig();\n  const [active, setActive] = useState<number | null>(null);\n\n  const values = [landlordNetKobo, ledgeKobo, paystackKobo];\n  const total = grossKobo || 1; // guard divide-by-zero on an empty landlord\n\n  // Build cumulative arcs.\n  let cursor = 0;\n  const segments = values.map((v, i) => {\n    const frac = v / total;\n    const start = cursor * 360;\n    cursor += frac;\n    const end = cursor * 360;\n    return { ...SERIES[i], label: i === 1 ? platformLabel : i === 2 ? processingLabel : SERIES[i].label, value: v, frac, start, end };\n  });\n\n  if (grossKobo === 0) {\n    return (\n      <section className=\"@container w-full min-w-0 space-y-3 font-body text-ink\">\n        <h2 className=\"font-heading text-xl\">{title}</h2>\n        <p className=\"rounded-panel border border-rule bg-card px-4 py-10 text-center text-sm text-dim\">\n          No payments collected yet.\n        </p>\n      </section>\n    );\n  }\n\n  return (\n    <section className=\"@container w-full min-w-0 space-y-3 font-body text-ink\">\n      <h2 className=\"font-heading text-xl\">{title}</h2>\n      <div className=\"rounded-panel border border-rule bg-card flex flex-col items-center gap-8 p-6 @xl:flex-row @xl:gap-10\">\n        \n        <div\n          className=\"relative shrink-0\"\n          style={{ width: 200, height: 200 }}\n        >\n          <svg\n            viewBox=\"0 0 220 220\"\n            width={200}\n            height={200}\n            role=\"group\"\n            aria-label={`Of ${formatKobo(grossKobo)} collected, ${formatKobo(\n              landlordNetKobo,\n            )} paid to landlord, ${formatKobo(ledgeKobo)} to ${platformLabel}, ${formatKobo(\n              paystackKobo,\n            )} ${processingLabel}.`}\n          >\n            {segments.map((s, i) => (\n              <motion.path\n                key={s.key}\n                d={s.value > 0 ? arc(s.start, s.end) : \"\"} tabIndex={s.value > 0 ? 0 : -1} role=\"button\" aria-label={`${s.label}: ${formatKobo(s.value)}, ${(s.frac * 100).toFixed(1)}%`} onKeyDown={(event) => { if (event.key === \"Enter\" || event.key === \" \") { event.preventDefault(); setActive(i); } }}\n                fill=\"none\"\n                stroke={s.color}\n                strokeWidth={active === i ? W + 6 : W}\n                strokeLinecap=\"butt\"\n                pathLength={1}\n                opacity={active === null || active === i ? 1 : 0.35}\n                className=\"cursor-pointer transition-[stroke-width,opacity] duration-150 motion-reduce:transition-none\"\n                initial={{ pathLength: animated && !reducedMotion ? 0 : 1 }} animate={{ pathLength: 1 }} transition={{ duration: reducedMotion || !animated ? 0 : 0.7, delay: reducedMotion || !animated ? 0 : i * 0.12 }}\n                onPointerEnter={() => setActive(i)} onFocus={() => setActive(i)} onBlur={() => setActive(null)}\n                onPointerLeave={() => setActive(null)}\n              />\n            ))}\n          </svg>\n          \n          <div className=\"pointer-events-none absolute inset-0 flex flex-col items-center justify-center text-center\">\n            <div\n              className=\"text-[0.7rem] font-medium leading-none\"\n              style={{ color: active === null ? \"var(--dim)\" : segments[active].color }}\n            >\n              {active === null ? \"Collected\" : segments[active].label}\n            </div>\n            <div className=\"mt-1.5 font-heading text-xl leading-none text-ink\">\n              {formatKobo(active === null ? grossKobo : segments[active].value)}\n            </div>\n            {/* The hovered slice reads out here, in the hole, rather than in a\n                floating card that would sit on the ring and hide the slice. */}\n            {active !== null && (\n              <div className=\"mt-1 text-[0.7rem] tabular-nums text-dim\">\n                {(segments[active].frac * 100).toFixed(1)}%\n              </div>\n            )}\n          </div>\n          \n        </div>\n\n        <ul className=\"w-full space-y-2 text-sm\">\n          {segments.map((s, i) => (\n            <li\n              key={s.key}\n              tabIndex={0} className=\"flex min-h-11 flex-wrap items-center justify-between gap-4 rounded-inset px-2 py-1.5 transition-colors focus-visible:outline-2 focus-visible:outline-lead\"\n              style={{ background: active === i ? \"var(--paper)\" : \"transparent\" }}\n              onPointerEnter={() => setActive(i)} onFocus={() => setActive(i)} onBlur={() => setActive(null)}\n              onPointerLeave={() => setActive(null)}\n            >\n              <span className=\"flex items-center gap-2.5\">\n                <span\n                  className=\"inline-block h-2.5 w-2.5 rounded-inset\"\n                  style={{ background: s.color }}\n                  aria-hidden\n                />\n                <span className=\"text-ink\">{s.label}</span>\n              </span>\n              <span className=\"flex items-baseline gap-2\">\n                <span className=\"font-heading tabular-nums text-ink\">\n                  {formatKobo(s.value)}\n                </span>\n                <span className=\"w-10 text-right text-xs tabular-nums text-dim\">\n                  {(s.frac * 100).toFixed(1)}%\n                </span>\n              </span>\n            </li>\n          ))}\n        </ul>\n      </div>\n    </section>\n  );\n}\n\nexport function formatKobo(kobo: number): string {\n  const naira = kobo / 100;\n  return (\n    \"₦\" +\n    naira.toLocaleString(\"en-NG\", {\n      minimumFractionDigits: 0,\n      maximumFractionDigits: 2,\n    })\n  );\n}\n",
      "type": "registry:block"
    }
  ],
  "meta": {
    "kind": "pattern",
    "whenToUse": "Use to show how collected money is divided. Pass integer kobo and ensure landlord net + platform retained + processing fees equals gross.",
    "origin": {
      "project": "Ledge",
      "path": "src/components/admin/collection-breakdown.tsx"
    },
    "credits": []
  },
  "type": "registry:block"
}
