{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "data-table-04",
  "description": "Services in a table with their status, latency and error rate, worst status first.",
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/data-table.json",
    "https://www.dashboardblocks.com/r/radix/format.json",
    "https://www.dashboardblocks.com/r/radix/status.json",
    "https://www.dashboardblocks.com/r/radix/status-label.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/data-table/data-table-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  createDataTableColumnHelper,\n  DataTableContent,\n  useDataTable,\n} from '@/registry/components/dashboardblocks/data-table'\nimport type { StatusLevel } from '@/registry/components/dashboardblocks/status'\nimport {\n  StatusLabel,\n  type StatusTone,\n} from '@/registry/components/dashboardblocks/status-label'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport { useMemo } from 'react'\n\nimport { cn } from '@/lib/utils'\n\ninterface ServiceRow {\n  errorRate: number\n  /** p95 latency in milliseconds. */\n  latency: number\n  name: string\n  region: string\n  status: StatusLevel\n}\n\ninterface DataTable4Props extends BlockStateProps {\n  /** Defaults to a sentence naming the services with issues. */\n  description?: string\n  /** An error rate above this, in percent, shows in red. @default 1 */\n  errorRateLimit?: number\n  /** A p95 latency above this, in milliseconds, shows in red. @default 500 */\n  latencyLimit?: number\n  /** Leave out, or pass none, to show the empty state. */\n  rows?: ServiceRow[]\n  title: string\n}\n\nconst exampleProps: DataTable4Props = {\n  rows: [\n    {\n      errorRate: 0.02,\n      latency: 118,\n      name: 'API gateway',\n      region: 'us-east-1',\n      status: 'operational',\n    },\n    {\n      errorRate: 1.84,\n      latency: 642,\n      name: 'Search',\n      region: 'eu-west-1',\n      status: 'degraded',\n    },\n    {\n      errorRate: 0.05,\n      latency: 96,\n      name: 'Auth',\n      region: 'us-east-1',\n      status: 'operational',\n    },\n    {\n      errorRate: 12.6,\n      latency: 2_310,\n      name: 'Webhooks',\n      region: 'ap-southeast-2',\n      status: 'partial',\n    },\n    {\n      errorRate: 0,\n      latency: 0,\n      name: 'Billing',\n      region: 'us-west-2',\n      status: 'maintenance',\n    },\n    {\n      errorRate: 0.11,\n      latency: 184,\n      name: 'Media uploads',\n      region: 'eu-central-1',\n      status: 'operational',\n    },\n  ],\n  title: 'Services',\n}\n\nconst STATUS: Record<StatusLevel, { label: string; tone: StatusTone }> = {\n  degraded: { label: 'Degraded', tone: 'warning' },\n  maintenance: { label: 'Maintenance', tone: 'info' },\n  major: { label: 'Major outage', tone: 'bad' },\n  operational: { label: 'Operational', tone: 'good' },\n  partial: { label: 'Partial outage', tone: 'bad' },\n  unknown: { label: 'No data', tone: 'neutral' },\n}\n\nconst STATUS_RANK: Record<StatusLevel, number> = {\n  major: 5,\n  partial: 4,\n  degraded: 3,\n  maintenance: 2,\n  operational: 1,\n  unknown: 0,\n}\n\nconst AFFECTED: StatusLevel[] = ['degraded', 'partial', 'major']\n\nconst summarize = (rows: ServiceRow[]) => {\n  const affected = rows\n    .filter((row) => AFFECTED.includes(row.status))\n    .sort((a, b) => STATUS_RANK[b.status] - STATUS_RANK[a.status])\n  const worst = affected[0]\n  if (!worst) return `All ${rows.length} services are running normally.`\n  return `${affected.length} of ${rows.length} services with issues; ${worst.name} is failing ${formatPercent(worst.errorRate / 100)} of requests.`\n}\n\nconst overLimit = 'font-medium text-red-700 dark:text-red-400'\n\nconst noRows: ServiceRow[] = []\n\nconst columnHelper = createDataTableColumnHelper<ServiceRow>()\n\nconst getColumns = (latencyLimit: number, errorRateLimit: number) =>\n  columnHelper.columns([\n    columnHelper.accessor('name', {\n      cell: ({ row }) => (\n        <>\n          <span className='block truncate'>{row.original.name}</span>\n          <span className='text-muted-foreground block text-xs font-normal'>\n            {row.original.region}\n          </span>\n        </>\n      ),\n      header: 'Service',\n      meta: { primary: true, truncate: true },\n    }),\n    columnHelper.accessor((row) => STATUS_RANK[row.status], {\n      cell: ({ row }) => {\n        const status = STATUS[row.original.status]\n        return <StatusLabel tone={status.tone}>{status.label}</StatusLabel>\n      },\n      header: 'Status',\n      id: 'status',\n    }),\n    columnHelper.accessor('latency', {\n      cell: ({ getValue, row }) =>\n        row.original.status === 'maintenance' ? (\n          '—'\n        ) : (\n          <span className={cn(getValue() > latencyLimit && overLimit)}>\n            {formatNumber(getValue())} ms\n          </span>\n        ),\n      header: 'p95 latency',\n      meta: { align: 'end' },\n    }),\n    columnHelper.accessor('errorRate', {\n      cell: ({ getValue, row }) =>\n        row.original.status === 'maintenance' ? (\n          '—'\n        ) : (\n          <span className={cn(getValue() > errorRateLimit && overLimit)}>\n            {formatPercent(getValue() / 100, { maximumFractionDigits: 2 })}\n          </span>\n        ),\n      header: 'Error rate',\n      meta: { align: 'end' },\n    }),\n  ])\n\nconst DataTable4 = (props: DataTable4Props) => {\n  const {\n    errorRateLimit = 1,\n    latencyLimit = 500,\n    rows = noRows,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const { description = summarize(rows) } = props\n  const columns = useMemo(\n    () => getColumns(latencyLimit, errorRateLimit),\n    [latencyLimit, errorRateLimit],\n  )\n  const table = useDataTable({\n    columns,\n    data: rows,\n    initialState: { sorting: [{ desc: true, id: 'status' }] },\n  })\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={getBlockState(status, rows)}>\n      <section className='@container/data-table flex flex-col gap-3'>\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            <BlockReady className='text-sm'>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </BlockReady>\n          </div>\n        </header>\n        <DataTableContent\n          caption={`${title}: ${description}`}\n          loadingRows={6}\n          table={table}\n        />\n      </section>\n    </BlockStateProvider>\n  )\n}\n\nexport { DataTable4, exampleProps as dataTable4ExampleProps, type DataTable4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/data-table/data-table-04.tsx"
    }
  ],
  "meta": {
    "shows": "Services in a table with their status, latency and error rate, worst status first.",
    "size": "full",
    "usedWith": [
      "status-01",
      "alerts-01",
      "deployments-01"
    ],
    "component": "DataTable4",
    "props": "{ description?: string; errorRateLimit?: number; latencyLimit?: number; rows?: { errorRate: number; latency: number; name: string; region: string; status: \"operational\" | \"degraded\" | \"partial\" | \"major\" | \"maintenance\" | \"unknown\" }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
