{
  "name": "kanban-board",
  "type": "registry:ui",
  "description": "CAI Kanban building block from the Sales board: stages, drag-and-drop, header, tabs, stats, search and filters",
  "files": [
    {
      "path": "components/ui/kanban-board.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { CalendarDays, ChevronLeft, ChevronRight, ExternalLink, GripVertical, Search } from \"lucide-react\"\n\nimport { cn } from \"@/lib/utils\"\n\n/**\n * CAI Kanban building block.\n *\n * Extracted from the CAI Sales opportunities board so any workspace (sales\n * leads, GitHub issues, project items, milestones, agent runs) gets the same\n * board: collapsed rails for empty stages, drag-and-drop between stages, a\n * per-card \"Move to stage\" select for keyboard users, and quiet card chrome.\n *\n * The board is data-only: pass stages and items, and an `onMove` handler when\n * moves are allowed. Without `onMove` the board is read-only.\n */\n\nexport type KanbanTone = \"brand\" | \"violet\" | \"sky\" | \"amber\" | \"rose\" | \"neutral\"\n\nconst toneClass: Record<KanbanTone, string> = {\n  brand: \"bg-emerald-500/10 text-emerald-700 dark:text-emerald-400\",\n  violet: \"bg-violet-500/10 text-violet-700 dark:text-violet-400\",\n  sky: \"bg-sky-500/10 text-sky-700 dark:text-sky-400\",\n  amber: \"bg-amber-500/10 text-amber-700 dark:text-amber-400\",\n  rose: \"bg-rose-500/10 text-rose-700 dark:text-rose-400\",\n  neutral: \"bg-muted text-foreground\",\n}\n\nconst dotClass: Record<KanbanTone, string> = {\n  brand: \"bg-emerald-400\",\n  violet: \"bg-violet-400\",\n  sky: \"bg-sky-400\",\n  amber: \"bg-amber-400\",\n  rose: \"bg-rose-400\",\n  neutral: \"bg-muted-foreground\",\n}\n\nexport type KanbanStage = {\n  id: string\n  label: string\n  /** Dot colour. Defaults to sky for the first stage, brand for the last, violet between. */\n  tone?: KanbanTone\n  /** Optional right-aligned summary in the column header, e.g. a value total. */\n  summary?: string\n}\n\nexport type KanbanBadge = { label: string; tone?: KanbanTone }\n\nexport type KanbanItem = {\n  id: string\n  stageId: string\n  title: string\n  href?: string\n  /** Opens `href` in a new tab with an external-link glyph. */\n  external?: boolean\n  subtitle?: string\n  meta?: string\n  badges?: KanbanBadge[]\n  /** Status line, e.g. \"Shortlisted\" (highlighted) or \"Unreviewed\". */\n  status?: { label: string; highlight?: boolean }\n  /** Small text under the badges, e.g. a value or a progress figure. */\n  detail?: string\n  /** Two-line clamp, e.g. a next step or a description. */\n  description?: string\n  /** 0–100, renders a thin progress bar. */\n  progress?: number\n  date?: { label: string; overdue?: boolean }\n  /** Disables dragging and the stage select for this card only. */\n  locked?: boolean\n}\n\nexport type KanbanBoardProps<T extends KanbanItem = KanbanItem> = {\n  stages: KanbanStage[]\n  items: T[]\n  /** Accessible name and visible heading, e.g. \"Issue workflow\". */\n  title?: string\n  /** Noun used in empty states and labels, e.g. \"issue\". */\n  itemNoun?: string\n  onMove?: (item: T, stageId: string) => void | Promise<void>\n  /** Item currently being moved; its card dims and moves are disabled. */\n  movingId?: string | null\n  /** Extra controls rendered at the bottom of each card. */\n  renderCardActions?: (item: T) => React.ReactNode\n  /** Replace the whole card body; drag handling is kept. */\n  renderCard?: (item: T) => React.ReactNode\n  /** Link renderer, e.g. Next.js Link. Defaults to <a>. */\n  linkComponent?: React.ElementType\n  /** Keep empty stages expanded instead of collapsing them into rails. */\n  expandEmptyStages?: boolean\n  className?: string\n}\n\nfunction defaultTone(index: number, total: number): KanbanTone {\n  if (index === 0) return \"sky\"\n  if (index === total - 1) return \"brand\"\n  return \"violet\"\n}\n\nexport function KanbanBoard<T extends KanbanItem = KanbanItem>({\n  stages,\n  items,\n  title,\n  itemNoun = \"item\",\n  onMove,\n  movingId = null,\n  renderCardActions,\n  renderCard,\n  linkComponent,\n  expandEmptyStages = false,\n  className,\n}: KanbanBoardProps<T>) {\n  const [expanded, setExpanded] = React.useState<Record<string, boolean>>({})\n  const [dragOver, setDragOver] = React.useState<string | null>(null)\n  const canMove = Boolean(onMove)\n\n  const columns = React.useMemo(\n    () => stages.map((stage, index) => ({\n      ...stage,\n      tone: stage.tone ?? defaultTone(index, stages.length),\n      cards: items.filter((item) => item.stageId === stage.id),\n    })),\n    [stages, items],\n  )\n\n  function drop(event: React.DragEvent<HTMLElement>, stageId: string) {\n    event.preventDefault()\n    setDragOver(null)\n    if (!onMove || movingId) return\n    const id = event.dataTransfer.getData(\"text/plain\")\n    const item = items.find((candidate) => candidate.id === id)\n    if (item && !item.locked && item.stageId !== stageId) void onMove(item, stageId)\n  }\n\n  const dropProps = (stageId: string) => canMove ? {\n    onDragOver: (event: React.DragEvent<HTMLElement>) => { event.preventDefault(); setDragOver(stageId) },\n    onDragLeave: (event: React.DragEvent<HTMLElement>) => {\n      if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setDragOver(null)\n    },\n    onDrop: (event: React.DragEvent<HTMLElement>) => drop(event, stageId),\n  } : {}\n\n  const label = title ?? \"Board\"\n\n  return (\n    <section aria-label={label} className={cn(\"min-w-0 space-y-4\", className)}>\n      {title ? <h2 className=\"text-base font-semibold\">{title}</h2> : null}\n      <div className=\"cai-scrollbar max-w-full overflow-x-auto overscroll-x-contain pb-2\" role=\"region\" aria-label={label + \" stages\"} tabIndex={0}>\n        <div className=\"flex min-w-max items-stretch gap-3 p-1\">\n          {columns.map((stage) => {\n            const { cards } = stage\n            if (!cards.length && !expandEmptyStages && !expanded[stage.id]) {\n              return (\n                <button\n                  key={stage.id}\n                  type=\"button\"\n                  aria-expanded={false}\n                  aria-label={\"Expand \" + stage.label}\n                  title={stage.label + \" · Empty.\" + (canMove ? \" Click to expand or drop a card here.\" : \"\")}\n                  onClick={() => setExpanded((current) => ({ ...current, [stage.id]: true }))}\n                  {...dropProps(stage.id)}\n                  className={cn(\n                    \"flex min-h-80 w-12 shrink-0 flex-col items-center gap-3 rounded-xl bg-muted/30 py-4 text-muted-foreground transition-colors hover:bg-muted/60 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-500\",\n                    dragOver === stage.id && \"bg-emerald-500/15 text-foreground ring-2 ring-emerald-500\",\n                  )}\n                >\n                  <span aria-hidden=\"true\" className={cn(\"h-2 w-2 rounded-full\", dotClass[stage.tone])} />\n                  <span className=\"text-xs tabular-nums\">0</span>\n                  <span className=\"text-sm font-medium [writing-mode:vertical-rl]\">{stage.label}</span>\n                  <ChevronRight aria-hidden=\"true\" className=\"mt-auto h-4 w-4\" />\n                </button>\n              )\n            }\n            return (\n              <div\n                key={stage.id}\n                {...dropProps(stage.id)}\n                className={cn(\n                  \"min-h-80 w-[min(20rem,calc(100vw-5rem))] shrink-0 rounded-xl bg-muted/20 p-3 transition-colors\",\n                  dragOver === stage.id && \"bg-emerald-500/5 ring-2 ring-emerald-500/60\",\n                )}\n              >\n                <div className=\"mb-3 flex items-center justify-between gap-3 px-1\">\n                  <div className=\"flex min-w-0 items-center gap-2\">\n                    <span aria-hidden=\"true\" className={cn(\"h-2 w-2 shrink-0 rounded-full\", dotClass[stage.tone])} />\n                    <h3 className=\"truncate text-sm font-semibold\">{stage.label}</h3>\n                    <span className=\"text-xs tabular-nums text-muted-foreground\">{cards.length}</span>\n                  </div>\n                  {!cards.length && !expandEmptyStages ? (\n                    <button\n                      type=\"button\"\n                      aria-label={\"Collapse \" + stage.label}\n                      onClick={() => setExpanded((current) => ({ ...current, [stage.id]: false }))}\n                      className=\"rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-emerald-500\"\n                    >\n                      <ChevronLeft className=\"h-4 w-4\" />\n                    </button>\n                  ) : stage.summary ? <span className=\"text-xs text-muted-foreground\">{stage.summary}</span> : null}\n                </div>\n                <div className=\"cai-scrollbar flex max-h-[70vh] min-w-0 flex-col gap-3 overflow-y-auto\" role=\"region\" aria-label={stage.label + \" \" + itemNoun + \"s\"} tabIndex={0}>\n                  {cards.map((item) => (\n                    <KanbanCard\n                      key={item.id}\n                      item={item}\n                      stages={stages}\n                      draggable={canMove && !item.locked}\n                      moving={movingId === item.id}\n                      moveDisabled={movingId !== null}\n                      onMove={onMove}\n                      onDragEnd={() => setDragOver(null)}\n                      actions={renderCardActions?.(item)}\n                      linkComponent={linkComponent}\n                    >\n                      {renderCard?.(item)}\n                    </KanbanCard>\n                  ))}\n                  {!cards.length ? (\n                    <div className=\"rounded-lg bg-muted/30 px-3 py-8 text-center text-xs text-muted-foreground\">\n                      {canMove ? \"Drop \" + withArticle(itemNoun) + \" here, or choose this stage on its card.\" : \"No \" + itemNoun + \"s in this stage.\"}\n                    </div>\n                  ) : null}\n                </div>\n              </div>\n            )\n          })}\n        </div>\n      </div>\n    </section>\n  )\n}\n\nfunction withArticle(noun: string) {\n  return (/^[aeiou]/i.test(noun) ? \"an \" : \"a \") + noun\n}\n\ntype KanbanCardProps<T extends KanbanItem> = {\n  item: T\n  stages: KanbanStage[]\n  draggable: boolean\n  moving: boolean\n  moveDisabled: boolean\n  onMove?: (item: T, stageId: string) => void | Promise<void>\n  onDragEnd: () => void\n  actions?: React.ReactNode\n  linkComponent?: React.ElementType\n  children?: React.ReactNode\n}\n\nexport function KanbanCard<T extends KanbanItem>({\n  item, stages, draggable, moving, moveDisabled, onMove, onDragEnd, actions, linkComponent, children,\n}: KanbanCardProps<T>) {\n  const LinkTag = (item.external ? \"a\" : linkComponent ?? \"a\") as React.ElementType\n  const titleClass = \"min-w-0 break-words text-left text-sm font-semibold leading-snug\"\n  return (\n    <article\n      draggable={draggable}\n      onDragStart={draggable ? (event) => { event.dataTransfer.setData(\"text/plain\", item.id); event.dataTransfer.effectAllowed = \"move\" } : undefined}\n      onDragEnd={onDragEnd}\n      className={cn(\"w-full min-w-0 shrink-0 rounded-lg bg-card p-4 shadow-sm transition-colors hover:bg-accent/60\", moving && \"opacity-60\")}\n    >\n      {children ?? (\n        <>\n          <div className=\"flex items-start justify-between gap-2\">\n            {item.href ? (\n              <LinkTag\n                href={item.href}\n                {...(item.external ? { target: \"_blank\", rel: \"noopener noreferrer\" } : {})}\n                className={cn(titleClass, \"hover:text-emerald-500 hover:underline\")}\n              >\n                {item.title}\n                {item.external ? <ExternalLink aria-hidden=\"true\" className=\"ml-1 inline h-3 w-3 align-baseline opacity-60\" /> : null}\n              </LinkTag>\n            ) : <p className={titleClass}>{item.title}</p>}\n            {draggable ? <GripVertical aria-hidden=\"true\" className=\"h-4 w-4 shrink-0 cursor-grab text-muted-foreground\" /> : null}\n          </div>\n          {item.subtitle ? <p className=\"mt-1.5 truncate text-xs text-muted-foreground\">{item.subtitle}</p> : null}\n          {item.meta ? <p className=\"mt-1 truncate text-xs text-muted-foreground\">{item.meta}</p> : null}\n          {item.badges?.length ? (\n            <div className=\"mt-2 flex flex-wrap items-center gap-2\">\n              {item.badges.map((badge) => (\n                <span key={badge.label} className={cn(\"inline-flex rounded-full px-2 py-0.5 text-[11px] font-medium\", toneClass[badge.tone ?? \"neutral\"])}>\n                  {badge.label}\n                </span>\n              ))}\n            </div>\n          ) : null}\n          {item.status ? (\n            <p className={cn(\"mt-3 text-xs\", item.status.highlight ? \"font-semibold text-emerald-600 dark:text-emerald-400\" : \"text-muted-foreground\")}>\n              {item.status.label}\n            </p>\n          ) : null}\n          {item.detail ? <p className=\"mt-3 text-xs tabular-nums text-muted-foreground\">{item.detail}</p> : null}\n          {typeof item.progress === \"number\" ? (\n            <div className=\"mt-2 h-1.5 w-full overflow-hidden rounded-full bg-muted\" role=\"progressbar\" aria-valuemin={0} aria-valuemax={100} aria-valuenow={Math.round(item.progress)}>\n              <div className=\"h-full rounded-full bg-emerald-400\" style={{ width: Math.max(0, Math.min(100, item.progress)) + \"%\" }} />\n            </div>\n          ) : null}\n          {item.description ? <p className=\"mt-3 line-clamp-2 text-xs text-muted-foreground\">{item.description}</p> : null}\n        </>\n      )}\n      {actions || onMove ? (\n        <div className=\"mt-4 grid min-w-0 grid-cols-1 gap-2 sm:grid-cols-2\">\n          {actions}\n          {onMove ? (\n            <label className=\"block min-w-0 text-[11px] text-muted-foreground\">\n              Move to stage\n              <select\n                aria-label={\"Move \" + item.title + \" to stage\"}\n                value={item.stageId}\n                disabled={moveDisabled || item.locked}\n                onChange={(event) => void onMove(item, event.target.value)}\n                className=\"mt-1 h-9 w-full min-w-0 rounded-md border-0 bg-muted/60 px-2 text-xs text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-500\"\n              >\n                {stages.map((stage) => <option key={stage.id} value={stage.id}>{stage.label}</option>)}\n              </select>\n            </label>\n          ) : null}\n        </div>\n      ) : null}\n      {item.date ? (\n        <p className={cn(\"mt-3 inline-flex items-center gap-1 text-xs\", item.date.overdue ? \"text-rose-600 dark:text-rose-400\" : \"text-muted-foreground\")}>\n          <CalendarDays aria-hidden=\"true\" className=\"h-3 w-3\" /> {item.date.label}\n        </p>\n      ) : null}\n    </article>\n  )\n}\n\n/* ---------- Workspace chrome: the header, tabs, stats and toolbar around a board ---------- */\n\n/**\n * Compact page header: title, inline children (usually KanbanTabs) and actions on\n * one row, so the board starts high on the page. Brand and product names belong to\n * the app nav or the CAI shell, not here (see skills/cai-mfe-chrome-review).\n */\nexport function KanbanHeader({\n  title, description, actions, children, className,\n}: {\n  title: string\n  /** Optional short status line. Don't restate the product or explain the page. */\n  description?: string\n  actions?: React.ReactNode\n  /** Rendered inline after the title, e.g. <KanbanTabs />. */\n  children?: React.ReactNode\n  className?: string\n}) {\n  return (\n    <header className={cn(\"mb-5 flex flex-wrap items-center justify-between gap-x-4 gap-y-3\", className)}>\n      <div className=\"flex min-w-0 flex-wrap items-center gap-x-4 gap-y-2\">\n        <h1 className=\"text-xl font-semibold tracking-tight\">{title}</h1>\n        {children}\n        {description ? <p className=\"text-xs text-muted-foreground\">{description}</p> : null}\n      </div>\n      {actions ? <div className=\"flex flex-wrap gap-2\">{actions}</div> : null}\n    </header>\n  )\n}\n\nexport type KanbanTab<V extends string = string> = { value: V; label: string; count?: number }\n\nexport function KanbanTabs<V extends string>({\n  tabs, value, onValueChange, label = \"Board views\", className,\n}: {\n  tabs: KanbanTab<V>[]\n  value: V\n  onValueChange: (value: V) => void\n  label?: string\n  className?: string\n}) {\n  return (\n    <nav aria-label={label} className={cn(\"flex flex-wrap gap-1.5\", className)}>\n      {tabs.map((tab) => (\n        <button\n          key={tab.value}\n          type=\"button\"\n          aria-current={value === tab.value ? \"page\" : undefined}\n          onClick={() => onValueChange(tab.value)}\n          className={cn(\n            \"rounded-lg px-3 py-2 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-500\",\n            value === tab.value ? \"bg-emerald-500/15 text-foreground\" : \"text-muted-foreground hover:bg-muted/50 hover:text-foreground\",\n          )}\n        >\n          {tab.label}\n          {typeof tab.count === \"number\" ? <span className=\"ml-1 text-xs opacity-65\">({tab.count})</span> : null}\n        </button>\n      ))}\n    </nav>\n  )\n}\n\nexport type KanbanStat = { label: string; value: React.ReactNode }\n\nexport function KanbanStats({ stats, className }: { stats: KanbanStat[]; className?: string }) {\n  return (\n    <div className={cn(\"flex flex-wrap items-center gap-x-6 gap-y-3\", className)}>\n      {stats.map((stat, index) => (\n        <React.Fragment key={stat.label}>\n          {index > 0 && index === stats.length - 1 && stats.length > 2 ? <div aria-hidden=\"true\" className=\"h-9 w-px bg-border\" /> : null}\n          <div>\n            <p className=\"text-xs text-muted-foreground\">{stat.label}</p>\n            <p className=\"text-2xl font-semibold tabular-nums\">{stat.value}</p>\n          </div>\n        </React.Fragment>\n      ))}\n    </div>\n  )\n}\n\nexport function KanbanSearch({\n  value, onValueChange, placeholder = \"Search…\", className,\n}: {\n  value: string\n  onValueChange: (value: string) => void\n  placeholder?: string\n  className?: string\n}) {\n  return (\n    <label className={cn(\"relative flex w-full max-w-xs items-center sm:w-64\", className)}>\n      <Search aria-hidden=\"true\" className=\"pointer-events-none absolute left-3 h-4 w-4 text-muted-foreground\" />\n      <input\n        value={value}\n        onChange={(event) => onValueChange(event.target.value)}\n        placeholder={placeholder}\n        aria-label={placeholder.replace(/…$/, \"\")}\n        className=\"h-10 w-full rounded-md border-0 bg-muted/50 pl-9 pr-3 text-sm shadow-none outline-none placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-emerald-500\"\n      />\n    </label>\n  )\n}\n\nexport type KanbanViewOption<V extends string = string> = { value: V; label: string; icon?: React.ComponentType<{ className?: string }> }\n\nexport function KanbanViewToggle<V extends string>({\n  options, value, onValueChange, label = \"View mode\",\n}: {\n  options: KanbanViewOption<V>[]\n  value: V\n  onValueChange: (value: V) => void\n  label?: string\n}) {\n  return (\n    <div className=\"inline-flex gap-1 rounded-lg bg-muted/30 p-1\" role=\"group\" aria-label={label}>\n      {options.map(({ value: optionValue, label: optionLabel, icon: Icon }) => (\n        <button\n          key={optionValue}\n          type=\"button\"\n          aria-pressed={value === optionValue}\n          onClick={() => onValueChange(optionValue)}\n          className={cn(\n            \"inline-flex items-center gap-2 rounded-md px-3 py-1.5 text-xs focus-visible:ring-2 focus-visible:ring-emerald-500\",\n            value === optionValue ? \"bg-card font-semibold text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n          )}\n        >\n          {Icon ? <Icon className=\"h-3.5 w-3.5\" /> : null}\n          {optionLabel}\n        </button>\n      ))}\n    </div>\n  )\n}\n\nexport type KanbanFilter = {\n  id: string\n  label: string\n  value: string\n  options: Array<{ value: string; label: string }>\n  onValueChange: (value: string) => void\n}\n\nexport function KanbanFilters({ filters, className }: { filters: KanbanFilter[]; className?: string }) {\n  if (!filters.length) return null\n  return (\n    <div className={cn(\"mb-6 flex flex-wrap items-end gap-3\", className)}>\n      {filters.map((filter) => (\n        <label key={filter.id} className=\"block min-w-40 text-xs text-muted-foreground\">\n          {filter.label}\n          <select\n            value={filter.value}\n            onChange={(event) => filter.onValueChange(event.target.value)}\n            className=\"mt-1 h-10 w-full rounded-md border-0 bg-muted/50 px-3 text-sm text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-500\"\n          >\n            {filter.options.map((option) => <option key={option.value} value={option.value}>{option.label}</option>)}\n          </select>\n        </label>\n      ))}\n    </div>\n  )\n}\n",
      "type": "registry:ui"
    }
  ]
}