{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "approval-card",
  "title": "Approval card",
  "description": "Paged human-in-the-loop questions with single- and multi-select answers, custom input, and submission state.",
  "registryDependencies": [
    "https://beadkit.dev/r/ai-core.json"
  ],
  "files": [
    {
      "path": "components/ai/ApprovalCard.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type ReactNode } from \"react\";\nimport styles from \"./ai-state.module.css\";\n\nexport type ApprovalQuestionType = \"radio\" | \"check\";\n\nexport interface ApprovalQuestion {\n  id?: string;\n  prompt: ReactNode;\n  options: readonly ReactNode[];\n  type?: ApprovalQuestionType;\n  allowCustomAnswer?: boolean;\n}\n\nexport interface ApprovalAnswer {\n  selected: number[];\n  custom?: string;\n}\n\nexport type ApprovalAnswers = Record<string, ApprovalAnswer>;\n\nexport interface ApprovalCardProps {\n  questions: readonly ApprovalQuestion[];\n  onSubmit?: (answers: ApprovalAnswers) => void;\n  title?: ReactNode;\n  className?: string;\n  defaultOpen?: boolean;\n}\n\nexport function ApprovalCard({\n  questions,\n  onSubmit,\n  title,\n  className = \"\",\n  defaultOpen = true,\n}: ApprovalCardProps) {\n  const [questionIndex, setQuestionIndex] = useState(0);\n  const [answers, setAnswers] = useState<Record<number, ApprovalAnswer>>({});\n  const [sent, setSent] = useState(false);\n  const [open, setOpen] = useState(defaultOpen);\n  const timer = useRef<number | null>(null);\n  const answersRef = useRef(answers);\n  answersRef.current = answers;\n  const question = questions[questionIndex];\n\n  useEffect(() => () => {\n    if (timer.current !== null) window.clearTimeout(timer.current);\n  }, []);\n\n  if (!question || questions.length === 0) return null;\n\n  const answer = answers[questionIndex] ?? { selected: [], custom: \"\" };\n  const type = question.type ?? \"radio\";\n  const hasAnswer = answer.selected.length > 0 || Boolean(answer.custom?.trim());\n  const last = questionIndex === questions.length - 1;\n  const keyFor = (item: ApprovalQuestion, index: number) => item.id ?? String(index);\n\n  const clearAdvance = () => {\n    if (timer.current === null) return;\n    window.clearTimeout(timer.current);\n    timer.current = null;\n  };\n\n  const submit = () => {\n    const result: ApprovalAnswers = {};\n    questions.forEach((item, index) => {\n      result[keyFor(item, index)] = answersRef.current[index] ?? { selected: [], custom: \"\" };\n    });\n    onSubmit?.(result);\n    setSent(true);\n  };\n\n  const advance = () => {\n    if (last) submit();\n    else setQuestionIndex((value) => Math.min(questions.length - 1, value + 1));\n  };\n\n  const choose = (optionIndex: number) => {\n    setAnswers((current) => {\n      const previous = current[questionIndex] ?? { selected: [], custom: \"\" };\n      const selected = type === \"radio\"\n        ? [optionIndex]\n        : previous.selected.includes(optionIndex)\n          ? previous.selected.filter((item) => item !== optionIndex)\n          : [...previous.selected, optionIndex];\n      return { ...current, [questionIndex]: { ...previous, selected, custom: type === \"radio\" ? \"\" : previous.custom } };\n    });\n    if (type === \"radio\") {\n      clearAdvance();\n      timer.current = window.setTimeout(() => {\n        timer.current = null;\n        if (last) submit();\n        else setQuestionIndex((value) => Math.min(questions.length - 1, value + 1));\n      }, 480);\n    }\n  };\n\n  const reset = () => {\n    setQuestionIndex(0);\n    setAnswers({});\n    setSent(false);\n    setOpen(true);\n  };\n\n  if (!open) {\n    return <button type=\"button\" onClick={() => setOpen(true)} className={`rounded-md border bg-card px-3 py-2 text-sm font-medium shadow-sm transition-colors hover:bg-muted motion-reduce:transition-none ${className}`}>Open approval</button>;\n  }\n\n  return (\n    <div data-ai-card=\"approval\" className={`flex min-h-48 w-full max-w-sm flex-col ${className}`}>\n      <div className=\"w-full overflow-hidden rounded-xl border bg-card shadow-sm\">\n        {sent ? (\n          <div className=\"flex h-36 flex-col items-center justify-center gap-2\" role=\"status\" aria-live=\"polite\">\n            <span className=\"flex size-6 items-center justify-center rounded-full bg-success text-white\">\n              <svg aria-hidden=\"true\" width=\"12\" height=\"12\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\"><path d=\"m20 6-11 11-5-5\" /></svg>\n            </span>\n            <span className={`${styles.fadeIn} text-sm font-medium`}>Answers sent</span>\n            <button type=\"button\" onClick={reset} className=\"text-xs font-medium text-foreground underline decoration-transparent underline-offset-2 hover:decoration-current\">Start over</button>\n          </div>\n        ) : (\n          <div key={questionIndex} className={`${styles.fadeUp} p-4`}>\n            <div className=\"flex items-start justify-between gap-3\">\n              <div>\n                {title ? <div className=\"mb-1 text-xs font-medium text-muted-foreground\">{title}</div> : null}\n                <div className=\"text-sm font-medium\">{question.prompt}</div>\n              </div>\n              <button type=\"button\" aria-label=\"Dismiss approval\" onClick={() => { clearAdvance(); setOpen(false); }} className=\"-m-1 flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground motion-reduce:transition-none\">\n                <svg aria-hidden=\"true\" width=\"14\" height=\"14\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.2\"><path d=\"M18 6 6 18M6 6l12 12\" /></svg>\n              </button>\n            </div>\n            <fieldset className=\"mt-2 flex flex-col gap-0.5\">\n              <legend className=\"sr-only\">{typeof question.prompt === \"string\" ? question.prompt : \"Approval options\"}</legend>\n              {question.options.map((option, index) => {\n                const selected = answer.selected.includes(index);\n                return <label key={index} className=\"-mx-1.5 flex cursor-pointer items-center gap-2 rounded-md px-1.5 py-1.5 text-left text-sm transition-colors hover:bg-muted motion-reduce:transition-none\">\n                  <input\n                    className=\"peer sr-only\"\n                    type={type === \"radio\" ? \"radio\" : \"checkbox\"}\n                    name={`approval-${question.id ?? questionIndex}`}\n                    checked={selected}\n                    onChange={() => choose(index)}\n                  />\n                  <span aria-hidden=\"true\" className={`flex size-4 shrink-0 items-center justify-center border peer-focus-visible:ring-2 peer-focus-visible:ring-ring peer-focus-visible:ring-offset-2 peer-focus-visible:ring-offset-card ${type === \"radio\" ? \"rounded-full\" : \"rounded\"} ${selected ? \"border-primary bg-primary text-primary-foreground\" : \"border-input text-transparent\"}`}>\n                    {selected ? <svg width=\"11\" height=\"11\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"3\"><path d=\"m20 6-11 11-5-5\" /></svg> : null}\n                  </span><span className={selected ? \"text-foreground\" : \"text-muted-foreground\"}>{option}</span>\n                </label>;\n              })}\n              {question.allowCustomAnswer !== false ? <label className=\"-mx-1.5 flex items-center gap-2 rounded-md px-1.5 py-1.5 transition-colors hover:bg-muted focus-within:bg-muted\">\n                <span aria-hidden=\"true\" className=\"size-4 shrink-0\" /><input value={answer.custom ?? \"\"} onChange={(event) => setAnswers((current) => ({ ...current, [questionIndex]: { selected: type === \"radio\" ? [] : answer.selected, custom: event.target.value } }))} placeholder=\"Type something…\" aria-label=\"Custom answer\" className=\"min-w-0 flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground\" />\n              </label> : null}\n            </fieldset>\n          </div>\n        )}\n        <div className=\"flex items-center justify-between border-t border-border bg-muted/40 px-4 py-2\">\n          <span className=\"flex items-center gap-1.5\">\n            <button type=\"button\" aria-label=\"Previous question\" disabled={questionIndex === 0 || sent} onClick={() => setQuestionIndex((value) => Math.max(0, value - 1))} className=\"flex size-6 items-center justify-center rounded text-muted-foreground hover:bg-muted disabled:opacity-35\"><span aria-hidden=\"true\">‹</span></button>\n            {questions.map((_, index) => <button key={index} type=\"button\" aria-label={`Go to question ${index + 1}`} aria-current={index === questionIndex && !sent ? \"step\" : undefined} disabled={sent} onClick={() => setQuestionIndex(index)} className={`size-2 rounded-full disabled:cursor-default ${index === questionIndex && !sent ? \"bg-foreground ring-2 ring-foreground/20\" : index < questionIndex || sent ? \"bg-muted-foreground\" : \"border border-muted-foreground\"}`} />)}\n            <button type=\"button\" aria-label=\"Next question\" disabled={last || sent} onClick={() => setQuestionIndex((value) => Math.min(questions.length - 1, value + 1))} className=\"flex size-6 items-center justify-center rounded text-muted-foreground hover:bg-muted disabled:opacity-35\"><span aria-hidden=\"true\">›</span></button>\n          </span>\n          {!sent ? <button type=\"button\" aria-label={last ? \"Send answers\" : \"Next question\"} disabled={!hasAnswer} onClick={advance} className=\"flex size-7 items-center justify-center rounded-md bg-primary text-primary-foreground transition-transform enabled:active:scale-95 disabled:bg-muted disabled:text-muted-foreground\"><span aria-hidden=\"true\">↑</span></button> : null}\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport default ApprovalCard;\n",
      "type": "registry:component",
      "target": "@components/ai/ApprovalCard.tsx"
    }
  ],
  "type": "registry:component"
}
