{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "staged-action",
  "title": "Staged action",
  "description": "Human-in-the-loop staged action card: pre-filled fields grounded in agent context that the user can edit inline before clicking Approve or Discard.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "registry/components/staged-action.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { Banknote, Mail, Pencil, RotateCcw, Rocket, ShieldCheck, Sparkles, X } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nexport type StagedField = {\n  key: string;\n  label: string;\n  value: string;\n  editable?: boolean;\n  mono?: boolean;\n};\n\nexport type StagedActionProps = {\n  title?: string;\n  subtitle?: string;\n  badge?: string;\n  guardrail?: string;\n  /** Icon in the header tile. */\n  icon?: React.ReactNode;\n  fields?: StagedField[];\n  /** Shown after approval, e.g. \"Transfer sent · txn_8F2K91\". */\n  doneLabel?: string;\n  /** Called with the final (possibly edited) key/value map when approved. */\n  onApprove?: (values: Record<string, string>) => void;\n  /** Called when the user discards the staged action. */\n  onDiscard?: () => void;\n  /** Loop a scripted edit → approve → execute demo across three action types. */\n  autoPlay?: boolean;\n  className?: string;\n};\n\ntype Preset = {\n  title: string;\n  subtitle: string;\n  guardrail: string;\n  icon: typeof Banknote;\n  fields: StagedField[];\n  edit: { key: string; to: string };\n  done: string;\n};\n\nconst PRESETS: Preset[] = [\n  {\n    title: \"Wire transfer\",\n    subtitle: \"Drafted from invoice INV-2084\",\n    guardrail: \"Within the $25,000 single-approver limit\",\n    icon: Banknote,\n    fields: [\n      { key: \"amount\", label: \"Amount\", value: \"$12,500.00\", editable: true, mono: true },\n      { key: \"recipient\", label: \"Recipient\", value: \"Acme Cloud Infrastructure\", editable: true },\n      { key: \"account\", label: \"From\", value: \"Operating ••4819\", editable: false, mono: true },\n      { key: \"memo\", label: \"Memo\", value: \"Q4 GPU reservation · INV-2084\", editable: true },\n    ],\n    edit: { key: \"amount\", to: \"$11,800.00\" },\n    done: \"Transfer sent · txn_8F2K91\",\n  },\n  {\n    title: \"Bulk email\",\n    subtitle: \"Drafted from the trial-expiry playbook\",\n    guardrail: \"Recipients opted in · unsubscribe link added\",\n    icon: Mail,\n    fields: [\n      { key: \"to\", label: \"To\", value: \"248 trial users\", editable: false },\n      { key: \"subject\", label: \"Subject\", value: \"Your trial ends in 3 days\", editable: true },\n      { key: \"send\", label: \"Send at\", value: \"Tomorrow, 09:00\", editable: true, mono: true },\n      { key: \"from\", label: \"From\", value: \"team@studio.test\", editable: false, mono: true },\n    ],\n    edit: { key: \"send\", to: \"Today, 16:00\" },\n    done: \"Scheduled · 248 emails queued\",\n  },\n  {\n    title: \"Publish release\",\n    subtitle: \"Drafted from 12 merged pull requests\",\n    guardrail: \"CI green on main · changelog generated\",\n    icon: Rocket,\n    fields: [\n      { key: \"tag\", label: \"Tag\", value: \"v2.4.0\", editable: true, mono: true },\n      { key: \"name\", label: \"Title\", value: \"Offline mode & faster sync\", editable: true },\n      { key: \"target\", label: \"Target\", value: \"main @ 9c1e2fa\", editable: false, mono: true },\n      { key: \"notes\", label: \"Notes\", value: \"9 features · 3 fixes\", editable: true },\n    ],\n    edit: { key: \"tag\", to: \"v2.5.0\" },\n    done: \"Release v2.5.0 published\",\n  },\n];\n\nconst EASE = [0.22, 1, 0.36, 1] as const;\nconst TICK = 100;\nconst LOOP = 8200;\n\nfunction useDemoClock(enabled: boolean, loop: number) {\n  const ref = useRef<HTMLDivElement>(null);\n  const [visible, setVisible] = useState(false);\n  const [elapsed, setElapsed] = useState(0);\n  useEffect(() => {\n    const el = ref.current;\n    if (!el || !enabled) return;\n    const io = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));\n    io.observe(el);\n    return () => io.disconnect();\n  }, [enabled]);\n  useEffect(() => {\n    if (!enabled || !visible) return;\n    const id = setInterval(() => setElapsed((e) => e + TICK), TICK);\n    return () => clearInterval(id);\n  }, [enabled, visible]);\n  return { ref, t: elapsed % loop, cycle: Math.floor(elapsed / loop) };\n}\n\ntype Status = \"staged\" | \"executing\" | \"done\" | \"discarded\";\ntype View = { values: Record<string, string>; editing: string | null; status: Status; progress: number; pressing: boolean };\n\nconst initialValues = (fields: StagedField[]) => Object.fromEntries(fields.map((f) => [f.key, f.value]));\n\nfunction demoView(t: number, p: Preset): View {\n  const values = initialValues(p.fields);\n  const from = values[p.edit.key];\n  let editing: string | null = null;\n  if (t >= 1200 && t < 2700) {\n    editing = p.edit.key;\n    // Clear the old value, then type the new one.\n    const clear = Math.max(0, from.length - Math.floor(Math.max(0, t - 1500) / 40));\n    const typed = t >= 1500 + from.length * 40 ? p.edit.to.slice(0, Math.floor((t - 1500 - from.length * 40) / 60) + 1) : \"\";\n    values[p.edit.key] = clear > 0 && !typed ? from.slice(0, clear) : typed;\n  }\n  if (t >= 2700) values[p.edit.key] = p.edit.to;\n  const status: Status = t >= 5000 ? \"done\" : t >= 3700 ? \"executing\" : \"staged\";\n  return { values, editing, status, progress: status === \"executing\" ? (t - 3700) / 1300 : status === \"done\" ? 1 : 0, pressing: t >= 3400 && t < 3700 };\n}\n\n/** A pre-filled action the agent staged (transfer, bulk email, release…). Fields edit inline before Approve & execute. */\nexport function StagedAction({\n  title,\n  subtitle,\n  badge = \"Staged\",\n  guardrail,\n  icon,\n  fields: fieldsProp,\n  doneLabel,\n  onApprove,\n  onDiscard,\n  autoPlay = false,\n  className,\n}: StagedActionProps) {\n  const reduce = useReducedMotion() ?? false;\n  const [touched, setTouched] = useState(false);\n  const [frozen, setFrozen] = useState(0);\n  const demo = autoPlay && !reduce && !touched && !fieldsProp;\n  const { ref, t, cycle } = useDemoClock(demo, LOOP);\n  const preset = PRESETS[demo ? cycle % PRESETS.length : frozen];\n  const fields = fieldsProp ?? preset.fields;\n  const [view, setView] = useState<View>({ values: initialValues(fields), editing: null, status: \"staged\", progress: 0, pressing: false });\n\n  // Sync the scripted frame during render (no effect → no extra commit per tick).\n  const [syncedT, setSyncedT] = useState(-1);\n  if (demo && t !== syncedT) {\n    setSyncedT(t);\n    setView(demoView(t, preset));\n  }\n\n  const originals = initialValues(fields);\n  const Icon = preset.icon;\n  const shownTitle = title ?? preset.title;\n  const shownSubtitle = subtitle ?? preset.subtitle;\n  const shownGuardrail = guardrail ?? preset.guardrail;\n  const shownDone = doneLabel ?? (fieldsProp ? \"Approved and executed\" : preset.done);\n  const editedCount = fields.filter((f) => view.values[f.key] !== originals[f.key]).length;\n\n  const patch = (p: Partial<View>) => {\n    if (demo) setFrozen(cycle % PRESETS.length);\n    setTouched(true);\n    setView((v) => ({ ...v, ...p }));\n  };\n\n  const approve = () => {\n    patch({ editing: null, status: \"executing\", progress: 0 });\n    onApprove?.(view.values);\n    let p = 0;\n    const id = setInterval(() => {\n      p += 0.1;\n      setView((v) => ({ ...v, progress: Math.min(1, p), status: p >= 1 ? \"done\" : \"executing\" }));\n      if (p >= 1) clearInterval(id);\n    }, 90);\n  };\n\n  const discard = () => {\n    patch({ editing: null, status: \"discarded\" });\n    onDiscard?.();\n  };\n\n  const reset = () => patch({ values: originals, editing: null, status: \"staged\", progress: 0 });\n\n  const onKey = (e: KeyboardEvent<HTMLInputElement>, key: string) => {\n    if (e.key === \"Enter\") patch({ editing: null });\n    if (e.key === \"Escape\") patch({ editing: null, values: { ...view.values, [key]: originals[key] } });\n  };\n\n  const locked = view.status !== \"staged\";\n\n  return (\n    <div ref={ref} className={cn(\"w-full max-w-md text-sm\", className)}>\n      <div className=\"overflow-hidden rounded-2xl border bg-card text-card-foreground shadow-[0_1px_2px_rgb(0_0_0/0.04),0_12px_32px_-18px_rgb(0_0_0/0.25)]\">\n        <AnimatePresence mode=\"wait\" initial={false}>\n          <motion.div\n            key={shownTitle}\n            initial={reduce ? false : { opacity: 0, y: 10, filter: \"blur(3px)\" }}\n            animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n            exit={reduce ? undefined : { opacity: 0, y: -10, filter: \"blur(3px)\" }}\n            transition={{ duration: 0.35, ease: EASE }}\n          >\n            {/* Header */}\n            <div className=\"flex items-center gap-3 bg-gradient-to-b from-muted/50 to-transparent px-4 py-3\">\n              <span className=\"grid size-9 shrink-0 place-items-center rounded-xl border bg-background shadow-xs\">\n                {icon ?? <Icon className=\"size-4\" aria-hidden />}\n              </span>\n              <div className=\"min-w-0 flex-1\">\n                <div className=\"flex items-center gap-2\">\n                  <span className=\"truncate font-medium\">{shownTitle}</span>\n                  <span\n                    className={cn(\n                      \"rounded-full border px-1.5 font-mono text-[10px] leading-4 transition-colors\",\n                      view.status === \"done\"\n                        ? \"border-emerald-500/40 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300\"\n                        : view.status === \"discarded\"\n                          ? \"text-muted-foreground\"\n                          : \"border-dashed border-foreground/30 text-foreground\",\n                    )}\n                  >\n                    {view.status === \"done\" ? \"Executed\" : view.status === \"discarded\" ? \"Discarded\" : view.status === \"executing\" ? \"Running\" : badge}\n                  </span>\n                </div>\n                <p className=\"mt-0.5 flex items-center gap-1 truncate text-xs text-muted-foreground\">\n                  <Sparkles className=\"size-3 shrink-0\" aria-hidden />\n                  {shownSubtitle}\n                </p>\n              </div>\n            </div>\n\n            {/* Fields */}\n            <dl className=\"mx-3 divide-y rounded-xl border bg-background\">\n              {fields.map((f) => {\n                const value = view.values[f.key] ?? \"\";\n                const editing = view.editing === f.key && !locked;\n                const edited = value !== originals[f.key] && !editing;\n                const canEdit = f.editable !== false && !locked;\n                return (\n                  <div key={f.key} className={cn(\"group relative flex min-h-10 items-center gap-3 px-3 text-xs transition-colors\", editing && \"bg-muted/60\")}>\n                    <dt className=\"flex w-20 shrink-0 items-center gap-1.5 text-muted-foreground\">\n                      {f.label}\n                      <AnimatePresence>\n                        {edited && (\n                          <motion.span\n                            initial={reduce ? false : { scale: 0 }}\n                            animate={{ scale: 1 }}\n                            exit={{ scale: 0 }}\n                            className=\"size-1.5 rounded-full bg-sky-500\"\n                            title=\"Edited\"\n                          />\n                        )}\n                      </AnimatePresence>\n                    </dt>\n                    <dd className=\"flex min-w-0 flex-1 items-center justify-end gap-2\">\n                      {editing ? (\n                        <input\n                          autoFocus={touched}\n                          aria-label={f.label}\n                          value={value}\n                          onChange={(e) => patch({ values: { ...view.values, [f.key]: e.target.value } })}\n                          onBlur={() => touched && patch({ editing: null })}\n                          onKeyDown={(e) => onKey(e, f.key)}\n                          className={cn(\n                            \"h-7 w-full max-w-60 rounded-md border border-sky-500/60 bg-background px-2 text-right text-xs shadow-[0_0_0_3px_rgb(14_165_233/0.15)] outline-none\",\n                            f.mono && \"font-mono\",\n                          )}\n                        />\n                      ) : (\n                        <button\n                          type=\"button\"\n                          disabled={!canEdit}\n                          onClick={() => patch({ editing: f.key })}\n                          className={cn(\n                            \"flex min-w-0 items-center gap-1.5 rounded-md px-1.5 py-1 text-right outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                            canEdit ? \"cursor-text hover:bg-muted\" : \"cursor-default\",\n                          )}\n                        >\n                          {edited && <span className=\"truncate text-[10px] text-muted-foreground line-through\">{originals[f.key]}</span>}\n                          <motion.span\n                            key={value}\n                            initial={reduce || !edited ? false : { opacity: 0, y: 4 }}\n                            animate={{ opacity: 1, y: 0 }}\n                            className={cn(\"truncate font-medium\", f.mono && \"font-mono\", view.status === \"discarded\" && \"text-muted-foreground line-through\")}\n                          >\n                            {value}\n                          </motion.span>\n                          {canEdit && <Pencil className=\"size-3 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover:opacity-100\" aria-hidden />}\n                        </button>\n                      )}\n                    </dd>\n                  </div>\n                );\n              })}\n            </dl>\n          </motion.div>\n        </AnimatePresence>\n\n        {/* Footer */}\n        <div className=\"mt-3 border-t bg-muted/30 px-3 py-2.5\">\n          <AnimatePresence mode=\"wait\" initial={false}>\n            {view.status === \"done\" || view.status === \"discarded\" ? (\n              <motion.div\n                key=\"result\"\n                role=\"status\"\n                initial={reduce ? false : { opacity: 0, y: 6 }}\n                animate={{ opacity: 1, y: 0 }}\n                exit={reduce ? undefined : { opacity: 0 }}\n                transition={{ duration: 0.3, ease: EASE }}\n                className=\"flex h-8 items-center gap-2 text-xs\"\n              >\n                {view.status === \"done\" ? (\n                  <svg viewBox=\"0 0 24 24\" className=\"size-6 shrink-0\" aria-hidden>\n                    <motion.circle cx=\"12\" cy=\"12\" r=\"11\" className=\"fill-emerald-500\" initial={reduce ? false : { scale: 0 }} animate={{ scale: 1 }} transition={{ type: \"spring\", stiffness: 500, damping: 18 }} style={{ transformOrigin: \"center\" }} />\n                    <motion.path\n                      d=\"M7 12.5l3.2 3.2L17 9\"\n                      fill=\"none\"\n                      stroke=\"white\"\n                      strokeWidth=\"2.4\"\n                      strokeLinecap=\"round\"\n                      strokeLinejoin=\"round\"\n                      initial={reduce ? false : { pathLength: 0 }}\n                      animate={{ pathLength: 1 }}\n                      transition={{ delay: 0.15, duration: 0.35, ease: \"easeOut\" }}\n                    />\n                  </svg>\n                ) : (\n                  <span className=\"grid size-6 shrink-0 place-items-center rounded-full bg-muted text-muted-foreground\">\n                    <X className=\"size-3.5\" aria-hidden />\n                  </span>\n                )}\n                <span className=\"min-w-0 flex-1 truncate font-medium\">{view.status === \"done\" ? shownDone : \"Discarded · nothing was executed\"}</span>\n                <button\n                  type=\"button\"\n                  onClick={reset}\n                  className=\"inline-flex h-7 shrink-0 cursor-pointer items-center gap-1 rounded-md px-2 text-[11px] text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\"\n                >\n                  <RotateCcw className=\"size-3\" aria-hidden />\n                  Reset\n                </button>\n              </motion.div>\n            ) : (\n              <motion.div key=\"actions\" exit={reduce ? undefined : { opacity: 0, y: -6 }} transition={{ duration: 0.2 }} className=\"flex h-8 items-center gap-2\">\n                <span className=\"flex min-w-0 flex-1 items-center gap-1.5 text-[11px] text-muted-foreground\">\n                  <ShieldCheck className=\"size-3.5 shrink-0 text-emerald-600 dark:text-emerald-400\" aria-hidden />\n                  <span className=\"truncate\">{editedCount > 0 ? `${editedCount} field edited · ${shownGuardrail}` : shownGuardrail}</span>\n                </span>\n                <button\n                  type=\"button\"\n                  onClick={discard}\n                  disabled={locked}\n                  className=\"h-8 shrink-0 cursor-pointer rounded-md border bg-background px-2.5 text-xs font-medium text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-40\"\n                >\n                  Discard\n                </button>\n                <motion.button\n                  type=\"button\"\n                  onClick={approve}\n                  disabled={locked}\n                  animate={{ scale: view.pressing ? 0.94 : 1 }}\n                  transition={{ type: \"spring\", stiffness: 600, damping: 22 }}\n                  className=\"relative h-8 shrink-0 cursor-pointer overflow-hidden rounded-md bg-foreground px-3 text-xs font-medium text-background outline-none hover:opacity-90 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-default\"\n                >\n                  {view.status === \"executing\" && (\n                    <span className=\"absolute inset-y-0 left-0 bg-background/25 transition-[width] duration-100 ease-linear\" style={{ width: `${view.progress * 100}%` }} aria-hidden />\n                  )}\n                  <span className=\"relative\">{view.status === \"executing\" ? \"Executing…\" : \"Approve & execute\"}</span>\n                </motion.button>\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport default function StagedActionDemo() {\n  return (\n    <div className=\"flex h-[380px] w-full items-start justify-center pt-2\">\n      <StagedAction autoPlay />\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/nextui/staged-action.tsx"
    }
  ],
  "meta": {
    "kind": "component",
    "category": "ai",
    "previewHeight": 400,
    "new": true,
    "free": true,
    "openSource": true,
    "added": "2026-11-06"
  },
  "categories": [
    "ai"
  ],
  "type": "registry:component"
}