{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-record-route-query",
  "title": "Record route query",
  "description": "What a record page carries in its hash route's query: a list's filters and search, the answers a form opens with, and the path to return to once a write is confirmed.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "files": [
    {
      "path": "pages/record-route-query.ts",
      "content": "import type { JsonValue } from \"@registrystack/app-runtime\";\n\n/**\n * What a record page carries in its hash route's query: a list's filters and\n * search, the answers a form opens with, the path to return to once a write\n * is confirmed, and the page a form was opened from. The route is the only\n * source of truth for all of them, so a link to a filtered list or a preset\n * form is a plain `href`.\n */\n\nconst FILTER = \"f.\";\nconst EMPTY = \"empty.\";\nconst SEARCH = \"q\";\nconst PRESET = \"preset.\";\nconst RETURN = \"return\";\nconst BACK = \"back\";\n\n/** A list narrowed by field and by search; a null filter matches a record where the field is empty. */\nexport interface ListQuery {\n  filters: Record<string, string | null>;\n  search: string;\n}\n\n/** The list a route's query asks for. */\nexport function listQueryOf(query: URLSearchParams): ListQuery {\n  const filters: Record<string, string | null> = {};\n  for (const [key, value] of query) {\n    if (key.startsWith(FILTER)) filters[key.slice(FILTER.length)] = value;\n    else if (key.startsWith(EMPTY) && value === \"1\")\n      filters[key.slice(EMPTY.length)] = null;\n  }\n  return { filters, search: query.get(SEARCH) ?? \"\" };\n}\n\n/** `path` with the query that asks for this list; `path` alone when it asks for nothing. */\nexport function listRoute(\n  path: string,\n  { filters, search }: Partial<ListQuery>,\n): string {\n  const query = new URLSearchParams();\n  for (const [field, value] of Object.entries(filters ?? {}))\n    if (value === null) query.set(`${EMPTY}${field}`, \"1\");\n    else query.set(`${FILTER}${field}`, value);\n  if (search) query.set(SEARCH, search);\n  const text = query.toString();\n  return text ? `${path}?${text}` : path;\n}\n\n/** The answers a route's query presets, by field id. */\nexport function presetOf(query: URLSearchParams): Record<string, JsonValue> {\n  const preset: Record<string, JsonValue> = {};\n  for (const [key, value] of query)\n    if (key.startsWith(PRESET)) preset[key.slice(PRESET.length)] = value;\n  return preset;\n}\n\n/**\n * The in-app path a route asks to return to, or null. Only a path starting\n * with a single `/` is one: anything else could leave the app.\n */\nexport function returnPathOf(query: URLSearchParams): string | null {\n  return inAppPath(query.get(RETURN));\n}\n\n/**\n * The in-app page a route says the form was opened from, or null. Its back\n * link and cancel lead there, but a confirmed write still opens the record\n * it wrote.\n */\nexport function backPathOf(query: URLSearchParams): string | null {\n  return inAppPath(query.get(BACK));\n}\n\nfunction inAppPath(path: string | null): string | null {\n  return path && /^\\/(?![/\\\\])/.test(path) ? path : null;\n}\n\n/**\n * `path` with a preset, a return path and the page it is opened from in its\n * query, after any query `path` already carries.\n */\nexport function formRoute(\n  path: string,\n  preset: Readonly<Record<string, string>>,\n  returnPath: string | undefined,\n  from?: string,\n): string {\n  const at = path.indexOf(\"?\");\n  const base = at < 0 ? path : path.slice(0, at);\n  const query = new URLSearchParams(at < 0 ? \"\" : path.slice(at + 1));\n  for (const [field, value] of Object.entries(preset))\n    query.set(`${PRESET}${field}`, value);\n  if (returnPath) query.set(RETURN, returnPath);\n  if (from) query.set(BACK, from);\n  const text = query.toString();\n  return text ? `${base}?${text}` : base;\n}\n",
      "type": "registry:block",
      "target": "@components/pages/record-route-query.ts"
    }
  ],
  "type": "registry:block"
}