{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "request-verified-prefill",
  "title": "Verified prefill",
  "description": "Walks the consent, request and accept steps of asking a verified source for one field's answer, and a read-only summary of a field's verified evidence once a request is submitted.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/ui-button",
    "@registrystack/field-renderers",
    "@registrystack/lib-content",
    "@registrystack/lib-format"
  ],
  "files": [
    {
      "path": "request/verified-prefill.tsx",
      "content": "import type { PrefillFailure, VerifiedEvidenceDisplay, VerifiedPrefill } from \"@registrystack/app-runtime\";\nimport type { ReactNode } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { useBlockContent, useLocale } from \"@/blocks/lib/content\";\nimport { InPhrase } from \"@/blocks/fields/values\";\nimport { fill, fillNodes, longDate } from \"@/blocks/lib/format\";\n\n/**\n * The consent, request and accept steps of asking a verified source for one\n * field's answer. `heading`, `disclosure` and `requestLabel` are the\n * caller's own, since only it knows which field this requests and from\n * which source. `accepted`/`draftEvidence` are shown only when the caller\n * decides the field they answer is still part of this form.\n */\nexport function VerifiedPrefillSection({\n  heading,\n  disclosure,\n  requestLabel,\n  prefill,\n  onAccept,\n  accepted,\n  acceptedDiverged,\n  draftEvidence,\n  draftDiverged,\n}: {\n  heading: string;\n  disclosure: string;\n  requestLabel: string;\n  prefill: {\n    consented: boolean;\n    busy: boolean;\n    consent: () => void;\n    request: () => Promise<unknown>;\n    proposal: VerifiedPrefill | null;\n    error: PrefillFailure | null;\n  };\n  onAccept: (proposal: VerifiedPrefill) => void;\n  accepted: VerifiedPrefill | null;\n  acceptedDiverged: boolean;\n  draftEvidence?: VerifiedEvidenceDisplay;\n  draftDiverged: boolean;\n}) {\n  const c = useBlockContent();\n  const locale = useLocale();\n  return (\n    <section aria-label={heading} className=\"card\">\n      <h2>{heading}</h2>\n      <p>{disclosure}</p>\n      {!prefill.consented && (\n        <Button\n          variant=\"outline\"\n          type=\"button\"\n          disabled={prefill.busy}\n          onClick={prefill.consent}\n        >\n          {heading}\n        </Button>\n      )}\n      {prefill.consented && (\n        <Button\n          type=\"button\"\n          disabled={prefill.busy}\n          onClick={() => void prefill.request()}\n        >\n          {requestLabel}\n        </Button>\n      )}\n      {prefill.busy && <p role=\"status\">{c.prefillLoading}</p>}\n      {prefill.proposal && (\n        <div role=\"status\">\n          <p>\n            {fill(\n              c.prefillAnswerLine,\n              {\n                label: c.prefillProposed,\n                answer: prefill.proposal.value ? c.yes : c.no,\n                source: prefill.proposal.sourceLabel,\n                date: verifiedOn(prefill.proposal.verifiedAt, locale),\n              },\n              locale,\n            )}\n          </p>\n          <Button type=\"button\" onClick={() => onAccept(prefill.proposal!)}>\n            {c.acceptPrefill}\n          </Button>\n        </div>\n      )}\n      {prefill.error && (\n        <p role=\"alert\">\n          {prefill.error === \"uncertain\"\n            ? c.prefillUncertain\n            : prefill.error === \"expired\"\n              ? c.prefillExpired\n              : prefill.error === \"denied\"\n                ? c.prefillDenied\n                : c.prefillUnavailable}\n        </p>\n      )}\n      {accepted && (\n        <p role=\"status\">\n          {fill(\n            c.prefillAnswerLine,\n            {\n              label: c.prefillVerified,\n              answer: accepted.value ? c.yes : c.no,\n              source: accepted.sourceLabel,\n              date: verifiedOn(accepted.verifiedAt, locale),\n            },\n            locale,\n          )}{\" \"}\n          {acceptedDiverged ? c.prefillChanged : \"\"}\n        </p>\n      )}\n      {draftEvidence && (\n        <p role=\"status\">\n          {draftEvidence.status === \"verified\"\n            ? `${fill(\n                c.prefillDraftLine,\n                {\n                  label: c.prefillVerified,\n                  answer: draftEvidence.sourceValue ? c.yes : c.no,\n                  date: verifiedOn(draftEvidence.verifiedAt, locale),\n                },\n                locale,\n              )} ${draftDiverged ? c.prefillChanged : \"\"}`\n            : c.prefillInvalid}\n        </p>\n      )}\n    </section>\n  );\n}\n\n/**\n * The read-only summary of a field's verified evidence, shown once a request\n * is submitted. `changedValue` is passed only when the caller has both a\n * value newer than the verification and the field to show it against. It\n * shows within a line of text, so a value there renders as text alone.\n */\nexport function VerifiedEvidenceSummary({\n  evidence,\n  changedValue,\n}: {\n  evidence: VerifiedEvidenceDisplay;\n  changedValue?: ReactNode;\n}) {\n  const c = useBlockContent();\n  const locale = useLocale();\n  return (\n    <div className=\"card\" role=\"status\">\n      {evidence.status === \"verified\" ? (\n        <>\n          <h3>{c.prefillVerified}</h3>\n          <p>\n            {fill(\n              c.prefillSummaryLine,\n              {\n                answer: evidence.sourceValue ? c.yes : c.no,\n                source: evidence.sourceLabel,\n                date: verifiedOn(evidence.verifiedAt, locale),\n              },\n              locale,\n            )}\n          </p>\n          {changedValue !== undefined && (\n            <p>\n              {fillNodes(\n                c.labelledSentence,\n                {\n                  label: c.prefillChanged,\n                  value: <InPhrase>{changedValue}</InPhrase>,\n                },\n                locale,\n              )}\n            </p>\n          )}\n        </>\n      ) : (\n        <p>{c.prefillInvalid}</p>\n      )}\n    </div>\n  );\n}\n\n/** When an answer was verified, as a long date; the value as given when it is not a date. */\nfunction verifiedOn(value: string, locale: string): string {\n  return longDate(value, locale) ?? value;\n}\n",
      "type": "registry:block",
      "target": "@components/request/verified-prefill.tsx"
    }
  ],
  "type": "registry:block"
}