{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "states-04",
  "description": "A chart that loads its own data and, when the request fails, says so where the number and the chart go.",
  "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"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/states/states-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStatus,\n  type LoadStatus,\n  useBlockData,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  chartAxisProps,\n  chartBarCursor,\n  chartBarValueLabel,\n  chartFadedColor,\n  chartGridProps,\n  chartReferenceLabel,\n  chartReferenceProps,\n  ChartTooltipContent,\n  chartValueAxisWidth,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIChange, KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { formatCompact, formatNumber } from '@/registry/lib/dashboardblocks/format'\nimport {\n  Bar,\n  BarChart,\n  CartesianGrid,\n  Cell,\n  LabelList,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface WeeklyPoint {\n  label: string\n  value: number\n}\n\ninterface States4Props {\n  description: string\n  load: () => Promise<WeeklyPoint[]>\n  title: string\n}\n\nconst SERIES_COLOR = 'var(--color-chart-2)'\nconst CHART_HEIGHT = 240\n\nconst exampleProps: States4Props = {\n  description: 'New accounts each week for the last 8 weeks, against the weekly average.',\n  load: () =>\n    new Promise((_, reject) =>\n      setTimeout(() => reject(new Error('The analytics service timed out.')), 1_000),\n    ),\n  title: 'Signups',\n}\n\nfunction toBlockStatus(status: LoadStatus): BlockStatus | undefined {\n  if (status === 'idle' || status === 'loading') return 'loading'\n  if (status === 'error') return 'error'\n  return undefined\n}\n\nconst States4 = (props: States4Props) => {\n  const { description, load, title } = props\n  const { data = [], status: loadStatus } = useBlockData(load, {\n    keepData: false,\n  })\n  const status = toBlockStatus(loadStatus)\n  const lastIndex = data.length - 1\n  const latest = lastIndex >= 0 ? data[lastIndex] : undefined\n  const prior = lastIndex >= 1 ? data[lastIndex - 1] : undefined\n  const average = data.length\n    ? data.reduce((sum, point) => sum + point.value, 0) / data.length\n    : 0\n\n  return (\n    <BlockStateProvider state={getBlockState(status, data)}>\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            {status || !latest ? (\n              <div className='flex flex-col gap-4 border-t pt-4'>\n                <div className='flex flex-col gap-1'>\n                  <KPIValue />\n                  {status === 'loading' && <BlockStateText className='text-sm' />}\n                </div>\n                <ChartPlaceholder height={CHART_HEIGHT} />\n              </div>\n            ) : (\n              <div className='flex flex-col gap-4 border-t pt-4'>\n                <div className='flex flex-col gap-1'>\n                  <p className='text-4xl font-light tracking-tight tabular-nums'>\n                    {formatNumber(latest.value)}\n                  </p>\n                  {prior && (\n                    <KPIChange\n                      comparison={`the week of ${prior.label}`}\n                      previous={prior.value}\n                      value={latest.value}\n                    />\n                  )}\n                </div>\n                <ChartPanelFigure className='h-60'>\n                  <ResponsiveContainer width='100%' height='100%'>\n                    <BarChart\n                      aria-label={`${title} chart`}\n                      data={data}\n                      margin={{ top: 20, right: 8, bottom: 0, left: 0 }}\n                    >\n                      <CartesianGrid {...chartGridProps} />\n                      <XAxis {...chartAxisProps} dataKey='label' />\n                      <YAxis\n                        {...chartAxisProps}\n                        tickFormatter={(value: number) => formatCompact(value)}\n                        width={chartValueAxisWidth}\n                      />\n                      <Tooltip\n                        content={(tooltipProps) => (\n                          <ChartTooltipContent {...tooltipProps} />\n                        )}\n                        cursor={chartBarCursor}\n                      />\n                      <Bar\n                        dataKey='value'\n                        maxBarSize={28}\n                        name={title}\n                        radius={[4, 4, 0, 0]}\n                      >\n                        {data.map((point, index) => (\n                          <Cell\n                            key={point.label}\n                            fill={\n                              index === lastIndex\n                                ? SERIES_COLOR\n                                : chartFadedColor(SERIES_COLOR)\n                            }\n                          />\n                        ))}\n                        <LabelList\n                          dataKey='value'\n                          content={chartBarValueLabel(lastIndex)}\n                        />\n                      </Bar>\n                      <ReferenceLine\n                        {...chartReferenceProps}\n                        y={average}\n                        label={{\n                          ...chartReferenceLabel,\n                          value: `avg ${formatNumber(Math.round(average))}`,\n                          position: 'insideBottomLeft',\n                          dy: -4,\n                        }}\n                      />\n                    </BarChart>\n                  </ResponsiveContainer>\n                </ChartPanelFigure>\n                <ChartPanelTable\n                  caption={`${title}: ${description}`}\n                  columns={[\n                    { key: 'label', label: 'Week of' },\n                    {\n                      format: (value) => formatNumber(value),\n                      key: 'value',\n                      label: title,\n                    },\n                  ]}\n                  rows={data}\n                />\n              </div>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { States4, exampleProps as states4ExampleProps, type States4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/states/states-04.tsx"
    }
  ],
  "meta": {
    "shows": "A chart that loads its own data and, when the request fails, says so where the number and the chart go.",
    "size": "half",
    "usedWith": [
      "states-01",
      "alerts-01"
    ],
    "component": "States4",
    "props": "{ description: string; load: () => Promise<{ label: string; value: number }[]>; title: string }"
  },
  "type": "registry:component"
}
