{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "donut-chart",
  "type": "registry:ui",
  "title": "Donut Chart",
  "dependencies": [
    "motion"
  ],
  "files": [
    {
      "path": "components/ui/donut-chart.tsx",
      "content": "\"use client\";\n\nimport React, { useMemo, useState } from \"react\";\nimport { motion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface DonutSegment {\n  label: string;\n  value: number;\n  color: string;\n}\n\nexport interface DonutChartProps {\n  segments?: DonutSegment[];\n  title?: string;\n  className?: string;\n}\n\nconst DEFAULT_SEGMENTS: DonutSegment[] = [\n  { label: \"Direct\", value: 4280, color: \"#3b82f6\" },\n  { label: \"Organic\", value: 3140, color: \"#34d399\" },\n  { label: \"Referral\", value: 1890, color: \"#a78bfa\" },\n  { label: \"Social\", value: 940, color: \"#fbbf24\" },\n];\n\nexport function DonutChart({\n  segments = DEFAULT_SEGMENTS,\n  title = \"Traffic Sources\",\n  className,\n}: DonutChartProps) {\n  const [hoveredIdx, setHoveredIdx] = useState<number | null>(null);\n  const [halfRing, setHalfRing] = useState(false);\n\n  const total = segments.reduce((acc, s) => acc + s.value, 0);\n  const active = hoveredIdx !== null ? segments[hoveredIdx] : null;\n\n  const size = 160;\n  const strokeWidth = 18;\n  const radius = (size - strokeWidth) / 2;\n  const circumference = 2 * Math.PI * radius;\n  const arcSpan = halfRing ? circumference / 2 : circumference;\n\n  const computedSegments = useMemo(() => {\n    return segments.map((seg, idx) => {\n      const ratio = total > 0 ? seg.value / total : 0;\n      const startRatio =\n        total > 0\n          ? segments.slice(0, idx).reduce((sum, s) => sum + s.value, 0) / total\n          : 0;\n      return {\n        ...seg,\n        segLength: Math.max(0, ratio * arcSpan - 3),\n        offset: -startRatio * arcSpan,\n      };\n    });\n  }, [segments, total, arcSpan]);\n\n  return (\n    <div\n      className={cn(\n        \"w-full max-w-sm rounded-2xl border border-white/10 bg-[#101013] p-5 shadow-[inset_0_1px_0_rgba(255,255,255,0.06)]\",\n        className\n      )}\n    >\n      <div className=\"flex items-center justify-between\">\n        <div>\n          <h3 className=\"text-xs font-semibold text-zinc-100\">{title}</h3>\n          <p className=\"text-[11px] text-zinc-500\">Hover segments to inspect</p>\n        </div>\n\n        <div className=\"inline-flex rounded-full border border-white/10 bg-zinc-950 p-0.5\">\n          {[\n            { id: false, label: \"Full\" },\n            { id: true, label: \"Gauge\" },\n          ].map((opt) => (\n            <button\n              key={opt.label}\n              type=\"button\"\n              onClick={() => setHalfRing(opt.id)}\n              className={cn(\n                \"rounded-full px-2.5 py-0.5 text-[10px] font-medium cursor-pointer transition-colors\",\n                halfRing === opt.id\n                  ? \"bg-white/12 text-white\"\n                  : \"text-zinc-500 hover:text-zinc-300\"\n              )}\n            >\n              {opt.label}\n            </button>\n          ))}\n        </div>\n      </div>\n\n      <div className=\"mt-4 flex flex-col items-center gap-4 sm:flex-row\">\n        {/* SVG Donut */}\n        <div\n          className={cn(\n            \"relative flex shrink-0 items-center justify-center\",\n            halfRing ? \"h-[96px] w-[160px]\" : \"h-[160px] w-[160px]\"\n          )}\n        >\n          <svg\n            width={size}\n            height={halfRing ? size / 2 + 12 : size}\n            viewBox={`0 0 ${size} ${halfRing ? size / 2 + 12 : size}`}\n            className=\"overflow-visible\"\n          >\n            <g\n              transform={\n                halfRing\n                  ? `rotate(180 ${size / 2} ${size / 2})`\n                  : `rotate(-90 ${size / 2} ${size / 2})`\n              }\n            >\n              {/* Track */}\n              <circle\n                cx={size / 2}\n                cy={size / 2}\n                r={radius}\n                fill=\"none\"\n                stroke=\"rgba(255,255,255,0.06)\"\n                strokeWidth={strokeWidth}\n                strokeDasharray={halfRing ? `${arcSpan} ${circumference}` : undefined}\n                strokeLinecap=\"round\"\n              />\n\n              {computedSegments.map((seg, idx) => {\n                const isHovered = hoveredIdx === idx;\n                const isDimmed = hoveredIdx !== null && hoveredIdx !== idx;\n\n                return (\n                  <motion.circle\n                    key={seg.label}\n                    cx={size / 2}\n                    cy={size / 2}\n                    r={radius}\n                    fill=\"none\"\n                    stroke={seg.color}\n                    strokeWidth={isHovered ? strokeWidth + 3 : strokeWidth}\n                    strokeDasharray={`${seg.segLength} ${circumference}`}\n                    strokeDashoffset={seg.offset}\n                    strokeLinecap=\"round\"\n                    initial={{ opacity: 0 }}\n                    animate={{ opacity: isDimmed ? 0.3 : 1 }}\n                    transition={{ duration: 0.25 }}\n                    onMouseEnter={() => setHoveredIdx(idx)}\n                    onMouseLeave={() => setHoveredIdx(null)}\n                    className=\"cursor-pointer transition-[stroke-width]\"\n                  />\n                );\n              })}\n            </g>\n          </svg>\n\n          {/* Center Readout */}\n          <div\n            className={cn(\n              \"pointer-events-none absolute flex flex-col items-center text-center\",\n              halfRing ? \"bottom-1\" : \"inset-0 justify-center\"\n            )}\n          >\n            <span className=\"font-mono text-lg font-semibold text-white tabular-nums\">\n              {(active ? active.value : total).toLocaleString()}\n            </span>\n            <span className=\"text-[10px] text-zinc-400\">\n              {active\n                ? `${active.label} (${Math.round((active.value / total) * 100)}%)`\n                : \"Total visits\"}\n            </span>\n          </div>\n        </div>\n\n        {/* Legend */}\n        <div className=\"flex w-full flex-1 flex-col gap-1.5\">\n          {segments.map((seg, idx) => {\n            const pct = Math.round((seg.value / total) * 100);\n            const isHovered = hoveredIdx === idx;\n            return (\n              <button\n                key={seg.label}\n                type=\"button\"\n                onMouseEnter={() => setHoveredIdx(idx)}\n                onMouseLeave={() => setHoveredIdx(null)}\n                className={cn(\n                  \"flex items-center justify-between rounded-lg px-2.5 py-1.5 text-xs cursor-pointer transition-colors\",\n                  isHovered ? \"bg-white/[0.07]\" : \"hover:bg-white/[0.04]\"\n                )}\n              >\n                <span className=\"flex items-center gap-2 text-zinc-300\">\n                  <span\n                    className=\"h-2 w-2 rounded-full\"\n                    style={{ backgroundColor: seg.color }}\n                  />\n                  {seg.label}\n                </span>\n                <span className=\"font-mono text-[11px] text-zinc-400 tabular-nums\">\n                  {pct}%\n                </span>\n              </button>\n            );\n          })}\n        </div>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:ui"
    }
  ]
}