import React from "react"; import { CodeableConcept, Observation, ObservationReferenceRange } from "fhir/r4"; import { FhirReferenceDisplay } from "../../utils/fhirReference"; import { FhirAnnotationDisplay } from "../../utils/fhirAnnotation"; import { FhirPeriodDisplay } from "../../utils/fhirPeriod"; import { FhirQuantityDisplay } from "../../utils/fhirQuantity"; import { FhirRangeDisplay } from "../../utils/fhirRange"; import { FhirRatioDisplay } from "../../utils/fhirRatio"; import { formatDateTime } from "../../utils/dateUtils"; import { Card, CardContent, CardHeader } from "../ui/card"; import { Calendar, UserRoundPlus } from "lucide-react"; import { SheetDescription, SheetHeader, SheetTitle } from "../ui/sheet"; import { formatDateForDisplay, formatTimeForDisplay } from "../../lib/utils"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "../ui/tabs"; import { FieldHistoryProvider, FieldHistoryScope, } from "../FieldHistory/FieldHistoryProvider"; import { ShowFieldHistoryToggle } from "../FieldHistory/ShowFieldHistoryToggle"; import { RecordEditedMarker } from "../FieldHistory/RecordEditedMarker"; import { Field } from "../FieldHistory/Field"; import { FieldGroup } from "../FieldHistory/FieldLayout"; import { SourcePractice } from "../ui/source-practice"; import { SELF_REPORTED_SOURCE_LABEL, SELF_REPORTED_SOURCE_WORDS, selfReportedSourceOf } from "../../utils/selfReportedSource"; import { StatusBadge } from "../ui/status"; import { ObservationResultPanel, isLaboratoryResult, resultName } from "./ObservationResultPanel"; import { bloodPressureReading, componentReadings } from "../../utils/observationPanelValue"; import { namedPerformers } from "../../utils/observationPerformer"; // A coded flag such as High often has no text; its coding's display is the words to show. const conceptText = (concept: CodeableConcept): string => concept.text || (concept.coding ?? []).map((coding) => coding.display || coding.code).filter(Boolean).join(", "); // Text, else the coding displays or codes; a dash only when nothing was recorded. const conceptWords = (concept?: CodeableConcept): string => (concept && conceptText(concept)) || "-"; const rangeText = (range: ObservationReferenceRange): string => { if (range.text) return range.text; const unit = range.high?.unit ?? range.low?.unit ?? ""; const { low, high } = { low: range.low?.value, high: range.high?.value }; const span = low !== undefined && high !== undefined ? `${low}–${high}` : low !== undefined ? `${low} or above` : high !== undefined ? `up to ${high}` : ""; return span ? `${span}${unit ? ` ${unit}` : ""}` : ""; }; interface FhirObservationDisplayProps { observation: Observation; isOpen: boolean; onClose: () => void; title?: string; headerAction?: React.ReactNode; // The patient's other observations, so a lab result can show its own trend. history?: Observation[]; // A staff reader: show the patient's own answer about where a reported reading came from. showReportedSource?: boolean; } export const FhirObservationDisplay: React.FC = ({ observation, isOpen, headerAction, history, showReportedSource = false, }) => { // A closed detail, or one with no reading (a host mounts it while closing, or with another record selected), draws // nothing and asks nothing of the reading: nothing below may read a reading that is not there. if (!isOpen || !observation) return null; const reportedSource = showReportedSource ? selfReportedSourceOf(observation) : undefined; // A blood pressure reads from its two halves; a panel-level number stored beside them is shown apart. const pressure = bloodPressureReading(observation); const renderValue = () => { if (pressure) return pressure; if (observation.valueQuantity) { return ; } if (observation.valueCodeableConcept) { return ( conceptWords(observation.valueCodeableConcept) ); } if (observation.valueString) { return observation.valueString; } if (observation.valueBoolean !== undefined) { return observation.valueBoolean ? "Yes" : "No"; } if (observation.valueInteger !== undefined) { return observation.valueInteger.toString(); } if (observation.valueRange) { return ; } if (observation.valueRatio) { return ; } if (observation.valueTime) { return observation.valueTime; } if (observation.valueDateTime) { return formatDateTime(observation.valueDateTime); } if (observation.valuePeriod) { return ; } return undefined; // let render the canonical em-dash }; const renderComponentValue = (component: any) => { if (component.valueQuantity) { return ; } if (component.valueCodeableConcept) { return ( conceptWords(component.valueCodeableConcept) ); } if (component.valueString) { return component.valueString; } if (component.valueBoolean !== undefined) { return component.valueBoolean ? "Yes" : "No"; } if (component.valueInteger !== undefined) { return component.valueInteger.toString(); } if (component.valueRange) { return ; } if (component.valueRatio) { return ; } if (component.valueTime) { return component.valueTime; } if (component.valueDateTime) { return formatDateTime(component.valueDateTime); } if (component.valuePeriod) { return ; } return undefined; // let render the canonical em-dash }; const getObservationProfile = () => { if (observation.category && observation.category.length > 0) { const category = observation.category[0]; if (category.coding && category.coding.length > 0) { const code = category.coding[0].code; if (code === "vital-signs") { return "Vital Signs"; } if (code === "laboratory") { return "Laboratory"; } if (code === "social-history") { return "Social History"; } if (code === "screening") { return "Screening Assessment"; } } } return ""; }; return (
{resultName(observation)}
{headerAction}
{/* A blood pressure reads from its halves on every path, whatever category it carries. */} {isLaboratoryResult(observation) && !pressure ? (
) : (

{observation?.code?.text || observation?.code?.coding?.[0]?.display || "Observation"}

{getObservationProfile() || observation?.resourceType}

{" "} {observation.meta?.lastUpdated && formatDateForDisplay(observation.meta.lastUpdated)}

Status

{/* THE SHARED PILL. What was here painted `registered` - an observation with no result yet - in solid green with white text, and let `final` fall through to the quietest treatment there is. The emphasis was inverted: the state a reader is looking for was the one drawn most quietly. */}

Classification

{observation?.category?.[0]?.coding?.[0]?.display || "-"}

{/* The chart already names the patient; a dash for a missing display says nothing. */} {observation?.subject?.display ? (

Patient

{observation.subject.display}

) : null}

Clinically relevant time

{observation?.effectiveDateTime && formatDateForDisplay(observation?.effectiveDateTime)}{" "} {observation?.effectiveDateTime && formatTimeForDisplay(observation?.effectiveDateTime)}

Type

{observation?.code?.text || "-"}

Healthcare event

{/* The visit this reading records, as recorded: its own label, or that one is linked; never a raw id, never looked up. */}

{observation?.encounter && (observation.encounter.reference || observation.encounter.display?.trim() || observation.encounter.identifier) ? observation.encounter.display?.trim() || "A visit is linked to this reading" : "No references recorded"}

{/* Six tabs are wider than a phone's sheet: the strip wraps rather than scrolling the whole body sideways. */} Value & Components Details Performers Based On Part Of Notes

Value & Components

Values

{pressure && observation.valueQuantity && ( } /> )} {observation.dataAbsentReason && ( )} {(observation.referenceRange ?? []).some((range) => rangeText(range)) && ( )}
{observation?.component && observation?.component?.map((component, index) => (
{component?.code?.coding?.[0]?.display || component?.code?.text || `Component ${index + 1}`}
{/* Shown only when recorded: an unrecorded interpretation is unspecified, and an unrecorded absence reason must not make a present value look missing. */} {(component.interpretation ?? []).some((concept) => conceptText(concept)) && ( )} {component.dataAbsentReason && conceptText(component.dataAbsentReason) && ( )}
))}

Details

Observed Body Part

} />

Performers

} /> {reportedSource && ( )} } />

Based On

} />

Part Of

} />

Notes

} />
)}
); }; export default FhirObservationDisplay;