{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-start",
  "title": "Start page",
  "description": "What a session may start with no record: the list of its starts, and one governed action's form without the record gate, confirmed by the registry reference.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/pages-record-route-query",
    "@registrystack/pages-content",
    "@registrystack/pages-record",
    "@registrystack/pages-register-routes",
    "@registrystack/pages-start-places",
    "@registrystack/pages-write-form-core",
    "@registrystack/shell-back-link",
    "@registrystack/shell-empty-state",
    "@registrystack/shell-error-panel",
    "@registrystack/shell-loading",
    "@registrystack/shell-page-heading",
    "@registrystack/ui-button",
    "@registrystack/lib-content"
  ],
  "files": [
    {
      "path": "pages/start-page.tsx",
      "content": "import type {\n  RecordActionReference,\n  RecordTask,\n} from \"@registrystack/app-runtime\";\nimport { useModel, useStarts } from \"@registrystack/app-runtime/react\";\nimport { Button } from \"@/components/ui/button\";\nimport { EmptyState } from \"@/blocks/shell/empty-state\";\nimport { ErrorPanel } from \"@/blocks/shell/error-panel\";\nimport { Loading } from \"@/blocks/shell/loading\";\nimport { BackLink } from \"@/blocks/shell/back-link\";\nimport { PageHeading } from \"@/blocks/shell/page-heading\";\nimport { useLocale } from \"@/blocks/lib/content\";\nimport { createWords, usePagesContent } from \"@/blocks/pages/pages-content\";\nimport {\n  entityRoutes,\n  registerEntities,\n  requestRoute,\n  UnavailableWrite,\n  type RecordTaskState,\n} from \"@/blocks/pages/record\";\nimport { formRoute } from \"@/blocks/pages/record-route-query\";\nimport { useRegisterRoutes } from \"@/blocks/pages/register-routes\";\nimport {\n  startActionPath,\n  startsPath,\n  unlistedStarts,\n} from \"@/blocks/pages/start-places\";\nimport {\n  ActionWriteForm,\n  readableRecordPath,\n  useFormRoute,\n  useOpenedAction,\n} from \"@/blocks/pages/write-form-core\";\n\n/** The write `StartPage` submits: a plain record task, unadapted. */\nexport type StartTask = RecordTaskState<RecordTask>;\n\n/**\n * What the session may start that no record list offers: a governed action\n * whose result it cannot list, a create of a record type it cannot list, and\n * a request create, which opens the request form of `RegisterRoutes`.\n * Without `actionId` it lists them; with it, it is the form of that action,\n * which needs no entity, so a session that holds only the right to invoke an\n * action still has somewhere to start. What is offered comes from the starts\n * the host serves for the session's own profile; the registry decides each call.\n */\nexport function StartPage({\n  actionId,\n  useTask,\n}: {\n  /** The governed action's id, as the route names it. */\n  actionId?: string;\n  /** The write the form submits; a page instantiates it over its own host command. */\n  useTask: () => StartTask;\n}) {\n  const starts = useStarts(),\n    model = useModel(),\n    routes = useRegisterRoutes();\n  if (starts.isPending || model.isPending) return <Loading />;\n  if (!starts.data)\n    return (\n      <ErrorPanel error={starts.error} retry={() => void starts.refetch()} />\n    );\n  if (!model.data)\n    return (\n      <ErrorPanel error={model.error} retry={() => void model.refetch()} />\n    );\n  return actionId === undefined ? (\n    <StartList\n      starts={unlistedStarts(starts.data, model.data.entities, routes)}\n    />\n  ) : (\n    <StartAction actionId={actionId} starts={starts.data} useTask={useTask} />\n  );\n}\n\nfunction StartList({ starts }: { starts: readonly RecordActionReference[] }) {\n  const pages = usePagesContent(),\n    locale = useLocale(),\n    routes = useRegisterRoutes(),\n    model = useModel().data;\n  const entities = model ? registerEntities(model.entities) : null;\n  const seen = new Set<string>();\n  const links = starts.flatMap((start) => {\n    const key = start.name === \"invoke\" ? start.actionId : start.entity;\n    // An action and a create may share an id, so each kind is kept apart.\n    const kept = `${start.name}:${key}`;\n    if (!key || seen.has(kept)) return [];\n    const requested = entities?.requests?.find(\n      (entity) => entity.id === start.entity,\n    );\n    const path =\n      start.name === \"invoke\"\n        ? startActionPath(key)\n        : entities && requested\n          ? routes.createRequest &&\n            requestRoute(entities, routes.createRequest, requested.id)\n          : entities &&\n            entityRoutes(routes, entities, start.entity)?.createRecord;\n    if (!path) return [];\n    seen.add(kept);\n    // A create the registry left unlabelled is worded as the record list\n    // words it, and a request create as a record's page words it.\n    const created = entities?.records?.find(\n      (entity) => entity.id === start.entity,\n    );\n    const label =\n      start.name === \"invoke\"\n        ? (start.label ?? key)\n        : requested\n          ? createWords(\n              pages,\n              \"requestChangeOf\",\n              start.label,\n              requested.label,\n              locale,\n            )\n          : !created\n            ? (start.label ?? key)\n            : createWords(\n                pages,\n                created.id === entities?.record.id\n                  ? \"createRecord\"\n                  : \"addRecord\",\n                start.label,\n                created.label,\n                locale,\n              );\n    // A record create opened from here leads back here, as an action does.\n    const href =\n      start.name === \"create\" && !requested\n        ? formRoute(path, {}, undefined, startsPath)\n        : path;\n    return [{ key: kept, label, href: `#${href}` }];\n  });\n  return (\n    <>\n      <PageHeading\n        title={pages.startsHeading}\n        description={links.length ? pages.startsDescription : undefined}\n      />\n      {links.length === 0 ? (\n        <NothingToStart />\n      ) : (\n        <div className=\"actions\">\n          {links.map((link) => (\n            <Button\n              key={link.key}\n              variant=\"outline\"\n              render={<a href={link.href} />}\n            >\n              {link.label}\n            </Button>\n          ))}\n        </div>\n      )}\n    </>\n  );\n}\n\nfunction StartAction({\n  actionId,\n  starts,\n  useTask,\n}: {\n  actionId: string;\n  starts: readonly RecordActionReference[];\n  useTask: () => StartTask;\n}) {\n  const pages = usePagesContent(),\n    routes = useRegisterRoutes(),\n    model = useModel().data,\n    task = useTask();\n  const action = useOpenedAction(\n    starts.find((item) => item.name === \"invoke\" && item.actionId === actionId),\n  );\n  const fields = (action?.fields ?? []).filter((field) => !field.readOnly);\n  const asked = useFormRoute(fields);\n  if (!action) return <UnavailableWrite />;\n  const entities = model ? registerEntities(model.entities) : null;\n  return (\n    <>\n      <BackLink href={startsPath}>{pages.back}</BackLink>\n      <PageHeading\n        title={action.label ?? actionId}\n        description={pages.actionDescription}\n      />\n      <ActionWriteForm\n        action={action}\n        entities={entities}\n        fields={fields}\n        preset={asked.preset}\n        task={task}\n        submitLabel={pages.actionSubmit}\n        cancelHref={asked.returnPath ?? startsPath}\n        returnPath={asked.returnPath}\n        confirmedPath={(resource) =>\n          entities ? readableRecordPath(routes, entities, resource) : null\n        }\n      />\n    </>\n  );\n}\n\n/**\n * Where the session starts nothing: an account that reads records is pointed\n * at the first record type it reads, and any other is told it holds no access\n * this app uses.\n */\nfunction NothingToStart() {\n  const pages = usePagesContent(),\n    routes = useRegisterRoutes(),\n    model = useModel().data;\n  const entities = model ? registerEntities(model.entities) : null;\n  const readable = entities?.records?.find(\n    ({ operations }) => operations.get || operations.list || operations.lookup,\n  );\n  const place =\n    entities && readable && entityRoutes(routes, entities, readable.id);\n  return (\n    <EmptyState title={pages.startsNothingTitle}>\n      {readable && place ? (\n        <>\n          {pages.startsReadOnlyBody}{\" \"}\n          <a href={`#${place.records}`}>{readable.pluralLabel}</a>.\n        </>\n      ) : (\n        pages.startsNothingBody\n      )}\n    </EmptyState>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/pages/start-page.tsx"
    }
  ],
  "type": "registry:block"
}