{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "batch-job-matrix",
  "title": "Batch Job Matrix",
  "description": "See a big batch at a glance: one square per job, coloured by status, with pills to pick out failures and a single button to retry them.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/batch-job-matrix/batch-job-matrix.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type CellStatus = \"done\" | \"running\" | \"failed\" | \"waiting\";\n\nexport interface BatchCell {\n  id: string;\n  status: CellStatus;\n  /** Why it failed. */\n  error?: string;\n}\n\nexport interface BatchRow {\n  id: string;\n  label: string;\n  cells: BatchCell[];\n}\n\nexport interface BatchJobMatrixProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  rows?: BatchRow[];\n  /** Milliseconds between each retried job finishing. */\n  retryMs?: number;\n  onRetry?: (ids: string[]) => void;\n}\n\nconst FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\nconst RING = \"outline-none focus-visible:ring-2 focus-visible:ring-[#5E6AD2]\";\nconst CELL_H = 26;\nconst GAP = 3;\n\nconst STATUS: { id: CellStatus; label: string; color: string }[] = [\n  { id: \"done\", label: \"Done\", color: \"#5E6AD2\" },\n  { id: \"running\", label: \"Running\", color: \"#4EA7FC\" },\n  { id: \"failed\", label: \"Failed\", color: \"#EB5757\" },\n  { id: \"waiting\", label: \"Waiting\", color: \"#62666D\" },\n];\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    border: \"rgba(255,255,255,0.08)\",\n    wait: \"rgba(255,255,255,0.08)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    chip: \"rgba(255,255,255,0.05)\",\n    btn: \"#E5E5E6\",\n    btnText: \"#08090A\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(8,9,10,0.08)\",\n    wait: \"rgba(8,9,10,0.08)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    chip: \"rgba(8,9,10,0.04)\",\n    btn: \"#08090A\",\n    btnText: \"#F7F8F8\",\n  },\n};\n\nconst ERRORS = [\"Timed out\", \"File was empty\", \"Rate limited\", \"Bad encoding\"];\n\n/** A fixed sequence, so server and browser draw the same grid. */\nfunction sampleRows(): BatchRow[] {\n  let seed = 7;\n  const next = () => {\n    seed = (seed * 1103515245 + 12345) % 2147483648;\n    return seed / 2147483648;\n  };\n  return [\n    \"Invoices\",\n    \"Receipts\",\n    \"Contracts\",\n    \"Emails\",\n    \"Tickets\",\n    \"Reports\",\n  ].map((label, r) => ({\n    id: label.toLowerCase(),\n    label,\n    cells: Array.from({ length: 24 }, (_, i) => {\n      const roll = next();\n      const reach = 24 - r * 2.5;\n      const status: CellStatus =\n        i > reach\n          ? \"waiting\"\n          : i > reach - 2\n            ? \"running\"\n            : roll < 0.07\n              ? \"failed\"\n              : \"done\";\n      return {\n        id: `${label.toLowerCase()}-${i + 1}`,\n        status,\n        error:\n          status === \"failed\"\n            ? ERRORS[Math.floor(next() * ERRORS.length)]\n            : undefined,\n      };\n    }),\n  }));\n}\n\nconst DEFAULT_ROWS = sampleRows();\n\nfunction useWidth<T extends HTMLElement>(fallback: number) {\n  const ref = React.useRef<T>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => {\n      setWidth(Math.round(entry.contentRect.width));\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\n/** Drawn as SVG, because a tiny border-radius circle turns lumpy on 1x screens. */\nfunction Dot({ size, color }: { size: number; color: string }) {\n  const r = size / 2;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={size}\n      height={size}\n      viewBox={`0 0 ${size} ${size}`}\n      className=\"block shrink-0\"\n    >\n      <circle cx={r} cy={r} r={r} style={{ fill: color }} />\n    </svg>\n  );\n}\n\nexport function BatchJobMatrix({\n  theme = \"dark\",\n  title = \"Process March documents\",\n  subtitle = \"One square per document.\",\n  rows = DEFAULT_ROWS,\n  retryMs = 180,\n  onRetry,\n  className,\n  style,\n  ...rest\n}: BatchJobMatrixProps) {\n  const c = PALETTES[theme];\n  const [fixed, setFixed] = React.useState<string[]>([]);\n  const [retrying, setRetrying] = React.useState(false);\n  const [focus, setFocus] = React.useState<CellStatus | null>(null);\n  const [hover, setHover] = React.useState<{\n    row: string;\n    cell: BatchCell;\n  } | null>(null);\n  const [wrapRef, width] = useWidth<HTMLDivElement>(780);\n  const timer = React.useRef(0);\n\n  const statusOf = (cell: BatchCell): CellStatus =>\n    fixed.includes(cell.id) ? \"done\" : cell.status;\n  const all = rows.flatMap((r) => r.cells);\n  const count = (id: CellStatus) =>\n    all.filter((x) => statusOf(x) === id).length;\n  const failed = all.filter((x) => statusOf(x) === \"failed\");\n  const done = count(\"done\");\n\n  const left = width < 520 ? 66 : 78;\n  const columns = Math.max(...rows.map((r) => r.cells.length), 1);\n  const cellW = Math.max(\n    4,\n    Math.floor((width - left - GAP * columns) / columns),\n  );\n  const height = rows.length * (CELL_H + GAP) - GAP;\n\n  React.useEffect(() => () => window.clearTimeout(timer.current), []);\n\n  const retry = () => {\n    if (retrying || !failed.length) return;\n    const ids = failed.map((x) => x.id);\n    onRetry?.(ids);\n    setRetrying(true);\n    let i = 0;\n    const step = () => {\n      const id = ids[i];\n      setFixed((prev) => [...prev, id]);\n      i += 1;\n      if (i < ids.length) timer.current = window.setTimeout(step, retryMs);\n      else setRetrying(false);\n    };\n    timer.current = window.setTimeout(step, retryMs);\n  };\n\n  const colorOf = (s: CellStatus) =>\n    s === \"waiting\" ? c.wait : STATUS.find((x) => x.id === s)?.color;\n\n  return (\n    <section\n      aria-label={title}\n      className={`@container w-full rounded-2xl border ${className ?? \"\"}`}\n      style={{\n        background: c.card,\n        borderColor: c.border,\n        color: c.text,\n        fontFamily: FONT,\n        ...style,\n      }}\n      {...rest}\n    >\n      <div className=\"p-5 @2xl:p-6\">\n        <div className=\"flex items-start justify-between gap-4\">\n          <header className=\"min-w-0\">\n            <h3 className=\"m-0 text-[20px] leading-[26px] font-[590] tracking-[-0.012em]\">\n              {title}\n            </h3>\n            <p\n              className=\"m-0 mt-1 text-[13px] leading-[20px] tracking-[-0.011em]\"\n              style={{ color: c.sub }}\n            >\n              {subtitle}\n            </p>\n          </header>\n          <div className=\"shrink-0 text-right\">\n            <p\n              aria-live=\"polite\"\n              className=\"m-0 text-[32px] leading-[32px] font-[510] tracking-[-0.022em] tabular-nums\"\n            >\n              {done}\n            </p>\n            <p\n              className=\"m-0 mt-1.5 text-[13px] leading-[20px] tabular-nums\"\n              style={{ color: c.sub }}\n            >\n              Of {all.length} done\n            </p>\n          </div>\n        </div>\n\n        <div\n          role=\"group\"\n          aria-label=\"Highlight\"\n          className=\"mt-3 flex flex-wrap gap-1.5\"\n        >\n          {STATUS.map((s) => {\n            const on = focus === s.id;\n            return (\n              <button\n                key={s.id}\n                type=\"button\"\n                data-status={s.id}\n                aria-pressed={on}\n                onClick={() => setFocus(on ? null : s.id)}\n                className={`flex h-7 cursor-pointer items-center gap-2 rounded-full border-0 px-3 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] motion-safe:transition-opacity motion-safe:duration-150 ${RING}`}\n                style={{\n                  background: c.chip,\n                  color: c.text,\n                  opacity: !focus || on ? 1 : 0.45,\n                }}\n              >\n                <Dot size={8} color={s.color} />\n                {s.label}\n                <span className=\"tabular-nums\" style={{ color: c.sub }}>\n                  {count(s.id)}\n                </span>\n              </button>\n            );\n          })}\n        </div>\n\n        <p\n          className=\"m-0 mt-3 truncate text-[12.5px] leading-[18px] tracking-[-0.008em]\"\n          style={{ height: 18, color: c.sub }}\n        >\n          {hover\n            ? `${hover.row} ${hover.cell.id.split(\"-\").pop()}: ${\n                STATUS.find((x) => x.id === statusOf(hover.cell))?.label\n              }${\n                statusOf(hover.cell) === \"failed\" && hover.cell.error\n                  ? `, ${hover.cell.error}`\n                  : \"\"\n              }`\n            : \"Pick a square for details.\"}\n        </p>\n\n        <div ref={wrapRef} className=\"mt-2 w-full\" style={{ height }}>\n          <svg\n            role=\"img\"\n            aria-label={`${title}: ${done} of ${all.length} done, ${failed.length} failed.`}\n            width={width}\n            height={height}\n            className=\"block\"\n            onPointerLeave={() => setHover(null)}\n          >\n            {rows.map((row, r) => {\n              const y = r * (CELL_H + GAP);\n              return (\n                <g key={row.id}>\n                  <text x={0} y={y + CELL_H / 2 + 4} fontSize=\"12\" fill={c.sub}>\n                    {row.label}\n                  </text>\n                  {row.cells.map((cell, i) => {\n                    const s = statusOf(cell);\n                    return (\n                      <rect\n                        key={cell.id}\n                        data-cell={cell.id}\n                        x={left + i * (cellW + GAP)}\n                        y={y}\n                        width={cellW}\n                        height={CELL_H}\n                        rx=\"3\"\n                        fill={colorOf(s)}\n                        opacity={!focus || focus === s ? 1 : 0.16}\n                        className=\"motion-safe:transition-[fill,opacity] motion-safe:duration-200\"\n                        onPointerEnter={() =>\n                          setHover({ row: row.label, cell })\n                        }\n                      />\n                    );\n                  })}\n                </g>\n              );\n            })}\n          </svg>\n        </div>\n      </div>\n\n      <footer\n        className=\"flex items-center justify-between gap-3 border-t px-5 py-3.5 @2xl:px-6\"\n        style={{ borderColor: c.border }}\n      >\n        <p\n          aria-live=\"polite\"\n          className=\"m-0 min-w-0 truncate text-[13px] leading-[20px] tracking-[-0.011em]\"\n          style={{ color: c.sub }}\n        >\n          {retrying\n            ? \"Retrying failed documents\"\n            : failed.length\n              ? `${failed.length} failed`\n              : \"Nothing has failed\"}\n        </p>\n        <button\n          type=\"button\"\n          data-retry=\"\"\n          disabled={!failed.length || retrying}\n          onClick={retry}\n          className={`h-9 shrink-0 cursor-pointer rounded-full border-0 text-[13px] leading-[20px] font-[510] tracking-[-0.011em] disabled:cursor-default ${RING}`}\n          style={{\n            width: 124,\n            background: failed.length && !retrying ? c.btn : c.chip,\n            color: failed.length && !retrying ? c.btnText : c.faint,\n          }}\n        >\n          Retry failed\n        </button>\n      </footer>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}