{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "gauge-02",
  "description": "A health score weighted from its contributing factors, marked on poor, fair and good bands, with the factors that pull it down most.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/gauge.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/gauge/gauge-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  Gauge,\n  type GaugeBand,\n  GaugeLegend,\n  GaugeToneLabel,\n  GaugeValue,\n  gaugeToneConfig,\n  getGaugeBand,\n  getWeightedScore,\n} from '@/registry/components/dashboardblocks/gauge'\nimport { formatPercent } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface HealthFactor {\n  detail: string\n  label: string\n  /** 0–100. */\n  score: number\n  weight: number\n}\n\ninterface Gauge2Props extends BlockStateProps {\n  /** Bands from 0 to 100, in order. */\n  bands: GaugeBand[]\n  description: string\n  /** Leave out, or pass none, to show the empty state. */\n  factors?: HealthFactor[]\n  title: string\n}\n\nconst exampleProps: Gauge2Props = {\n  bands: [\n    { from: 0, label: 'Poor', to: 40, tone: 'critical' },\n    { from: 40, label: 'Fair', to: 70, tone: 'caution' },\n    { from: 70, label: 'Good', to: 100, tone: 'positive' },\n  ],\n  description: 'Northwind Traders, on the Enterprise plan until it renews Dec 14.',\n  factors: [\n    {\n      detail: '312 of 350 seats active this month',\n      label: 'Product usage',\n      score: 84,\n      weight: 0.35,\n    },\n    {\n      detail: 'NPS +32 from 18 responses',\n      label: 'Sentiment',\n      score: 71,\n      weight: 0.2,\n    },\n    {\n      detail: 'Last business review 94 days ago',\n      label: 'Engagement',\n      score: 66,\n      weight: 0.1,\n    },\n    {\n      detail: '14 open tickets, 3 escalated',\n      label: 'Support',\n      score: 58,\n      weight: 0.2,\n    },\n    {\n      detail: 'Invoice 21 days overdue',\n      label: 'Billing',\n      score: 34,\n      weight: 0.15,\n    },\n  ],\n  title: 'Account health',\n}\n\nconst joinLabels = (labels: string[]) =>\n  labels.length < 2\n    ? (labels[0] ?? '')\n    : `${labels.slice(0, -1).join(', ')} and ${labels[labels.length - 1]}`\n\nconst Gauge2 = (props: Gauge2Props) => {\n  const { bands, description, factors = [], errorMessage, status, title } = props\n  const state = getBlockState(status, factors)\n  const exact = getWeightedScore(factors)\n  const score = Math.round(exact)\n  const band = getGaugeBand(score, bands)\n  const totalWeight = factors.reduce((sum, factor) => sum + factor.weight, 0) || 1\n  const drags = factors\n    .map((factor) => ({ ...factor, pull: (factor.score - exact) * factor.weight }))\n    .filter((factor) => factor.pull < 0)\n    .sort((a, b) => a.pull - b.pull)\n    .slice(0, 2)\n    .map((factor) => factor.label.toLowerCase())\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex items-start justify-between gap-3'>\n              <div className='flex min-w-0 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              </div>\n              {!state && band && (\n                <GaugeToneLabel className='mt-0.5' label={band.label} tone={band.tone} />\n              )}\n            </header>\n            <div className='grid gap-6 border-t pt-5 @xl:grid-cols-[14rem_1fr] @xl:items-start'>\n              <div className='flex flex-col items-center gap-3'>\n                <Gauge\n                  bands={bands}\n                  className='w-full max-w-52'\n                  indicator='marker'\n                  label={title}\n                  sweep={270}\n                  value={score}\n                  valueText={`${score} out of 100${band ? `, ${band.label.toLowerCase()}` : ''}`}\n                >\n                  <GaugeValue className='text-4xl'>{score}</GaugeValue>\n                  {!state && (\n                    <span className='text-muted-foreground text-xs'>out of 100</span>\n                  )}\n                </Gauge>\n                {!state && drags.length > 0 && (\n                  <p className='text-muted-foreground text-center text-sm text-balance'>\n                    {joinLabels(drags).replace(/^./, (first) => first.toUpperCase())}{' '}\n                    {drags.length === 1 ? 'pulls' : 'pull'} the score down most.\n                  </p>\n                )}\n                <GaugeLegend bands={bands} className='justify-center' />\n              </div>\n              {(state === undefined || state === 'loading') && (\n                <div className='flex min-w-0 flex-col gap-1'>\n                  <h3 className='text-sm font-medium'>What makes up the score</h3>\n                  <BlockStateRows rows={5} />\n                  {!state && (\n                    <ul className='flex flex-col divide-y'>\n                      {factors.map((factor) => {\n                        const factorBand = getGaugeBand(factor.score, bands)\n                        const config = factorBand\n                          ? gaugeToneConfig[factorBand.tone]\n                          : undefined\n                        return (\n                          <li key={factor.label} className='flex flex-col gap-1.5 py-2.5'>\n                            <div className='flex items-baseline justify-between gap-3'>\n                              <span className='truncate text-sm'>{factor.label}</span>\n                              <span className='flex shrink-0 items-baseline gap-1.5 text-sm'>\n                                <span className='font-medium tabular-nums'>\n                                  {factor.score}\n                                </span>\n                                {factorBand && (\n                                  <GaugeToneLabel\n                                    className='text-xs'\n                                    label={factorBand.label}\n                                    tone={factorBand.tone}\n                                  />\n                                )}\n                              </span>\n                            </div>\n                            <div\n                              aria-hidden\n                              className='bg-muted h-1.5 w-full rounded-full'\n                            >\n                              <div\n                                className={cn(\n                                  'h-full rounded-full',\n                                  config?.swatch ?? 'bg-primary',\n                                )}\n                                style={{\n                                  width: `${Math.min(100, Math.max(0, factor.score))}%`,\n                                }}\n                              />\n                            </div>\n                            <span className='text-muted-foreground flex items-baseline justify-between gap-3 text-xs'>\n                              <span className='min-w-0'>{factor.detail}</span>\n                              <span className='shrink-0 tabular-nums'>\n                                Weighs{' '}\n                                {formatPercent(factor.weight / totalWeight, {\n                                  maximumFractionDigits: 0,\n                                })}\n                              </span>\n                            </span>\n                          </li>\n                        )\n                      })}\n                    </ul>\n                  )}\n                </div>\n              )}\n            </div>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Gauge2, exampleProps as gauge2ExampleProps, type Gauge2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/gauge/gauge-02.tsx"
    }
  ],
  "meta": {
    "shows": "A health score weighted from its contributing factors, marked on poor, fair and good bands, with the factors that pull it down most.",
    "size": "half",
    "usedWith": [
      "status-01",
      "metric-list-03",
      "alerts-01"
    ],
    "component": "Gauge2",
    "props": "{ bands: { from: number; label: string; to: number; tone: \"neutral\" | \"critical\" | \"positive\" | \"caution\" }[]; description: string; factors?: { detail: string; label: string; score: number; weight: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
