{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "request-holder-steps",
  "title": "Holder step indicator and routes",
  "description": "Where a holder is in a change, as a position in words and a list of steps, and the paths and matcher of the holder's steps that follow the register's own request paths. The indicator is not a set of links; each step has its own buttons.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/pages-content",
    "@registrystack/pages-record",
    "@registrystack/pages-register-routes",
    "@registrystack/lib-format",
    "@registrystack/lib-content"
  ],
  "files": [
    {
      "path": "request/holder-steps.tsx",
      "content": "import type { EntityModel } from \"@registrystack/app-runtime\";\nimport { useLocale } from \"@/blocks/lib/content\";\nimport { fill } from \"@/blocks/lib/format\";\nimport { usePagesContent } from \"@/blocks/pages/pages-content\";\n\n/** The places in a holder's change, in the order they come. */\nexport type HolderStep = \"record\" | \"change\" | \"answers\" | \"documents\" | \"review\";\n\n/** Whether any of these requests takes supporting documents, so the documents step is part of the change. */\nexport function takesDocuments(requests: readonly EntityModel[]): boolean {\n  return requests.some((request) => request.operations.attachments?.length);\n}\n\n/**\n * Where a holder is in a change: the step's position in words, and the steps\n * as a list with the current one marked. The steps are not links; a reload\n * of any step re-reads the draft from the registry, so a holder moves with\n * each step's own buttons.\n */\nexport function HolderSteps({\n  current,\n  documents,\n}: {\n  current: HolderStep;\n  /** Whether the request takes documents; a request that takes none skips that step. */\n  documents: boolean;\n}) {\n  const pages = usePagesContent();\n  const locale = useLocale();\n  const steps: { id: HolderStep; label: string }[] = [\n    { id: \"record\", label: pages.holderStepRecord },\n    { id: \"change\", label: pages.holderStepChange },\n    { id: \"answers\", label: pages.holderStepAnswers },\n    ...(documents\n      ? [{ id: \"documents\" as const, label: pages.holderStepDocuments }]\n      : []),\n    { id: \"review\", label: pages.holderStepReview },\n  ];\n  const index = steps.findIndex((step) => step.id === current);\n  return (\n    <div className=\"mb-4\">\n      <p className=\"eyebrow\">\n        {fill(\n          pages.holderStepPosition,\n          {\n            current: index + 1,\n            total: steps.length,\n          },\n          locale,\n        )}\n      </p>\n      <ol\n        aria-label={pages.holderStepsLabel}\n        className=\"flex flex-wrap gap-x-5 gap-y-1 text-sm\"\n      >\n        {steps.map((step, position) => (\n          <li\n            key={step.id}\n            aria-current={position === index ? \"step\" : undefined}\n            className={position === index ? \"font-semibold\" : \"muted\"}\n          >\n            {step.label}\n          </li>\n        ))}\n      </ol>\n    </div>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/request/holder-steps.tsx"
    },
    {
      "path": "pages/holder-step-routes.ts",
      "content": "import { requestRoute, type RegisterEntities } from \"@/blocks/pages/record\";\nimport { decodeSegment, type RegisterRoutes } from \"@/blocks/pages/register-routes\";\n\n/** The step that lists the records a holder may ask to change. */\nexport const chooseRecordRoute = \"/change\";\n\n/** The step that lists what can be changed on one record. */\nexport function chooseChangeRoute(recordId: string): string {\n  return `${chooseRecordRoute}/${encodeURIComponent(recordId)}`;\n}\n\n/** The step that takes a request's supporting documents. */\nexport function documentsRoute(\n  routes: RegisterRoutes,\n  entities: RegisterEntities,\n  request: string,\n  id: string,\n): string {\n  return requestRoute(entities, `${routes.request(id)}/documents`, request);\n}\n\n/** The step that shows a saved draft for a last check, its submission and its discard. */\nexport function reviewRoute(\n  routes: RegisterRoutes,\n  entities: RegisterEntities,\n  request: string,\n  id: string,\n): string {\n  return requestRoute(entities, `${routes.request(id)}/review`, request);\n}\n\n/** Which holder step a path stands for, with the id it carries. */\nexport type HolderStepMatch =\n  | { kind: \"chooseRecord\" }\n  | { kind: \"chooseChange\"; id: string }\n  | { kind: \"documents\"; id: string }\n  | { kind: \"review\"; id: string };\n\n// An id no real one equals, so the segment it lands on is always the id's own.\nconst SENTINEL = \"\\u0000holder-step-sentinel\\u0000\";\n\nfunction segmentsOf(path: string): string[] {\n  return path.split(\"/\").filter(Boolean);\n}\n\n/**\n * Which holder step a path stands for. The request steps follow the pattern\n * `routes.request` gives, so an app that shapes its own request paths keeps\n * its steps beside them.\n */\nexport function matchHolderStep(\n  routes: RegisterRoutes,\n  path: string,\n): HolderStepMatch | null {\n  const segments = segmentsOf(path);\n  const [head, ...rest] = segments;\n  if (head === segmentsOf(chooseRecordRoute)[0]) {\n    if (rest.length === 0) return { kind: \"chooseRecord\" };\n    if (rest.length !== 1) return null;\n    const id = decodeSegment(rest[0]!);\n    return id === null ? null : { kind: \"chooseChange\", id };\n  }\n  const pattern = segmentsOf(routes.request(SENTINEL));\n  const step = segments.at(-1);\n  if (\n    segments.length !== pattern.length + 1 ||\n    (step !== \"documents\" && step !== \"review\")\n  )\n    return null;\n  let id: string | undefined;\n  for (const [index, segment] of pattern.entries()) {\n    if (segment === encodeURIComponent(SENTINEL)) {\n      const decoded = decodeSegment(segments[index]!);\n      if (decoded === null) return null;\n      id = decoded;\n    } else if (segment !== segments[index]) return null;\n  }\n  return id === undefined ? null : { kind: step, id };\n}\n",
      "type": "registry:block",
      "target": "@components/pages/holder-step-routes.ts"
    }
  ],
  "type": "registry:block"
}