{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "retention-04",
  "description": "Users gained above the axis and lost below it, with the net change and the quick ratio.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/retention.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/retention/retention-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  ChartTooltipContent,\n  chartActiveDot,\n  chartAxisProps,\n  chartBarCursor,\n  chartGridProps,\n  chartValueAxisWidth,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelLegend,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  type GrowthAccountingInput,\n  getQuickRatio,\n  growthColors,\n} from '@/registry/components/dashboardblocks/retention'\nimport {\n  formatCompact,\n  formatNumber,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\nimport {\n  Bar,\n  CartesianGrid,\n  ComposedChart,\n  Line,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface GrowthAccountingPeriod extends GrowthAccountingInput {\n  label: string\n}\n\ninterface Retention4Props extends BlockStateProps {\n  description: string\n  /** Oldest first. Leave out, or pass none, to show the empty state. */\n  periods?: GrowthAccountingPeriod[]\n  periodName?: string\n  title: string\n  /** @default 'users' */\n  unit?: string\n}\n\nconst WEEK_ENDINGS = [\n  'Jul 5',\n  'Jul 12',\n  'Jul 19',\n  'Jul 26',\n  'Aug 2',\n  'Aug 9',\n  'Aug 16',\n  'Aug 23',\n  'Aug 30',\n  'Sep 6',\n  'Sep 13',\n  'Sep 20',\n]\n\nconst exampleProps: Retention4Props = {\n  description: 'Weekly active users gained and lost, last 12 weeks.',\n  periods: WEEK_ENDINGS.map((label, week) => ({\n    churned: Math.round(1_020 + week * 18 + Math.sin(week * 1.3) * 90),\n    label,\n    new: Math.round(1_180 + week * 34 + Math.sin(week / 1.7) * 160),\n    resurrected: Math.round(310 + week * 6 + Math.cos(week * 0.9) * 45),\n  })),\n  title: 'Growth accounting',\n  unit: 'users',\n}\n\nconst formatChange = (value: number) =>\n  formatSigned(Math.round(value), (count) => formatNumber(count))\n\nconst formatRatio = (ratio: number) =>\n  Number.isFinite(ratio)\n    ? formatNumber(ratio, { maximumFractionDigits: 2, minimumFractionDigits: 2 })\n    : 'No churn'\n\nconst Retention4 = (props: Retention4Props) => {\n  const {\n    description,\n    periodName = 'week',\n    periods = [],\n    errorMessage,\n    status,\n    title,\n    unit = 'users',\n  } = props\n  const state = getBlockState(status, periods)\n  const rows = periods.map((period) => ({\n    churned: -period.churned,\n    label: period.label,\n    net: period.new + period.resurrected - period.churned,\n    new: period.new,\n    resurrected: period.resurrected,\n  }))\n  const latest = periods[periods.length - 1]\n  const latestRow = rows[rows.length - 1]\n  const ratio = latest ? getQuickRatio(latest) : 0\n  const netTotal = rows.reduce((sum, row) => sum + row.net, 0)\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            <dl className='grid grid-cols-2 gap-x-6 gap-y-4 border-t pt-4 @lg:grid-cols-3 @lg:items-start'>\n              <div className='col-span-2 flex flex-col gap-1 @lg:col-span-1'>\n                <dt className='text-muted-foreground text-sm'>\n                  {latest && !state ? `Quick ratio, ${latest.label}` : 'Quick ratio'}\n                </dt>\n                <dd>\n                  <KPIValue value={latest ? ratio : undefined} format={formatRatio} />\n                </dd>\n                {(!state || state === 'loading') && (\n                  <dd className='text-muted-foreground text-xs'>\n                    <BlockReady>\n                      {Number.isFinite(ratio)\n                        ? `${formatRatio(ratio)} ${unit} gained for each one lost`\n                        : `No ${unit} lost`}\n                    </BlockReady>\n                  </dd>\n                )}\n              </div>\n              {state === 'loading' && (\n                <>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Net change</dt>\n                    <dd>\n                      <KPIValue className='text-2xl' value={undefined} />\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>\n                      Net change, {periodName}s\n                    </dt>\n                    <dd>\n                      <KPIValue className='text-2xl' value={undefined} />\n                    </dd>\n                  </div>\n                </>\n              )}\n              {!state && (\n                <>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>\n                      Net change, {latest?.label}\n                    </dt>\n                    <dd className='text-2xl font-light tracking-tight tabular-nums'>\n                      {formatChange(latestRow?.net ?? 0)}\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>\n                      Net change, {periods.length} {periodName}s\n                    </dt>\n                    <dd className='text-2xl font-light tracking-tight tabular-nums'>\n                      {formatChange(netTotal)}\n                    </dd>\n                  </div>\n                </>\n              )}\n            </dl>\n            {(!state || state === 'loading') && (\n              <ChartPanelLegend\n                items={[\n                  { color: growthColors.new, label: 'New' },\n                  { color: growthColors.resurrected, label: 'Resurrected' },\n                  { color: growthColors.churned, label: 'Churned' },\n                  {\n                    color: 'var(--color-foreground)',\n                    label: 'Net change',\n                    shape: 'line',\n                  },\n                ]}\n              />\n            )}\n            {state ? (\n              <ChartPlaceholder height={240} />\n            ) : (\n              <>\n                <ChartPanelFigure className='h-60'>\n                  <ResponsiveContainer width='100%' height='100%'>\n                    <ComposedChart\n                      aria-label={title}\n                      data={rows}\n                      margin={{ top: 8, right: 12, bottom: 0, left: 0 }}\n                      stackOffset='sign'\n                    >\n                      <CartesianGrid {...chartGridProps} />\n                      <XAxis\n                        {...chartAxisProps}\n                        dataKey='label'\n                        interval='preserveStartEnd'\n                        minTickGap={16}\n                      />\n                      <YAxis\n                        {...chartAxisProps}\n                        tickFormatter={(value: number) =>\n                          value < 0\n                            ? formatSigned(value, formatCompact)\n                            : formatCompact(value)\n                        }\n                        width={chartValueAxisWidth}\n                      />\n                      <ReferenceLine y={0} stroke='var(--color-border)' />\n                      <Tooltip\n                        content={(tooltipProps) => (\n                          <ChartTooltipContent\n                            {...tooltipProps}\n                            valueFormatter={formatChange}\n                          />\n                        )}\n                        cursor={chartBarCursor}\n                      />\n                      <Bar\n                        dataKey='new'\n                        fill={growthColors.new}\n                        name='New'\n                        stackId='users'\n                      />\n                      <Bar\n                        dataKey='resurrected'\n                        fill={growthColors.resurrected}\n                        name='Resurrected'\n                        radius={[4, 4, 0, 0]}\n                        stackId='users'\n                      />\n                      <Bar\n                        dataKey='churned'\n                        fill={growthColors.churned}\n                        name='Churned'\n                        radius={[0, 0, 4, 4]}\n                        stackId='users'\n                      />\n                      <Line\n                        activeDot={chartActiveDot}\n                        dataKey='net'\n                        dot={{ fill: 'var(--color-foreground)', r: 2.5, strokeWidth: 0 }}\n                        name='Net change'\n                        stroke='var(--color-foreground)'\n                        strokeWidth={2}\n                        type='linear'\n                      />\n                    </ComposedChart>\n                  </ResponsiveContainer>\n                </ChartPanelFigure>\n                <ChartPanelTable\n                  caption={`${title}: ${description} Net change is new plus resurrected minus churned.`}\n                  columns={[\n                    {\n                      key: 'label',\n                      label: `${periodName.charAt(0).toUpperCase()}${periodName.slice(1)} ending`,\n                    },\n                    { format: formatChange, key: 'new', label: 'New' },\n                    { format: formatChange, key: 'resurrected', label: 'Resurrected' },\n                    { format: formatChange, key: 'churned', label: 'Churned' },\n                    { format: formatChange, key: 'net', label: 'Net change' },\n                    { key: 'ratio', label: 'Quick ratio' },\n                  ]}\n                  rows={rows.map((row, index) => ({\n                    ...row,\n                    ratio: formatRatio(getQuickRatio(periods[index])),\n                  }))}\n                />\n              </>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Retention4,\n  exampleProps as retention4ExampleProps,\n  type GrowthAccountingPeriod,\n  type Retention4Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/retention/retention-04.tsx"
    }
  ],
  "meta": {
    "shows": "Users gained above the axis and lost below it, with the net change and the quick ratio.",
    "size": "full",
    "usedWith": [
      "retention-01",
      "stat-group-02"
    ],
    "component": "Retention4",
    "props": "{ description: string; periods?: { label: string; churned: number; new: number; resurrected: number }[]; periodName?: string; title: string; unit?: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
