{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "lib-tab-memory",
  "title": "Tab-scoped cursor memory",
  "description": "The cursor stack behind a paged list's Previous button, held in this browser tab so a route remount never loses it.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/lib-queue-parts"
  ],
  "files": [
    {
      "path": "lib/tab-memory.ts",
      "content": "import { useRef } from \"react\";\nimport { useRememberedState } from \"@registrystack/app-runtime/react\";\nimport type { CursorEntry } from \"@/blocks/lib/queue-parts\";\n\n/**\n * Tab memory for a paged list, not persistence. The app remounts a page on\n * every route change (the router keys the page element by the full hash), so\n * the cursor stack behind Previous cannot live in component state. It is\n * in-memory by design: a reload or a pasted link lands on its page with\n * nothing before it, so Previous is disabled until Next stacks an earlier page\n * in this tab.\n *\n * Each list owns its own stack: two lists are two result sets, and a cursor\n * from one is never offered to the other.\n */\nexport interface TabCursors {\n  /** The key the stack is remembered under in this tab's memory. */\n  key: string;\n}\n\n/** The memory one list keeps for this tab, held at module scope by its page. */\nexport function tabCursors(key: string): TabCursors {\n  return { key };\n}\n\nconst noEntries: CursorEntry[] = [];\n\n/**\n * The page before the one being read, and the three moves that change it.\n *\n * The stack is reconciled against the URL on an instance's first render: a URL\n * the page did not write (Back, Forward, a cleared filter) always arrives as a\n * fresh mount with a stack pointing elsewhere, so it collapses to the landed\n * page and Previous can never mix cursors across result sets. Later renders of\n * the same instance are stale views during an in-flight navigation and must\n * not touch the stack this page's own clicks maintain.\n */\nexport function useTabCursors(\n  memory: TabCursors,\n  view: { cursor?: string | undefined; page: number },\n) {\n  const [remembered, setEntries] = useRememberedState<CursorEntry[]>(\n    memory.key,\n    noEntries,\n  );\n  let entries = remembered;\n  const reconciled = useRef(false);\n  if (!reconciled.current) {\n    reconciled.current = true;\n    const top = entries.at(-1);\n    if (!top || top.cursor !== view.cursor || top.page !== view.page) {\n      entries = [{ cursor: view.cursor, page: view.page }];\n      setEntries(entries);\n    }\n  }\n  return {\n    /** The entry Previous returns to, and nothing on the first page in this tab. */\n    previous: entries.at(-2),\n    /** Stacks the page a Next click leaves behind. */\n    next(cursor: string, page: number) {\n      setEntries((entries) => [...entries, { cursor, page }]);\n    },\n    /** Drops the page a Previous click leaves. */\n    back() {\n      setEntries((entries) => entries.slice(0, -1));\n    },\n    /** Collapses the stack to a first page, after a view change or a recovery. */\n    restart() {\n      setEntries([{ cursor: undefined, page: 1 }]);\n    },\n  };\n}\n",
      "type": "registry:lib",
      "target": "@lib/tab-memory.ts"
    }
  ],
  "type": "registry:lib"
}