{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "revenue-bar-chart",
  "title": "Revenue bar chart",
  "description": "Monthly stacked revenue bars split between platform net and processing fees, with a toggle to include landlord payments.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/items/revenue-bar-chart/revenue-bar-chart.tsx",
      "content": "\"use client\";\n\nimport { motion, useReducedMotionConfig } from \"motion/react\";\nimport { useState } from \"react\";\n\n// Monthly platform revenue, split into what Ledge keeps vs what Paystack takes.\n// Each bar's height = platform fee for the month, stacked as Ledge net (gold, kept)\n// + Paystack fee (clay, cost). \"To landlords\" (emerald) is off by default: it's\n// ~20x the fee, so enabling it rescales the bars to full GMV and the revenue split\n// becomes a thin band at the base — honest, but it flattens the revenue signal.\nexport type MonthPoint = {\n  month: string; // ISO date (first of month)\n  grossKobo: number;\n  platformFeeKobo: number;\n  paystackKobo: number;\n};\n\nconst LEDGE = \"var(--collection-platform)\"; // matches \"Ledge's money\" in the donut\nconst PAYSTACK = \"var(--collection-processing)\"; // matches \"Paystack fees\"\nconst LANDLORD = \"var(--collection-landlord)\"; // matches \"Paid to landlord\"\nconst CHART_H = 176; // px plot height\n\nfunction monthLabel(iso: string) {\n  return new Date(iso + \"T00:00:00Z\").toLocaleDateString(\"en-NG\", {\n    month: \"short\",\n    timeZone: \"UTC\",\n  });\n}\n\nexport type RevenueBarChartProps = { data: MonthPoint[]; title?: string; defaultShowLandlords?: boolean; animated?: boolean };\nexport function RevenueBarChart({ data, title = \"Monthly revenue\", defaultShowLandlords = false, animated = true }: RevenueBarChartProps) {\n  const reducedMotion = useReducedMotionConfig();\n  const [active, setActive] = useState<number | null>(null);\n  const [showGMV, setShowGMV] = useState(defaultShowLandlords);\n\n  const totalOf = (d: MonthPoint) => (showGMV ? d.grossKobo : d.platformFeeKobo);\n  const max = Math.max(1, ...data.map(totalOf));\n  const hasAny = data.some((d) => d.platformFeeKobo > 0);\n  // The breakdown is always on screen, so hovering a bar never changes the\n  // card's height: it shows the latest month until a bar is picked.\n  const shown = data[active ?? data.length - 1];\n\n  return (\n    <section className=\"w-full min-w-0 space-y-3 font-body text-ink\">\n      <div className=\"flex flex-wrap items-baseline justify-between gap-2\">\n        <h2 className=\"font-heading text-xl\">{title}</h2>\n        <div className=\"flex flex-wrap items-center gap-4 text-xs text-dim\">\n          <span className=\"flex items-center gap-1.5\">\n            <span className=\"inline-block h-2.5 w-2.5 rounded-inset\" style={{ background: LEDGE }} />\n            Ledge net\n          </span>\n          <span className=\"flex items-center gap-1.5\">\n            <span className=\"inline-block h-2.5 w-2.5 rounded-inset\" style={{ background: PAYSTACK }} />\n            Paystack fee\n          </span>\n          \n          <button\n            type=\"button\"\n            onClick={() => setShowGMV((v) => !v)}\n            className=\"flex items-center gap-1.5 transition-opacity\"\n            style={{ opacity: showGMV ? 1 : 0.45 }}\n            aria-pressed={showGMV}\n          >\n            <span className=\"inline-block h-2.5 w-2.5 rounded-inset\" style={{ background: LANDLORD }} />\n            <span className={showGMV ? \"\" : \"line-through\"}>To landlords</span>\n            <span className=\"text-[0.6rem] uppercase tracking-wide\">\n              {showGMV ? \"remove\" : \"add\"}\n            </span>\n          </button>\n        </div>\n      </div>\n\n      <div className=\"rounded-panel border border-rule bg-card p-6\">\n        {!hasAny ? (\n          <p className=\"py-10 text-center text-sm text-dim\">\n            No revenue in this window yet.\n          </p>\n        ) : (\n          <div className=\"relative\">\n            <div className=\"flex items-end justify-between gap-1.5\" style={{ height: CHART_H }}>\n              {data.map((d, i) => {\n                const ledgeKobo = Math.max(0, d.platformFeeKobo - d.paystackKobo);\n                const landlordKobo = Math.max(0, d.grossKobo - d.platformFeeKobo);\n                const total = totalOf(d);\n                const barH = (total / max) * CHART_H;\n                // Segments top → bottom; each height is its share of the bar.\n                const px = (v: number) => (total > 0 ? (v / total) * barH : 0);\n                const segments = [\n                  ...(showGMV ? [{ c: LANDLORD, h: px(landlordKobo) }] : []),\n                  { c: PAYSTACK, h: px(d.paystackKobo) },\n                  { c: LEDGE, h: px(ledgeKobo) },\n                ].filter((seg) => seg.h > 0);\n                const on = active === i;\n                return (\n                  <div\n                    key={d.month}\n                    tabIndex={0} role=\"button\" aria-label={`${monthLabel(d.month)}: platform fee ${formatKobo(d.platformFeeKobo)}, rent collected ${formatKobo(d.grossKobo)}`} onFocus={() => setActive(i)} onBlur={() => setActive(null)} onClick={() => setActive(i)} onKeyDown={(event) => { if (event.key === \"Enter\" || event.key === \" \") { event.preventDefault(); setActive(i); } }} className=\"group flex h-full min-w-0 flex-1 flex-col justify-end rounded-inset focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-lead\"\n                    onMouseEnter={() => setActive(i)}\n                    onMouseLeave={() => setActive(null)}\n                  >\n                    <motion.div\n                      initial={{ scaleY: animated && !reducedMotion ? 0 : 1 }} whileInView={{ scaleY: 1 }} viewport={{ once: true }} transition={{ duration: reducedMotion || !animated ? 0 : 0.7, delay: reducedMotion || !animated ? 0 : i * 0.045 }}\n                      className=\"flex w-full flex-col justify-end transition-opacity motion-reduce:transition-none\"\n                      style={{\n                        height: Number(barH.toFixed(6)),\n                        opacity: active === null || on ? 1 : 0.4,\n                        transformOrigin: \"bottom\",\n                      }}\n                    >\n                      {segments.map((seg, si) => (\n                        <div\n                          key={si}\n                          style={{\n                            height: Number((si === segments.length - 1 ? Math.max(2, seg.h) : seg.h).toFixed(6)),\n                            background: seg.c,\n                            borderTopLeftRadius: si === 0 ? \"var(--inset-radius)\" : 0,\n                            borderTopRightRadius: si === 0 ? \"var(--inset-radius)\" : 0,\n                            marginBottom: si < segments.length - 1 ? 2 : 0,\n                          }}\n                        />\n                      ))}\n                    </motion.div>\n                    <div className=\"mt-2 text-center text-[0.65rem] text-dim\">\n                      {monthLabel(d.month)}\n                    </div>\n                  </div>\n                );\n              })}\n            </div>\n\n            {shown && (\n              <div className=\"mt-4 rounded-panel border border-rule bg-paper p-3 text-sm\">\n                <div className=\"mb-1 font-medium\">\n                  {new Date(shown.month + \"T00:00:00Z\").toLocaleDateString(\"en-NG\", {\n                    month: \"long\",\n                    year: \"numeric\",\n                    timeZone: \"UTC\",\n                  })}\n                </div>\n                <dl className=\"grid grid-cols-2 gap-x-6 gap-y-1 text-xs\">\n                  <dt className=\"text-dim\">Platform fee</dt>\n                  <dd className=\"text-right font-heading tabular-nums\">\n                    {formatKobo(shown.platformFeeKobo)}\n                  </dd>\n                  <dt className=\"flex items-center gap-1.5\">\n                    <span className=\"inline-block h-2 w-2 rounded-inset\" style={{ background: LEDGE }} />\n                    Ledge net\n                  </dt>\n                  <dd className=\"text-right font-heading tabular-nums\">\n                    {formatKobo(Math.max(0, shown.platformFeeKobo - shown.paystackKobo))}\n                  </dd>\n                  <dt className=\"flex items-center gap-1.5\">\n                    <span className=\"inline-block h-2 w-2 rounded-inset\" style={{ background: PAYSTACK }} />\n                    Paystack fee\n                  </dt>\n                  <dd className=\"text-right font-heading tabular-nums\">\n                    {formatKobo(shown.paystackKobo)}\n                  </dd>\n                  <dt className=\"flex items-center gap-1.5\">\n                    <span className=\"inline-block h-2 w-2 rounded-inset\" style={{ background: LANDLORD }} />\n                    To landlords\n                  </dt>\n                  <dd className=\"text-right font-heading tabular-nums\">\n                    {formatKobo(Math.max(0, shown.grossKobo - shown.platformFeeKobo))}\n                  </dd>\n                  <dt className=\"text-dim\">Rent collected (GMV)</dt>\n                  <dd className=\"text-right font-heading tabular-nums text-dim\">\n                    {formatKobo(shown.grossKobo)}\n                  </dd>\n                </dl>\n              </div>\n            )}\n          </div>\n        )}\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 inspect platform revenue over time. The landlord toggle rescales to gross collections; each amount is integer kobo.",
    "origin": {
      "project": "Ledge",
      "path": "src/components/admin/revenue-trend.tsx"
    },
    "credits": []
  },
  "type": "registry:block"
}
