{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "alerts-03",
  "description": "A table of alert rules with their conditions, severities and channels, and a switch to pause or resume each one.",
  "registryDependencies": [
    "switch",
    "https://www.dashboardblocks.com/r/radix/alerts.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/table.json",
    "https://www.dashboardblocks.com/r/radix/format.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/alerts/alerts-03.tsx",
      "content": "'use client'\n\nimport {\n  type AlertSeverity,\n  SeverityLabel,\n} from '@/registry/components/dashboardblocks/alerts'\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableStateRows,\n} from '@/registry/components/dashboardblocks/table'\nimport { formatRelativeTime } from '@/registry/lib/dashboardblocks/format'\nimport { useState } from 'react'\n\nimport { Switch } from '@/components/ui/switch'\n\ninterface AlertRule {\n  channels: string[]\n  condition: string\n  enabled: boolean\n  id: string\n  lastTriggered?: Date\n  name: string\n  severity: AlertSeverity\n}\n\ninterface Alerts3Props extends BlockStateProps {\n  /** Defaults to a sentence counting the active rules and the last one to fire. */\n  description?: string\n  now: Date\n  onEnabledChange?: (id: string, enabled: boolean) => void\n  /** Leave out, or pass none, to show the empty state. */\n  rules?: AlertRule[]\n  title: string\n}\n\nconst NOW = Date.UTC(2026, 8, 25, 14, 30)\nconst hoursAgo = (hours: number) => new Date(NOW - hours * 3_600_000)\n\nconst exampleProps: Alerts3Props = {\n  now: new Date(NOW),\n  rules: [\n    {\n      channels: ['PagerDuty', 'Slack'],\n      condition: 'Error rate > 5% for 5 min',\n      enabled: true,\n      id: 'r1',\n      lastTriggered: hoursAgo(0.1),\n      name: 'API errors',\n      severity: 'critical',\n    },\n    {\n      channels: ['Slack'],\n      condition: 'p95 latency > 800 ms for 10 min',\n      enabled: true,\n      id: 'r2',\n      lastTriggered: hoursAgo(0.9),\n      name: 'Slow search',\n      severity: 'warning',\n    },\n    {\n      channels: ['Email'],\n      condition: 'Disk usage > 80%',\n      enabled: true,\n      id: 'r3',\n      lastTriggered: hoursAgo(26),\n      name: 'Database disk',\n      severity: 'warning',\n    },\n    {\n      channels: ['Slack', 'Email'],\n      condition: 'Daily signups < 50% of 7-day average',\n      enabled: false,\n      id: 'r4',\n      name: 'Signup drop',\n      severity: 'info',\n    },\n  ],\n  title: 'Alert rules',\n}\n\nconst summarize = (rules: AlertRule[], active: number, now: Date) => {\n  const fired = rules\n    .filter((rule) => rule.lastTriggered)\n    .sort(\n      (a, b) => (b.lastTriggered?.getTime() ?? 0) - (a.lastTriggered?.getTime() ?? 0),\n    )[0]\n  const count = `${active} of ${rules.length} rules active.`\n  if (!fired?.lastTriggered) return count\n  return `${count} ${fired.name} fired last, ${formatRelativeTime(fired.lastTriggered, now)}.`\n}\n\nconst Alerts3 = (props: Alerts3Props) => {\n  const { now, onEnabledChange, rules = [], errorMessage, status, title } = props\n  const [enabled, setEnabled] = useState<ReadonlyMap<string, boolean>>(() => new Map())\n  const state = getBlockState(status, rules)\n  const isEnabled = (rule: AlertRule) => enabled.get(rule.id) ?? rule.enabled\n  const active = rules.filter(isEnabled).length\n  const { description = summarize(rules, active, now) } = props\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <section className='@container/data-table flex flex-col gap-3'>\n        <header className='flex flex-col gap-0.5'>\n          <h2 className='font-semibold tracking-tight'>{title}</h2>\n          <BlockReady className='text-sm'>\n            <p aria-live='polite' className='text-muted-foreground text-sm'>\n              {description}\n            </p>\n          </BlockReady>\n        </header>\n        <DataTable caption={title}>\n          <DataTableHeader>\n            <DataTableRow>\n              <DataTableHead>Rule</DataTableHead>\n              <DataTableHead>Severity</DataTableHead>\n              <DataTableHead>Notifies</DataTableHead>\n              <DataTableHead>Last triggered</DataTableHead>\n              <DataTableHead>Status</DataTableHead>\n            </DataTableRow>\n          </DataTableHeader>\n          <DataTableBody>\n            <DataTableStateRows colSpan={5} rows={4} />\n            {!state &&\n              rules.map((rule) => {\n                const ruleEnabled = isEnabled(rule)\n                return (\n                  <DataTableRow key={rule.id}>\n                    <DataTableCell primary truncate>\n                      <span className='block truncate'>{rule.name}</span>\n                      <span className='text-muted-foreground block font-mono text-xs font-normal whitespace-normal'>\n                        {rule.condition}\n                      </span>\n                    </DataTableCell>\n                    <DataTableCell>\n                      <SeverityLabel severity={rule.severity} />\n                    </DataTableCell>\n                    <DataTableCell className='text-muted-foreground whitespace-nowrap'>\n                      {rule.channels.join(', ')}\n                    </DataTableCell>\n                    <DataTableCell className='text-muted-foreground whitespace-nowrap'>\n                      {rule.lastTriggered ? (\n                        <time dateTime={rule.lastTriggered.toISOString()}>\n                          {formatRelativeTime(rule.lastTriggered, now)}\n                        </time>\n                      ) : (\n                        'Never'\n                      )}\n                    </DataTableCell>\n                    <DataTableCell>\n                      <div className='inline-flex min-h-6 items-center gap-2 text-sm whitespace-nowrap'>\n                        <Switch\n                          aria-label={rule.name}\n                          checked={ruleEnabled}\n                          onCheckedChange={(checked) => {\n                            setEnabled((current) =>\n                              new Map(current).set(rule.id, checked),\n                            )\n                            onEnabledChange?.(rule.id, checked)\n                          }}\n                        />\n                        <span aria-hidden className='w-12'>\n                          {ruleEnabled ? 'Active' : 'Paused'}\n                        </span>\n                      </div>\n                    </DataTableCell>\n                  </DataTableRow>\n                )\n              })}\n          </DataTableBody>\n        </DataTable>\n      </section>\n    </BlockStateProvider>\n  )\n}\n\nexport { Alerts3, exampleProps as alerts3ExampleProps, type Alerts3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/alerts/alerts-03.tsx"
    }
  ],
  "meta": {
    "shows": "A table of alert rules with their conditions, severities and channels, and a switch to pause or resume each one.",
    "size": "full",
    "usedWith": [
      "alerts-01",
      "alerts-04"
    ],
    "component": "Alerts3",
    "props": "{ description?: string; now: Date; onEnabledChange?: (id: string, enabled: boolean) => void; rules?: { channels: string[]; condition: string; enabled: boolean; id: string; lastTriggered?: Date; name: string; severity: \"resolved\" | \"warning\" | \"info\" | \"critical\" }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
