{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-create-record",
  "title": "Create record page",
  "description": "A register record, created with the create action the record list offers this session.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/pages-write-form-core",
    "@registrystack/field-value-errors",
    "@registrystack/pages-record",
    "@registrystack/pages-content",
    "@registrystack/pages-register-routes",
    "@registrystack/pages-task-feedback",
    "@registrystack/lib-content",
    "@registrystack/record-form",
    "@registrystack/shell-back-link",
    "@registrystack/shell-loading",
    "@registrystack/shell-error-panel",
    "@registrystack/shell-page-heading",
    "@registrystack/shell-routing",
    "@registrystack/ui-button",
    "@registrystack/lib-format",
    "@registrystack/pages-record-route-query"
  ],
  "files": [
    {
      "path": "pages/create-record-page.tsx",
      "content": "import { useRef, useState, type FormEvent } from \"react\";\nimport type {\n  EntityModel,\n  FieldModel,\n  JsonValue,\n  RecordActionReference,\n  RecordTask,\n} from \"@registrystack/app-runtime\";\nimport {\n  formValues,\n  recordTask,\n} from \"@registrystack/app-runtime/react\";\nimport { Button } from \"@/components/ui/button\";\nimport { useBlockContent, useLocale } from \"@/blocks/lib/content\";\nimport {\n  ErrorSummary,\n  RecordFormFields,\n  soleChoice,\n} from \"@/blocks/record/record-form\";\nimport { BackLink } from \"@/blocks/shell/back-link\";\nimport { PageHeading } from \"@/blocks/shell/page-heading\";\nimport {\n  backToListOf,\n  createWords,\n  usePagesContent,\n} from \"@/blocks/pages/pages-content\";\nimport { presetOf, returnPathOf } from \"@/blocks/pages/record-route-query\";\nimport {\n  ActionWriteForm,\n  OfferedStart,\n  readableRecordPath,\n  useFormRoute,\n} from \"@/blocks/pages/write-form-core\";\nimport { useRegisterRoutes } from \"@/blocks/pages/register-routes\";\nimport { TaskFeedback } from \"@/blocks/pages/task-feedback\";\nimport {\n  entityRoutes,\n  RecordEntityGate,\n  useOptionLabel,\n  UnavailableWrite,\n  type RecordTaskState,\n  type RegisterEntities,\n} from \"@/blocks/pages/record\";\nimport { valueErrors } from \"@/blocks/fields/value-errors\";\n\n/** The write `CreateRecordPage` submits: a plain record task, unadapted. */\nexport type CreateRecordTask = RecordTaskState<RecordTask>;\n\n/**\n * A record of one record entity, the register's own when `entity` is left\n * out, created with the create action the entity's record list offers this\n * session. The fields, their words and the schema they are checked against\n * come from the model and the action, never from an entity or field id in\n * this file. The route's query may preset answers, which show read-only and\n * are sent with the submission, and name the path to return to afterwards.\n */\nexport function CreateRecordPage({\n  entity,\n  useTask,\n}: {\n  /** The entity's id; the register's own record entity when left out. */\n  entity?: string;\n  /** The write this page submits; a page instantiates it over its own host command. */\n  useTask: () => CreateRecordTask;\n}) {\n  return (\n    <RecordEntityGate entity={entity}>\n      {(entities, model) => (\n        <CreateRecord entities={entities} entity={model} useTask={useTask} />\n      )}\n    </RecordEntityGate>\n  );\n}\n\nfunction CreateRecord({\n  entities,\n  entity,\n  useTask,\n}: {\n  entities: RegisterEntities;\n  entity: EntityModel;\n  useTask: () => CreateRecordTask;\n}) {\n  return (\n    <OfferedStart\n      entity={entity.id}\n      matches={(item) => item.name === \"create\" && item.entity === entity.id}\n    >\n      {(action) => (\n        <CreateRecordForm\n          entities={entities}\n          entity={entity}\n          action={action}\n          useTask={useTask}\n        />\n      )}\n    </OfferedStart>\n  );\n}\n\nfunction CreateRecordForm({\n  entities,\n  entity,\n  action,\n  useTask,\n}: {\n  entities: RegisterEntities;\n  entity: EntityModel;\n  action: RecordActionReference;\n  useTask: () => CreateRecordTask;\n}) {\n  const pages = usePagesContent(),\n    locale = useLocale(),\n    appRoutes = useRegisterRoutes(),\n    task = useTask();\n  const routes = entityRoutes(appRoutes, entities, entity.id);\n  const fields = (action.fields ?? []).filter((field) => !field.readOnly);\n  const { preset, returnPath, backPath } = useFormRoute(fields);\n  const leaveTo = returnPath ?? backPath;\n  const ownEntity = entity.id === entities.record.id;\n  if (!routes) return <UnavailableWrite />;\n  return (\n    <>\n      <BackLink href={leaveTo ?? routes.records}>\n        {leaveTo\n          ? pages.back\n          : backToListOf(pages, entity, ownEntity, locale)}\n      </BackLink>\n      <PageHeading\n        title={\n          ownEntity\n            ? createWords(\n                pages,\n                \"createTitle\",\n                action.label,\n                entity.label,\n                locale,\n              )\n            : createWords(\n                pages,\n                \"addRecord\",\n                action.label,\n                entity.label,\n                locale,\n              )\n        }\n        description={createWords(\n          pages,\n          \"createDescription\",\n          entity.createDescription,\n          entity.label,\n          locale,\n        )}\n      />\n      <ActionWriteForm\n        action={action}\n        entities={entities}\n        fields={fields}\n        preset={preset}\n        task={task}\n        submitLabel={createWords(\n          pages,\n          \"createSubmit\",\n          action.label,\n          entity.label,\n          locale,\n        )}\n        cancelHref={leaveTo ?? (ownEntity ? \"/\" : routes.records)}\n        returnPath={returnPath}\n        confirmedPath={(resource) =>\n          readableRecordPath(appRoutes, entities, resource)\n        }\n      />\n    </>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/pages/create-record-page.tsx"
    }
  ],
  "type": "registry:block"
}