{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "risk-indicator",
  "type": "zoblocks:component",
  "title": "Risk Indicator",
  "description": "The band leads and the numeral is demoted. Staleness is on the face rather than in a tooltip, an expired score offers recompute or acknowledge and no third option, and the not-a-diagnosis framing is a required prop rather than a convention.",
  "categories": [
    "clinical",
    "ai"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "registryDependencies": [
    "utils",
    "tokens",
    "risk-core",
    "clinical-status"
  ],
  "files": [
    {
      "path": "registry/zoblocks/risk-indicator/risk-indicator.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/risk-indicator.tsx",
      "content": "/**\n * RiskIndicator — a risk score that cannot be displayed without its date, its\n * drivers, and the fact that it is not a diagnosis.\n *\n *     <RiskIndicator\n *       assessment={readmission}\n *       now={serverTime}\n *       notADiagnosis=\"A statistical estimate, not a diagnosis.\"\n *     />\n *\n * The band leads and the numeral is demoted: two decimal places imply a\n * precision the model does not have. Staleness is on the face rather than in a\n * tooltip, because a score computed nightly and read at noon — after the\n * admission that would have changed it — is the failure nobody sees.\n *\n * `notADiagnosis` is a required prop rather than a convention. Every product\n * that made it optional shipped without it.\n *\n * Styling lives in `styles/zoblocks-risk.css`, installed alongside.\n */\n\nimport * as React from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { ClinicalStatus } from \"@/components/zoblocks/clinical-status\";\nimport {\n  BAND_STEP,\n  concentration,\n  describeAge,\n  describeRisk,\n  driverDirection,\n  freshness,\n  ordinal,\n  topDrivers,\n  type RiskAssessment,\n  type RiskDriver,\n} from \"@/lib/zoblocks-risk\";\n\nexport {\n  BAND_LABEL,\n  BAND_STEP,\n  concentration,\n  describeAge,\n  describeRisk,\n  driverDirection,\n  freshness,\n  fromRiskAssessment,\n  ordinal,\n  toBand,\n  topDrivers,\n  type Freshness,\n  type RiskAssessment,\n  type RiskAssessmentInput,\n  type RiskBand,\n  type RiskDriver,\n} from \"@/lib/zoblocks-risk\";\n\nexport interface RiskIndicatorProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  /**\n   * The RiskAssessment, with its drivers and the date it was computed. A score cannot render\n   * without them — it is a statistical estimate, not a diagnosis, and undated it is not even\n   * that.\n   */\n  assessment: RiskAssessment;\n  /** ISO 8601, supplied by the host. The component never reads a clock. */\n  now: string;\n  /**\n   * The framing, and it is required.\n   *\n   * Not a default string either: what a score is not depends on what it is —\n   * a readmission model and a suicide-risk model need different sentences, and\n   * a shared one would be wrong for both.\n   */\n  notADiagnosis: string;\n  /** How many drivers to show on the face. The rest live in the explanation. */\n  driverCount?: number;\n  /** Row height and type scale. Inherited from the nearest density provider when omitted. */\n  density?: \"compact\" | \"default\";\n  /** Opens the model card. See the DSI source-attribute record. */\n  onOpenModel?: (assessment: RiskAssessment) => void;\n  /**\n   * Acknowledges an expired score.\n   *\n   * A stale score cannot be dismissed — only recomputed or acknowledged — and\n   * the acknowledgement is the host's to record.\n   */\n  onAcknowledge?: (assessment: RiskAssessment) => void;\n  /**\n   * Fired when the reader asks for a fresh score. Without it a stale score says it is stale\n   * and offers nothing to do about it.\n   */\n  onRecompute?: (assessment: RiskAssessment) => void;\n}\n\nexport const RiskIndicator = React.forwardRef<HTMLDivElement, RiskIndicatorProps>(\n  function RiskIndicator(\n    {\n      assessment,\n      now,\n      notADiagnosis,\n      driverCount = 4,\n      density = \"default\",\n      onOpenModel,\n      onAcknowledge,\n      onRecompute,\n      className,\n      ...rest\n    },\n    ref,\n  ) {\n    const fresh = freshness(assessment, now);\n    const drivers = topDrivers(assessment.drivers ?? [], driverCount);\n    const dominant = concentration(assessment.drivers ?? []);\n    const scored = assessment.band !== \"unknown\";\n\n    return (\n      <div\n        {...rest}\n        ref={ref}\n        className={cn(\"zb-risk\", className)}\n        data-zb-risk=\"\"\n        data-zb-band={assessment.band}\n        data-zb-freshness={fresh.state}\n        data-zb-density={density}\n        role=\"group\"\n        aria-label={describeRisk(assessment, now, notADiagnosis, driverCount)}\n      >\n        <div className=\"zb-risk__head\" aria-hidden=\"true\">\n          <span className=\"zb-risk__outcome\">{assessment.outcome}</span>\n          <ClinicalStatus\n            scale=\"risk\"\n            step={BAND_STEP[assessment.band]}\n            density=\"compact\"\n            aria-hidden=\"true\"\n          />\n        </div>\n\n        {scored ? (\n          <div className=\"zb-risk__figures\" aria-hidden=\"true\">\n            {/*\n              The numeral is demoted, not hidden. Two decimal places imply a\n              precision the model does not have, so it is rounded to a whole\n              per cent and set below the band rather than beside it.\n            */}\n            {typeof assessment.probability === \"number\" ? (\n              <span className=\"zb-risk__probability\">\n                {Math.round(assessment.probability * 100)}\n                <span className=\"zb-risk__unit\">%</span>\n              </span>\n            ) : null}\n\n            {/*\n              A percentile is never rendered without the cohort it is a\n              percentile of. \"94th percentile\" of an unnamed population reads\n              as \"94th percentile of people like this patient\", which is a\n              claim nobody made.\n            */}\n            {typeof assessment.percentile === \"number\" && assessment.cohort ? (\n              <span className=\"zb-risk__percentile\">\n                {ordinal(assessment.percentile)} pct · {assessment.cohort}\n              </span>\n            ) : null}\n          </div>\n        ) : (\n          <p className=\"zb-risk__unscored\" aria-hidden=\"true\">\n            The model could not score this patient. This is not a low score.\n          </p>\n        )}\n\n        {drivers.length ? <Drivers drivers={drivers} dominant={dominant} /> : null}\n\n        {/*\n          Staleness on the face, never in a tooltip.\n          Computed nightly, read at noon, after the admission that would have\n          changed it — and nobody hovered.\n        */}\n        {/*\n          Not aria-hidden as a whole: it holds the model button.\n          Hiding a container hides everything focusable inside it, which is\n          both an axe violation and a button nobody can reach.\n        */}\n        <div className=\"zb-risk__foot\">\n          <span className=\"zb-risk__age\" data-zb-freshness={fresh.state} aria-hidden=\"true\">\n            {fresh.state === \"expired\"\n              ? `Expired ${describeAge(fresh.expiredForMs)} ago`\n              : `Computed ${describeAge(fresh.ageMs)} ago`}\n          </span>\n\n          {assessment.model ? (\n            onOpenModel ? (\n              <button\n                type=\"button\"\n                className=\"zb-risk__model\"\n                onClick={() => onOpenModel(assessment)}\n              >\n                {assessment.model.name}\n                {typeof assessment.model.auc === \"number\" ? ` · AUC ${assessment.model.auc}` : \"\"}\n              </button>\n            ) : (\n              <span className=\"zb-risk__model\" data-zb-static=\"\">\n                {assessment.model.name}\n                {typeof assessment.model.auc === \"number\" ? ` · AUC ${assessment.model.auc}` : \"\"}\n              </span>\n            )\n          ) : null}\n        </div>\n\n        {/*\n          An expired score offers two ways out and no third.\n          Dismissal is not one of them: a score somebody waved away is a score\n          that stays on the panel looking current.\n        */}\n        {fresh.state === \"expired\" && (onRecompute || onAcknowledge) ? (\n          <div className=\"zb-risk__expired\">\n            {onRecompute ? (\n              <button\n                type=\"button\"\n                className=\"zb-risk__action\"\n                onClick={() => onRecompute(assessment)}\n              >\n                Recompute\n              </button>\n            ) : null}\n            {onAcknowledge ? (\n              <button\n                type=\"button\"\n                className=\"zb-risk__action\"\n                data-zb-secondary=\"\"\n                onClick={() => onAcknowledge(assessment)}\n              >\n                Acknowledge as expired\n              </button>\n            ) : null}\n          </div>\n        ) : null}\n\n        {/* Last on the face and last in the spoken sentence: put first it is\n            boilerplate a reader skips. */}\n        <p className=\"zb-risk__framing\" aria-hidden=\"true\">\n          {notADiagnosis}\n        </p>\n      </div>\n    );\n  },\n);\n\n/**\n * The drivers, with direction and weight.\n *\n * Weights are the model's own units and are never normalised across models —\n * rescaling somebody else's attributions to fit a bar is how a driver\n * contributing 11.2 and one contributing 0.4 end up looking comparable. The\n * bar is scaled within *this* set only, which is a comparison the data\n * supports.\n */\nfunction Drivers({ drivers, dominant }: { drivers: RiskDriver[]; dominant: number | null }) {\n  const widest = Math.max(...drivers.map((d) => Math.abs(d.weight)), 1);\n  const weighted = drivers.some((d) => d.weight !== 0);\n\n  return (\n    <div className=\"zb-risk__drivers\" aria-hidden=\"true\">\n      <p className=\"zb-risk__drivers-title\">Top drivers</p>\n      <ul className=\"zb-risk__driver-list\">\n        {drivers.map((driver) => (\n          <li\n            key={driver.label}\n            className=\"zb-risk__driver\"\n            data-zb-direction={driverDirection(driver)}\n          >\n            <span className=\"zb-risk__driver-label\">{driver.label}</span>\n            {weighted ? (\n              <>\n                <span className=\"zb-risk__driver-bar\">\n                  <span\n                    className=\"zb-risk__driver-fill\"\n                    style={{ inlineSize: `${(Math.abs(driver.weight) / widest) * 100}%` }}\n                  />\n                </span>\n                <span className=\"zb-risk__driver-weight\">\n                  {driver.weight > 0 ? \"+\" : \"\"}\n                  {driver.weight}\n                </span>\n              </>\n            ) : (\n              // `basis` from a FHIR RiskAssessment names references with no\n              // weights. Drawing a bar for them would be inventing one.\n              <span className=\"zb-risk__driver-weight\" data-zb-unweighted=\"\">\n                contributing\n              </span>\n            )}\n          </li>\n        ))}\n      </ul>\n\n      {/*\n        The number that says whether this is a synthesis or a proxy.\n        A model whose top driver is most of the total is not modelling a\n        patient; it is reporting one event, and the clinician should know which.\n      */}\n      {dominant !== null && dominant >= 0.5 ? (\n        <p className=\"zb-risk__concentration\">\n          {Math.round(dominant * 100)}% of this score comes from one factor.\n        </p>\n      ) : null}\n    </div>\n  );\n}\n"
    }
  ]
}
