{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "alerts-02",
  "description": "A metric over the last hour against its threshold line, with the current value, the peak and actions to take.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "button",
    "https://www.dashboardblocks.com/r/radix/alerts.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/alerts/alerts-02.tsx",
      "content": "'use client'\n\nimport {\n  SeverityLabel,\n  type AlertSeverity,\n} from '@/registry/components/dashboardblocks/alerts'\nimport {\n  BlockStateProvider,\n  type BlockStateProps,\n  SkeletonText,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  ChartPlaceholder,\n  chartActiveDot,\n  chartAxisProps,\n  chartGridProps,\n  chartLastDot,\n  chartLineCursor,\n  chartReferenceLabel,\n  chartReferenceProps,\n  ChartTooltipContent,\n  chartValueAxisWidth,\n  useChartId,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport { useState } from 'react'\nimport {\n  Area,\n  CartesianGrid,\n  ComposedChart,\n  Line,\n  ReferenceArea,\n  ReferenceLine,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n} from 'recharts'\n\nimport { Button, buttonVariants } from '@/components/ui/button'\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Reading {\n  label: string\n  value: number\n}\n\ninterface Alerts2Props extends BlockStateProps {\n  description: string\n  formatter?: (value: number) => string\n  metric: string\n  /** Oldest first. Leave out, or pass none, to show the empty state. */\n  readings?: Reading[]\n  runbookHref?: string\n  severity: AlertSeverity\n  threshold: number\n  title: string\n}\n\nconst exampleProps: Alerts2Props = {\n  description: 'checkout-api in eu-west-1, firing for 12 minutes.',\n  formatter: (value) => formatPercent(value / 100, { minimumFractionDigits: 1 }),\n  metric: 'Error rate',\n  readings: Array.from({ length: 30 }, (_, index) => {\n    const minute = 14 * 60 + index * 2\n    const spike = index >= 23 ? 3.6 + Math.sin(index) * 0.8 + (index - 23) * 0.35 : 0\n    return {\n      label: `${Math.floor(minute / 60)}:${String(minute % 60).padStart(2, '0')}`,\n      value: Number((1.1 + Math.sin(index / 2.4) * 0.35 + spike).toFixed(2)),\n    }\n  }),\n  runbookHref: '#',\n  severity: 'critical',\n  threshold: 5,\n  title: 'Error rate above threshold',\n}\n\nconst BREACH_COLOR = 'var(--color-red-500)'\n\nconst Alerts2 = (props: Alerts2Props) => {\n  const {\n    description,\n    formatter = (value) => formatNumber(value),\n    metric,\n    readings = [],\n    runbookHref,\n    severity,\n    errorMessage,\n    status,\n    threshold,\n    title,\n  } = props\n  const id = useChartId()\n  const [acknowledged, setAcknowledged] = useState(false)\n  const state = getBlockState(status, readings)\n  const values = readings.map((reading) => reading.value)\n  const current = values[values.length - 1] ?? 0\n  const highest = Math.max(...values, 0)\n  const lowest = Math.min(...values, highest)\n  const peak = Math.max(highest, threshold)\n  const yMax = Math.ceil(peak * 1.15)\n  const breached = current > threshold\n  const strokeSplit = highest === lowest ? 0 : (highest - threshold) / (highest - lowest)\n  const fillSplit = highest === 0 ? 0 : (highest - threshold) / highest\n  const clamp = (value: number) => Math.min(1, Math.max(0, value))\n  const gap = formatter(Math.abs(current - threshold))\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 flex-wrap items-start justify-between gap-3'>\n              <div className='flex min-w-0 flex-col gap-0.5'>\n                <div className='flex flex-wrap items-baseline gap-x-2'>\n                  <h2 className='font-semibold tracking-tight'>{title}</h2>\n                  {!state && (\n                    <SeverityLabel severity={acknowledged ? 'resolved' : severity}>\n                      {acknowledged ? 'Acknowledged' : undefined}\n                    </SeverityLabel>\n                  )}\n                </div>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </div>\n              <div className='flex flex-wrap gap-2'>\n                {runbookHref && (\n                  <a\n                    href={runbookHref}\n                    className={buttonVariants({ size: 'sm', variant: 'ghost' })}\n                  >\n                    <IconPlaceholder\n                      lucide='BookOpenIcon'\n                      tabler='IconBook'\n                      hugeicons='BookOpen02Icon'\n                      phosphor='BookOpenIcon'\n                      remixicon='RiBookOpenLine'\n                    />\n                    Runbook\n                  </a>\n                )}\n                <Button\n                  variant='outline'\n                  size='sm'\n                  disabled={Boolean(state) || acknowledged}\n                  onClick={() => setAcknowledged(true)}\n                >\n                  {acknowledged ? 'Acknowledged' : 'Acknowledge'}\n                </Button>\n              </div>\n            </header>\n            <dl className='grid grid-cols-2 gap-x-6 gap-y-4 border-t pt-4 @md:grid-cols-[1fr_auto_auto]'>\n              <div className='col-span-2 flex flex-col gap-1 @md:col-span-1'>\n                <dt className='text-muted-foreground text-sm'>{metric} now</dt>\n                <dd>\n                  <KPIValue format={formatter} value={current} />\n                </dd>\n                {state === 'loading' && (\n                  <dd className='text-sm'>\n                    <SkeletonText className='w-48' />\n                  </dd>\n                )}\n                {!state && (\n                  <dd className='text-muted-foreground text-sm'>\n                    <span\n                      className={\n                        breached\n                          ? 'font-medium text-red-700 dark:text-red-400'\n                          : 'font-medium text-emerald-700 dark:text-emerald-400'\n                      }\n                    >\n                      {gap} {breached ? 'above' : 'below'}\n                    </span>{' '}\n                    the {formatter(threshold)} threshold\n                  </dd>\n                )}\n              </div>\n              {state !== 'error' && state !== 'empty' && (\n                <>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Peak</dt>\n                    <dd>\n                      <KPIValue className='text-2xl' format={formatter} value={highest} />\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Threshold</dt>\n                    <dd>\n                      <KPIValue\n                        className='text-2xl'\n                        format={formatter}\n                        value={threshold}\n                      />\n                    </dd>\n                  </div>\n                </>\n              )}\n            </dl>\n            {state && <ChartPlaceholder height={208} />}\n            {!state && (\n              <ChartPanelFigure className='h-52'>\n                <ResponsiveContainer width='100%' height='100%'>\n                  <ComposedChart\n                    aria-label={`${metric} over the last hour`}\n                    data={readings}\n                    margin={{ top: 8, right: 16, bottom: 0, left: 0 }}\n                  >\n                    <defs>\n                      <linearGradient id={`${id}-stroke`} x1='0' x2='0' y1='0' y2='1'>\n                        <stop offset={clamp(strokeSplit)} stopColor={BREACH_COLOR} />\n                        <stop\n                          offset={clamp(strokeSplit)}\n                          stopColor={BREACH_COLOR}\n                          stopOpacity={0}\n                        />\n                      </linearGradient>\n                      <linearGradient id={`${id}-fill`} x1='0' x2='0' y1='0' y2='1'>\n                        <stop offset={0} stopColor={BREACH_COLOR} stopOpacity={0.3} />\n                        <stop\n                          offset={clamp(fillSplit)}\n                          stopColor={BREACH_COLOR}\n                          stopOpacity={0.15}\n                        />\n                        <stop\n                          offset={clamp(fillSplit)}\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0.2}\n                        />\n                        <stop\n                          offset={1}\n                          stopColor='var(--color-chart-2)'\n                          stopOpacity={0}\n                        />\n                      </linearGradient>\n                    </defs>\n                    <CartesianGrid {...chartGridProps} />\n                    <XAxis\n                      {...chartAxisProps}\n                      dataKey='label'\n                      interval='preserveStartEnd'\n                      minTickGap={40}\n                    />\n                    <YAxis\n                      {...chartAxisProps}\n                      domain={[0, yMax]}\n                      tickFormatter={(value: number) => formatter(value)}\n                      width={chartValueAxisWidth}\n                    />\n                    <ReferenceArea\n                      y1={threshold}\n                      y2={yMax}\n                      fill={BREACH_COLOR}\n                      fillOpacity={0.05}\n                      ifOverflow='hidden'\n                    />\n                    <ReferenceLine\n                      {...chartReferenceProps}\n                      y={threshold}\n                      stroke={BREACH_COLOR}\n                      strokeOpacity={1}\n                      label={{\n                        ...chartReferenceLabel,\n                        position: 'insideTopLeft',\n                        value: `Threshold ${formatter(threshold)}`,\n                      }}\n                    />\n                    <Tooltip\n                      content={(tooltipProps) => (\n                        <ChartTooltipContent\n                          {...tooltipProps}\n                          valueFormatter={formatter}\n                        />\n                      )}\n                      cursor={chartLineCursor}\n                    />\n                    <Area\n                      activeDot={chartActiveDot}\n                      dataKey='value'\n                      dot={chartLastDot(\n                        readings.length,\n                        breached ? BREACH_COLOR : 'var(--color-chart-2)',\n                      )}\n                      fill={`url(#${id}-fill)`}\n                      name={metric}\n                      stroke='var(--color-chart-2)'\n                      strokeWidth={2}\n                      type='monotone'\n                    />\n                    <Line\n                      activeDot={false}\n                      dataKey='value'\n                      dot={false}\n                      stroke={`url(#${id}-stroke)`}\n                      strokeWidth={2}\n                      tooltipType='none'\n                      type='monotone'\n                    />\n                  </ComposedChart>\n                </ResponsiveContainer>\n              </ChartPanelFigure>\n            )}\n            {!state && (\n              <ChartPanelTable\n                caption={`${metric} over the last hour. Threshold ${formatter(threshold)}.`}\n                columns={[\n                  { key: 'label', label: 'Time' },\n                  { format: formatter, key: 'value', label: metric },\n                ]}\n                rows={readings}\n              />\n            )}\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { Alerts2, exampleProps as alerts2ExampleProps, type Alerts2Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/alerts/alerts-02.tsx"
    }
  ],
  "meta": {
    "shows": "A metric over the last hour against its threshold line, with the current value, the peak and actions to take.",
    "size": "half",
    "usedWith": [
      "alerts-01",
      "chart-panel-01",
      "status-02"
    ],
    "component": "Alerts2",
    "props": "{ description: string; formatter?: (value: number) => string; metric: string; readings?: { label: string; value: number }[]; runbookHref?: string; severity: \"resolved\" | \"warning\" | \"info\" | \"critical\"; threshold: number; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
