{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pages-record-action",
  "title": "Record action page",
  "description": "One of the registry's governed actions whose result lands in the register's records, invoked with the inputs the record list offers it with.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/pages-write-form-core",
    "@registrystack/field-value-errors",
    "@registrystack/pages-create-record",
    "@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"
  ],
  "files": [
    {
      "path": "pages/record-action-page.tsx",
      "content": "import { useState, type FormEvent } from \"react\";\nimport {\n  recordPreset,\n  type EntityModel,\n  type RecordActionReference,\n  type RecordTask,\n  type RecordView,\n} from \"@registrystack/app-runtime\";\nimport {\n  formValues,\n  recordTask,\n  useRecord,\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 { Loading } from \"@/blocks/shell/loading\";\nimport { ErrorPanel } from \"@/blocks/shell/error-panel\";\nimport { PageHeading } from \"@/blocks/shell/page-heading\";\nimport { navigate } from \"@/blocks/shell/routing\";\nimport {\n  ActionWriteForm,\n  OfferedStart,\n  readableRecordPath,\n  useFormRoute,\n  useOpenedAction,\n} from \"@/blocks/pages/write-form-core\";\nimport { backToListOf, usePagesContent } from \"@/blocks/pages/pages-content\";\nimport { useRegisterRoutes } from \"@/blocks/pages/register-routes\";\nimport { TaskFeedback } from \"@/blocks/pages/task-feedback\";\nimport {\n  entityRoutes,\n  fieldOf,\n  RecordEntityGate,\n  recordTitle,\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 `RecordActionPage` submits: a plain record task, unadapted. */\nexport type RecordActionTask = RecordTaskState<RecordTask>;\n\n/**\n * One of the registry's governed actions, invoked with the inputs the host\n * offers it with. Without `recordId` it is one whose result lands in the\n * entity's records, offered by the entity's record list; with it, one taken\n * on that record, offered by the record read, and every input that names this\n * record is preset and shown read-only. Its label, its inputs and their words\n * come from the action the host serves, never from an action, entity or field\n * id in this file; the registry checks the inputs and decides what the action\n * writes.\n */\nexport function RecordActionPage({\n  actionId,\n  recordId,\n  entity,\n  useTask,\n}: {\n  /** The governed action's id, as the route names it. */\n  actionId: string;\n  /** The record the action is taken on, for an action offered on a record. */\n  recordId?: string;\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: () => RecordActionTask;\n}) {\n  return (\n    <RecordEntityGate entity={entity}>\n      {(entities, model) =>\n        recordId === undefined ? (\n          <RecordAction\n            entities={entities}\n            entity={model}\n            actionId={actionId}\n            useTask={useTask}\n          />\n        ) : (\n          <RecordScopedAction\n            entities={entities}\n            entity={model}\n            recordId={recordId}\n            actionId={actionId}\n            useTask={useTask}\n          />\n        )\n      }\n    </RecordEntityGate>\n  );\n}\n\nfunction RecordAction({\n  entities,\n  entity,\n  actionId,\n  useTask,\n}: {\n  entities: RegisterEntities;\n  entity: EntityModel;\n  actionId: string;\n  useTask: () => RecordActionTask;\n}) {\n  const task = useTask();\n  return (\n    <OfferedStart\n      entity={entity.id}\n      matches={(item) =>\n        item.name === \"invoke\" &&\n        item.entity === entity.id &&\n        item.actionId === actionId\n      }\n    >\n      {(action) => (\n        <RecordActionForm\n          entities={entities}\n          entity={entity}\n          action={action}\n          task={task}\n        />\n      )}\n    </OfferedStart>\n  );\n}\n\nfunction RecordScopedAction({\n  entities,\n  entity,\n  recordId,\n  actionId,\n  useTask,\n}: {\n  entities: RegisterEntities;\n  entity: EntityModel;\n  recordId: string;\n  actionId: string;\n  useTask: () => RecordActionTask;\n}) {\n  const task = useTask();\n  // The same read as the record page, so the action it offered is the one used.\n  const query = useRecord(entity.id, recordId);\n  const action = useOpenedAction(\n    query.data?.actions.find(\n      (item) => item.name === \"invoke\" && item.actionId === actionId,\n    ),\n  );\n  if (query.isPending) return <Loading />;\n  if (!query.data || query.error)\n    return (\n      <ErrorPanel error={query.error} retry={() => void query.refetch()} />\n    );\n  // An action the registry would refuse for this record now is not opened,\n  // unless this form already wrote: the reread after a confirmed write can\n  // withdraw the action, and the confirmation must outlive that.\n  if (!action || (action.availability && !task.result))\n    return <UnavailableWrite />;\n  return (\n    <RecordActionForm\n      entities={entities}\n      entity={entity}\n      action={action}\n      record={query.data}\n      task={task}\n    />\n  );\n}\n\nfunction RecordActionForm({\n  entities,\n  entity,\n  action,\n  record,\n  task,\n}: {\n  entities: RegisterEntities;\n  entity: EntityModel;\n  action: RecordActionReference;\n  /** The record the action is taken on, when it is one offered on a record. */\n  record?: RecordView;\n  task: RecordActionTask;\n}) {\n  const pages = usePagesContent(),\n    locale = useLocale(),\n    appRoutes = useRegisterRoutes();\n  const routes = entityRoutes(appRoutes, entities, entity.id);\n  const fields = (action.fields ?? []).filter((field) => !field.readOnly);\n  const asked = useFormRoute(fields);\n  const returnPath = asked.returnPath;\n  // What the record gives an input wins over what a link asks for.\n  const preset = {\n    ...asked.preset,\n    ...(record ? recordPreset(entity, record, fields) : {}),\n  };\n  // An input filled from the record the action is taken on reads that record;\n  // a source input the form holds names its own record, even of this type.\n  const anchorSources = Object.fromEntries(\n    record\n      ? fields.flatMap((field) =>\n          field.fillFrom?.entity === entity.id &&\n          !fields.some((item) => item.id === field.fillFrom!.input)\n            ? [[field.fillFrom.input, record.id]]\n            : [],\n        )\n      : [],\n  );\n  if (!routes) return <UnavailableWrite />;\n  // Where the page leaves to: the path a link asked for, else the page it was\n  // opened from, else the record the action is taken on, else the entity's list.\n  const leaveTo =\n    returnPath ??\n    asked.backPath ??\n    (record ? routes.record(record.id) : routes.records);\n  return (\n    <>\n      <BackLink href={leaveTo}>\n        {returnPath || asked.backPath || record\n          ? pages.back\n          : backToListOf(pages, entity, entity.id === entities.record.id, locale)}\n      </BackLink>\n      <PageHeading\n        eyebrow={\n          record ? (recordTitle(entity, record, locale) ?? entity.label) : undefined\n        }\n        title={action.label ?? action.actionId ?? \"\"}\n        description={pages.actionDescription}\n      />\n      <ActionWriteForm\n        action={action}\n        entities={entities}\n        fields={fields}\n        preset={preset}\n        anchorSources={anchorSources}\n        anchorLabel={(field) => {\n          const from = field.fillFrom!;\n          const owner = [\n            entities.record,\n            ...(entities.records ?? []),\n            ...(entities.requests ?? []),\n          ].find((item) => item.id === from.entity);\n          return (owner && fieldOf(owner, from.field)?.label) ?? from.field;\n        }}\n        task={task}\n        submitLabel={pages.actionSubmit}\n        cancelHref={leaveTo}\n        returnPath={returnPath}\n        confirmedPath={(resource) =>\n          readableRecordPath(appRoutes, entities, resource)\n        }\n      />\n    </>\n  );\n}\n",
      "type": "registry:block",
      "target": "@components/pages/record-action-page.tsx"
    }
  ],
  "type": "registry:block"
}