{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"undo-window","type":"registry:component","title":"Undo Window","description":"A short recovery window for reversible actions.","dependencies":[],"categories":["systems","recovery","undo","actions"],"files":[{"path":"components/lab/undo-window/undo-window.tsx","target":"components/lab/undo-window/undo-window.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useCallback, useEffect, useRef, useState } from \"react\";\nimport { canRequestUndo } from \"./undo-policy\";\nimport \"./undo-window.css\";\n\nexport type UndoStatus = \"available\" | \"pending\" | \"restored\" | \"failed\" | \"expired\";\nexport type UndoEntry = { id: string; label: string; expiresAt: number; status: UndoStatus };\nexport type UndoWindowProps = {\n  /** A single host-owned action. Replace its id when a new action supersedes it. */\n  entry: UndoEntry | null;\n  /** Request reversal; the host must report success or failure in entry.status. */\n  onUndo: (id: string) => void;\n  /** The deadline passed. The host decides how and when to finalize. */\n  onExpire: (id: string) => void;\n};\n\nexport function UndoWindow({ entry, onUndo, onExpire }: UndoWindowProps) {\n  const [now, setNow] = useState(() => Date.now());\n  const requested = useRef<string | null>(null);\n  const expired = useRef<string | null>(null);\n  const expireRef = useRef(onExpire);\n  const actionRef = useRef<HTMLButtonElement>(null);\n  const statusRef = useRef<HTMLDivElement>(null);\n  useEffect(() => { expireRef.current = onExpire; }, [onExpire]);\n  const id = entry?.id;\n  const deadline = entry?.expiresAt;\n  const status = entry?.status;\n\n  const preserveFocus = useCallback(() => {\n    if (document.activeElement === actionRef.current) statusRef.current?.focus();\n  }, []);\n\n  useEffect(() => {\n    requested.current = null;\n    expired.current = null;\n  }, [id]);\n\n  useEffect(() => {\n    if (!id || status !== \"available\" || deadline === undefined) return;\n    const tick = () => {\n      const current = Date.now();\n      setNow(current);\n      if (current >= deadline && expired.current !== id) {\n        expired.current = id;\n        preserveFocus();\n        expireRef.current(id);\n      }\n    };\n    tick();\n    const timer = window.setInterval(tick, 1000);\n    document.addEventListener(\"visibilitychange\", tick);\n    return () => { window.clearInterval(timer); document.removeEventListener(\"visibilitychange\", tick); };\n  }, [id, status, deadline, preserveFocus]);\n\n  if (!entry) return null;\n  const remaining = Math.max(0, Math.ceil((entry.expiresAt - now) / 1000));\n  const available = canRequestUndo(entry.status, entry.expiresAt, now);\n  const request = () => {\n    const current = Date.now();\n    if (!canRequestUndo(entry.status, entry.expiresAt, current)) {\n      if (entry.status === \"available\" && expired.current !== entry.id) {\n        expired.current = entry.id;\n        preserveFocus();\n        onExpire(entry.id);\n      }\n      return;\n    }\n    if (requested.current === entry.id) return;\n    requested.current = entry.id;\n    preserveFocus();\n    onUndo(entry.id);\n  };\n  const message = entry.status === \"pending\" ? \"Restoring…\" : entry.status === \"restored\" ? \"Restored\" : entry.status === \"failed\" ? \"Could not restore\" : entry.status === \"expired\" || !available ? \"Undo window ended\" : \"Archived\";\n  return (\n    <section className=\"uw-window\" aria-label=\"Recent action\">\n      <div className=\"uw-copy\" ref={statusRef} tabIndex={-1} role=\"status\" aria-live=\"polite\">\n        <strong>{message}</strong><span>{entry.label}</span>\n        {entry.status === \"failed\" ? <span>Try restoring it from the archive.</span> : null}\n      </div>\n      {available ? <div className=\"uw-action\"><span>{remaining}s to undo</span><button ref={actionRef} type=\"button\" onClick={request}>Undo</button></div> : null}\n    </section>\n  );\n}\n"},{"path":"components/lab/undo-window/undo-window.css","target":"components/lab/undo-window/undo-window.css","type":"registry:file","content":".uw-window { display:flex; justify-content:space-between; align-items:center; gap:16px; padding:14px 16px; border:1px solid #dedbe9; border-radius:12px; background:#f6f4ff; color:#24202f; font:inherit; }\n.uw-copy { display:grid; gap:2px; min-width:0; font-size:14px; line-height:1.45; }\n.uw-copy span { color:#555160; overflow-wrap:anywhere; }\n.uw-action { display:flex; align-items:center; gap:12px; white-space:nowrap; color:#555160; font-size:12px; }\n.uw-action button { border:0; border-radius:8px; padding:8px 12px; background:#4f27e0; color:white; font:inherit; font-weight:600; cursor:pointer; }\n.uw-action button:focus-visible { outline:2px solid #4f27e0; outline-offset:3px; }\n@media (max-width:400px) { .uw-window { align-items:flex-start; flex-direction:column; } }\n"},{"path":"components/lab/undo-window/undo-policy.ts","target":"components/lab/undo-window/undo-policy.ts","type":"registry:file","content":"/** The host owns the deadline and the operation. */\nexport function canRequestUndo(status: string, expiresAt: number, now: number): boolean {\n  return status === \"available\" && Number.isFinite(expiresAt) && now < expiresAt;\n}\n"}]}