All files / src/components FhirResourceViewer.tsx

100% Statements 85/85
78.68% Branches 96/122
100% Functions 17/17
100% Lines 79/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469                                                                                                          4x                                                                                                 4x                 47x 47x 8x 8x 7x         40x 19x       19x   21x 11x       11x   10x 2x       2x   8x 3x 3x   5x 1x       1x   4x 1x       1x   3x 1x       1x   2x 1x   1x       47x                 47x 47x       47x 11x 11x 4x   7x 7x 3x           9x 9x 1x 1x 1x     9x 9x 44x   9x                                               26x                                                 448x 448x 448x 448x   448x       37x 37x                                             47x 47x 47x 47x   47x     9x                                                                                                                     63x 63x 438x 438x 437x 437x       63x                       63x 630x 402x     63x   63x                                                                                                           402x 402x                                     402x            
"use client";
 
import { useState, useCallback } from "react";
import {
  ChevronDown,
  ChevronRight,
  Copy,
  Activity,
  User,
  Stethoscope,
  FlaskConical,
  Pill,
  Syringe,
  FileText,
  Heart,
  Scissors,
  ClipboardList,
  AlertTriangle,
  X,
} from "lucide-react";
 
/* ── Types ─────────────────────────────────────── */
 
interface FhirResource {
  resourceType: string;
  id?: string;
  [key: string]: unknown;
}
 
interface BundleEntry {
  fullUrl?: string;
  resource: FhirResource;
}
 
interface FhirBundle {
  resourceType: "Bundle";
  id?: string;
  type?: string;
  total?: number;
  entry?: BundleEntry[];
}
 
interface FhirResourceViewerProps {
  bundle: FhirBundle;
  title?: string;
  onClose?: () => void;
}
 
/* ── Resource type icons & colors ─────────────── */
 
const RESOURCE_META: Record<
  string,
  { icon: typeof Activity; color: string; label: string }
> = {
  Patient: { icon: User, color: "var(--fhir-patient)", label: "Patients" },
  Encounter: {
    icon: Stethoscope,
    color: "var(--fhir-encounter)",
    label: "Encounters",
  },
  Condition: {
    icon: Heart,
    color: "var(--fhir-condition)",
    label: "Conditions",
  },
  Observation: {
    icon: FlaskConical,
    color: "var(--fhir-observation)",
    label: "Observations",
  },
  MedicationRequest: {
    icon: Pill,
    color: "var(--fhir-medication)",
    label: "Medications",
  },
  Procedure: {
    icon: Scissors,
    color: "var(--fhir-procedure)",
    label: "Procedures",
  },
  Immunization: {
    icon: Syringe,
    color: "var(--fhir-immunization)",
    label: "Immunizations",
  },
  DiagnosticReport: {
    icon: FileText,
    color: "var(--fhir-diagnostic)",
    label: "Diagnostic Reports",
  },
  AllergyIntolerance: {
    icon: AlertTriangle,
    color: "var(--fhir-allergy)",
    label: "Allergies",
  },
  CarePlan: {
    icon: ClipboardList,
    color: "var(--fhir-careplan)",
    label: "Care Plans",
  },
};
 
const DEFAULT_META = {
  icon: Activity,
  color: "var(--text-secondary)",
  label: "Resources",
};
 
/* ── Helpers ───────────────────────────────────── */
 
function getResourceDisplayName(r: FhirResource): string {
  const rt = r.resourceType;
  if (rt === "Patient") {
    const names = r.name as Array<{ given?: string[]; family?: string }>;
    if (names?.[0]) {
      return `${(names[0].given ?? []).join(" ")} ${
        names[0].family ?? ""
      }`.trim();
    }
  }
  if (rt === "Condition" || rt === "Procedure") {
    const cc = r.code as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return cc?.text ?? cc?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "Observation") {
    const cc = r.code as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return cc?.text ?? cc?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "MedicationRequest") {
    const med = r.medicationCodeableConcept as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return med?.text ?? med?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "Encounter") {
    const t = r.type as Array<{ text?: string }>;
    return t?.[0]?.text ?? r.id ?? "—";
  }
  if (rt === "Immunization") {
    const vc = r.vaccineCode as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return vc?.text ?? vc?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "DiagnosticReport") {
    const cc = r.code as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return cc?.text ?? cc?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "AllergyIntolerance") {
    const cc = r.code as {
      text?: string;
      coding?: Array<{ display?: string }>;
    };
    return cc?.text ?? cc?.coding?.[0]?.display ?? r.id ?? "—";
  }
  if (rt === "CarePlan") {
    return (r.title as string) ?? r.id ?? "—";
  }
  return r.id ?? "—";
}
 
function getResourceDate(r: FhirResource): string | null {
  const candidates = [
    r.effectiveDateTime,
    r.recordedDate,
    r.onsetDateTime,
    r.occurrenceDateTime,
    r.authoredOn,
    r.period && (r.period as { start?: string }).start,
    r.birthDate,
  ] as (string | undefined)[];
  const d = candidates.find(Boolean);
  return d ? String(d).slice(0, 10) : null;
}
 
function getObservationValue(r: FhirResource): string | null {
  if (r.resourceType !== "Observation") return null;
  const vq = r.valueQuantity as { value?: number; unit?: string };
  if (vq?.value != null) {
    return `${Number(vq.value).toFixed(1)} ${vq.unit ?? ""}`.trim();
  }
  const vcc = r.valueCodeableConcept as { text?: string };
  if (vcc?.text) return vcc.text;
  return null;
}
 
/* ── Sub-components ────────────────────────────── */
 
function ResourceDetail({ resource }: { resource: FhirResource }) {
  const [copied, setCopied] = useState(false);
  const copyJson = useCallback(() => {
    navigator.clipboard.writeText(JSON.stringify(resource, null, 2));
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  }, [resource]);
 
  const meta = RESOURCE_META[resource.resourceType] ?? DEFAULT_META;
  const skipKeys = new Set(["resourceType", "id", "meta", "text"]);
  const entries = Object.entries(resource).filter(([k]) => !skipKeys.has(k));
 
  return (
    <div className="bg-[var(--surface-2)]/50 rounded-lg p-3 space-y-2">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2">
          <span
            className="w-2 h-2 rounded-full shrink-0"
            style={{ background: meta.color }}
          />
          <span className="text-xs font-semibold text-[var(--text-primary)]">
            {resource.resourceType}/{resource.id ?? "?"}
          </span>
        </div>
        <button
          onClick={copyJson}
          className="flex items-center gap-1 text-[10px] text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
        >
          <Copy size={10} />
          {copied ? "Copied!" : "JSON"}
        </button>
      </div>
      {/* Key-value pairs */}
      <div className="space-y-1">
        {entries.map(([key, val]) => (
          <div key={key} className="flex gap-2 text-xs">
            <span className="text-[var(--layer1-text)] shrink-0 w-40 truncate">
              {key}
            </span>
            <span className="text-[var(--text-primary)] break-all truncate">
              {typeof val === "string"
                ? val
                : typeof val === "number" || typeof val === "boolean"
                  ? String(val)
                  : JSON.stringify(val)}
            </span>
          </div>
        ))}
      </div>
    </div>
  );
}
 
function ResourceGroup({
  resourceType,
  resources,
}: {
  resourceType: string;
  resources: FhirResource[];
}) {
  const [expanded, setExpanded] = useState(false);
  const [selectedIdx, setSelectedIdx] = useState<number | null>(null);
  const meta = RESOURCE_META[resourceType] ?? DEFAULT_META;
  const Icon = meta.icon;
 
  return (
    <div className="border border-[var(--border)] rounded-lg overflow-hidden">
      <button
        onClick={() => {
          setExpanded(!expanded);
          setSelectedIdx(null);
        }}
        className="w-full text-left px-4 py-2.5 flex items-center gap-3 hover:bg-[var(--surface-2)]/50"
      >
        {expanded ? (
          <ChevronDown size={14} className="text-[var(--text-secondary)]" />
        ) : (
          <ChevronRight size={14} className="text-[var(--text-secondary)]" />
        )}
        <Icon size={14} style={{ color: meta.color }} />
        <span className="text-sm font-medium" style={{ color: meta.color }}>
          {meta.label}
        </span>
        <span className="text-xs text-[var(--text-secondary)] ml-auto">
          {resources.length} resource{resources.length !== 1 ? "s" : ""}
        </span>
      </button>
 
      {expanded && (
        <div className="border-t border-[var(--border)]">
          {/* Resource list */}
          <div className="divide-y divide-gray-800">
            {resources.map((r, i) => {
              const display = getResourceDisplayName(r);
              const date = getResourceDate(r);
              const value = getObservationValue(r);
              const isSelected = selectedIdx === i;
 
              return (
                <div key={r.id ?? i}>
                  <button
                    onClick={() => setSelectedIdx(isSelected ? null : i)}
                    className={`w-full text-left px-4 py-2 flex items-center gap-3 text-xs transition-colors ${
                      isSelected
                        ? "bg-[var(--surface-2)]"
                        : "hover:bg-[var(--surface-2)]/40"
                    }`}
                  >
                    <span
                      className="w-1.5 h-1.5 rounded-full shrink-0"
                      style={{ background: meta.color }}
                    />
                    <span className="text-[var(--text-primary)] flex-1 truncate">
                      {display}
                    </span>
                    {value && (
                      <span className="text-cyan-400 font-mono shrink-0">
                        {value}
                      </span>
                    )}
                    {date && (
                      <span className="text-[var(--text-secondary)] shrink-0">
                        {date}
                      </span>
                    )}
                    {isSelected ? (
                      <ChevronDown
                        size={12}
                        className="text-[var(--text-secondary)]"
                      />
                    ) : (
                      <ChevronRight
                        size={12}
                        className="text-[var(--text-secondary)]"
                      />
                    )}
                  </button>
                  {isSelected && (
                    <div className="px-4 pb-3">
                      <ResourceDetail resource={r} />
                    </div>
                  )}
                </div>
              );
            })}
          </div>
        </div>
      )}
    </div>
  );
}
 
/* ── Main component ────────────────────────────── */
 
export default function FhirResourceViewer({
  bundle,
  title,
  onClose,
}: FhirResourceViewerProps) {
  // Group entries by resource type
  const grouped: Record<string, FhirResource[]> = {};
  for (const entry of bundle.entry ?? []) {
    const rt = entry.resource?.resourceType;
    if (!rt) continue;
    if (!grouped[rt]) grouped[rt] = [];
    grouped[rt].push(entry.resource);
  }
 
  // Ordered resource types
  const typeOrder = [
    "Patient",
    "Encounter",
    "Condition",
    "Observation",
    "MedicationRequest",
    "Procedure",
    "Immunization",
    "DiagnosticReport",
    "AllergyIntolerance",
    "CarePlan",
  ];
  const orderedTypes = [
    ...typeOrder.filter((t) => grouped[t]),
    ...Object.keys(grouped).filter((t) => !typeOrder.includes(t)),
  ];
 
  const totalResources = bundle.entry?.length ?? 0;
 
  return (
    <div className="border border-layer3 rounded-xl overflow-hidden bg-[var(--surface)]/80">
      {/* Header */}
      <div className="flex items-center justify-between px-4 py-3 bg-layer3/10 border-b border-layer3/30">
        <div className="flex items-center gap-2">
          <Activity size={14} className="text-green-800 dark:text-green-300" />
          <span className="text-sm font-semibold text-green-800 dark:text-green-300">
            {title ?? "FHIR Resource Viewer"}
          </span>
          <span className="text-xs text-[var(--text-secondary)]">
            {totalResources} resources · {orderedTypes.length} types
          </span>
        </div>
        {onClose && (
          <button
            onClick={onClose}
            className="text-[var(--text-secondary)] hover:text-[var(--text-primary)]"
          >
            <X size={16} />
          </button>
        )}
      </div>
 
      {/* Synthetic data notice */}
      <div className="px-4 py-2 bg-amber-950/60 border-b border-amber-700/50 flex items-center gap-2 text-xs text-amber-300">
        <AlertTriangle size={12} className="shrink-0" />
        <span>
          <strong>Synthea Synthetic Data</strong> — algorithmically generated
          patients, not real individuals. No pseudonymisation required.
        </span>
      </div>
 
      {/* Bundle info bar */}
      <div className="px-4 py-2 border-b border-[var(--border)] flex flex-wrap gap-4 text-xs text-[var(--text-secondary)]">
        <span>
          Bundle ID:{" "}
          <span className="text-[var(--text-primary)]">{bundle.id ?? "—"}</span>
        </span>
        <span>
          Type:{" "}
          <span className="text-[var(--text-primary)]">
            {bundle.type ?? "—"}
          </span>
        </span>
        <span>
          Total:{" "}
          <span className="text-[var(--text-primary)]">{totalResources}</span>
        </span>
      </div>
 
      {/* Resource type summary */}
      <div className="px-4 py-3 border-b border-[var(--border)]">
        <div className="flex flex-wrap gap-2">
          {orderedTypes.map((rt) => {
            const meta = RESOURCE_META[rt] ?? DEFAULT_META;
            return (
              <span
                key={rt}
                className="inline-flex items-center gap-1 text-xs px-2 py-0.5 rounded-full"
                style={{
                  background: `${meta.color}20`,
                  color: meta.color,
                }}
              >
                {rt} ({grouped[rt].length})
              </span>
            );
          })}
        </div>
      </div>
 
      {/* Resource groups */}
      <div className="p-4 space-y-2 max-h-[500px] overflow-y-auto">
        {orderedTypes.map((rt) => (
          <ResourceGroup key={rt} resourceType={rt} resources={grouped[rt]} />
        ))}
      </div>
    </div>
  );
}