{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "provenance-chip",
  "type": "zoblocks:component",
  "title": "Provenance Chip",
  "description": "A 20px affix beside a value, never competing with it. Six source classes with CSS glyphs rather than colours, staleness folded in against a per-datum policy, and — for an AI-extracted value — the model, the source span and whether anybody has confirmed it.",
  "categories": [
    "clinical",
    "ai"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "provenance-core"
  ],
  "files": [
    {
      "path": "registry/zoblocks/provenance-chip/provenance-chip.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/provenance-chip.tsx",
      "content": "/**\n * ProvenanceChip — where a value came from, and how much of it a human has\n * actually looked at.\n *\n *     <ProvenanceChip record={provenance} now={serverTime} />\n *     <ProvenanceChip resourceId=\"obs-1\" ledger={ledger} now={serverTime} />\n *\n * An affix, never a headline. It sits beside a value and does not compete with\n * it, because the value is what the reader came for and the provenance is what\n * decides whether they can act on it.\n *\n * Six source classes, each with a CSS glyph rather than a colour — colour\n * would put them on a scale from better to worse, and they are not one. A\n * patient-reported PHQ-9 is the correct provenance for a PHQ-9; the same class\n * standing in for a blood-pressure measurement is not, and that judgement\n * belongs to the caller.\n *\n * Styling lives in `styles/zoblocks-provenance.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport {\n  SOURCE_GLYPH,\n  SOURCE_LABEL,\n  describeAgeShort,\n  describeProvenance,\n  staleness,\n  type ProvenanceLedger,\n  type ProvenanceRecord,\n  type StalenessPolicy,\n} from \"@/lib/zoblocks-provenance\";\n\nexport {\n  SOURCE_GLYPH,\n  SOURCE_LABEL,\n  describeAgeShort,\n  describeProvenance,\n  fromProvenance,\n  ledgerFrom,\n  staleness,\n  toSource,\n  type ProvenanceAgent,\n  type ProvenanceLedger,\n  type ProvenanceRecord,\n  type ProvenanceSource,\n  type Staleness,\n  type StalenessPolicy,\n} from \"@/lib/zoblocks-provenance\";\n\ntype Base = Omit<React.HTMLAttributes<HTMLElement>, \"children\">;\n\nexport interface ProvenanceChipProps extends Base {\n  /** The record, or a ledger lookup below. One of the two is required. */\n  record?: ProvenanceRecord;\n  /**\n   * Read from a ledger instead.\n   *\n   * Components never fetch their own provenance: a chart with two hundred\n   * values would make two hundred requests and every chip would settle at a\n   * different moment. The host subscribes once and passes this down.\n   */\n  ledger?: ProvenanceLedger;\n  /**\n   * The resource this provenance is about. Used to link back to the record rather than to\n   * re-fetch it.\n   */\n  resourceId?: string;\n  /**\n   * Which version was seen. Provenance for a value that has since changed is provenance for a\n   * different value.\n   */\n  versionId?: string;\n\n  /** ISO 8601, supplied by the host. No relative age is shown without it. */\n  now?: string;\n  /** How old is too old, per kind of datum. Four days differs by datum. */\n  stalenessPolicy?: StalenessPolicy;\n\n  /** Hides the source word, leaving the glyph and the age. For dense grids. */\n  glyphOnly?: boolean;\n  /** Opens the chain: the agents, the device, the document, the versions. */\n  onOpenChain?: (record: ProvenanceRecord) => void;\n  /**\n   * Jumps to the span an extracted value was lifted from.\n   *\n   * The single most requested behaviour from clinicians reviewing extraction,\n   * which is why it is its own handler rather than something folded into the\n   * chain.\n   */\n  onOpenSpan?: (span: NonNullable<ProvenanceRecord[\"span\"]>, record: ProvenanceRecord) => void;\n}\n\nexport const ProvenanceChip = React.forwardRef<HTMLElement, ProvenanceChipProps>(\n  function ProvenanceChip(\n    {\n      record: given,\n      ledger,\n      resourceId,\n      versionId,\n      now,\n      stalenessPolicy,\n      glyphOnly = false,\n      onOpenChain,\n      onOpenSpan,\n      className,\n      ...rest\n    },\n    ref,\n  ) {\n    const record = given ?? (resourceId ? ledger?.get(resourceId, versionId) : undefined);\n\n    // Nothing rather than a chip claiming an unknown origin. A chip that says\n    // \"unknown\" on every value it has no ledger entry for teaches readers to\n    // ignore the whole column.\n    if (!record) return null;\n\n    const age = now ? staleness(record, now, stalenessPolicy) : { state: \"unknown\" as const };\n    // The age is a fact and the staleness judgement is an opinion, so the age\n    // shows whenever there is a date to measure from and only the highlight\n    // waits for a policy.\n    const at = record.observedAt ?? record.recordedAt;\n    const elapsedMs = now && at ? Date.parse(now) - Date.parse(at) : Number.NaN;\n    const showAge = Number.isFinite(elapsedMs) && elapsedMs >= 0;\n    const label = describeProvenance(record, now, stalenessPolicy);\n    const extracted = record.source === \"ai-extracted\";\n    const unconfirmed = extracted && !record.confirmed;\n\n    const shared = {\n      className: cn(\"zb-prov\", className),\n      \"data-zb-provenance\": \"\",\n      \"data-zb-source\": record.source,\n      \"data-zb-staleness\": age.state,\n      \"data-zb-unconfirmed\": unconfirmed ? \"\" : undefined,\n      \"aria-label\": label,\n      ...rest,\n    };\n\n    const body = (\n      <>\n        <span\n          className=\"zb-prov__glyph\"\n          data-zb-glyph={SOURCE_GLYPH[record.source]}\n          aria-hidden=\"true\"\n        />\n        {glyphOnly ? null : (\n          <span className=\"zb-prov__word\" aria-hidden=\"true\">\n            {SOURCE_LABEL[record.source]}\n          </span>\n        )}\n        {/*\n          Staleness is folded in rather than kept in the chain. A device\n          reading from four days ago and one from four minutes ago carry\n          different weight, and the reader has to see that without hovering.\n        */}\n        {showAge ? (\n          <span className=\"zb-prov__age\" data-zb-staleness={age.state} aria-hidden=\"true\">\n            {describeAgeShort(elapsedMs)}\n          </span>\n        ) : null}\n        {/*\n          The confirmation state, for extracted values only.\n          Absent means the question does not apply; a dot means nobody has\n          looked, which is a different thing from a low-confidence score.\n        */}\n        {unconfirmed ? (\n          <span className=\"zb-prov__unconfirmed\" aria-hidden=\"true\">\n            unreviewed\n          </span>\n        ) : null}\n      </>\n    );\n\n    const chip = onOpenChain ? (\n      <button\n        {...(shared as React.ButtonHTMLAttributes<HTMLButtonElement>)}\n        ref={ref as React.Ref<HTMLButtonElement>}\n        type=\"button\"\n        onClick={() => onOpenChain(record)}\n      >\n        {body}\n      </button>\n    ) : (\n      // A span when there is nothing to open. A focusable element with no\n      // action is worse here than anywhere: there is one beside every value.\n      <span\n        {...(shared as React.HTMLAttributes<HTMLSpanElement>)}\n        ref={ref as React.Ref<HTMLSpanElement>}\n        role=\"img\"\n      >\n        {body}\n      </span>\n    );\n\n    if (extracted && record.span && onOpenSpan) {\n      const span = record.span;\n      return (\n        <span className=\"zb-prov__pair\">\n          {chip}\n          <button\n            type=\"button\"\n            className=\"zb-prov__span\"\n            onClick={() => onOpenSpan(span, record)}\n            aria-label={`View the source span: ${span.document}${\n              typeof span.page === \"number\" ? `, page ${span.page}` : \"\"\n            }`}\n          >\n            View span\n          </button>\n        </span>\n      );\n    }\n\n    return chip;\n  },\n);\n"
    }
  ]
}
