import React from 'react'; import { Card, CardBody, CardHeader } from '../ui/wp-card'; import { Divider } from '../ui/divider'; import { DismissibleAlert } from '../ui/dismissible-alert'; import { Heading } from '../ui/heading'; import { Flex, FlexItem } from '../ui/flex'; import { Text } from '../ui/text'; import { Spacer } from '../ui/spacer'; import { useFormContext } from 'react-hook-form'; import { Check, PlusCircle, Trash2 } from 'lucide-react'; import { Badge } from '../ui/badge'; import { FhirResource } from 'fhir/r4'; import { TypeSelector, FhirReference, PatientSearch } from '../index'; import { useAuthServiceContext, usePatientServiceContext, useFhirServiceContext, usePractitionerRoleServiceContext } from '../../services'; import { formatFhirHumanName } from '../../utils/fhirHumanName.tsx'; interface ReferenceTypeOption { name: string; key: string; } interface FhirReferenceCollectionProps { references: any; label: string; context: string; referenceTypes: ReferenceTypeOption[]; getReferenceList: (referenceType: string) => FhirResource[]; disabled?: boolean; required?: boolean; readonly?: boolean; multiple?: boolean; enableInlineActions?: boolean; } const FhirReferenceCollection: React.FC = ({ references, label, context, referenceTypes, getReferenceList, disabled = false, required = false, readonly = false, multiple = true, enableInlineActions = true }) => { const { setValue, watch } = useFormContext(); const { selectedPractitionerRole } = useAuthServiceContext(); const { patientState } = usePatientServiceContext(); const { createReferenceObject } = useFhirServiceContext(); const { getPractitionerRoleDisplay } = usePractitionerRoleServiceContext(); const spacerMargin = '24px'; const handleReferenceTypeChange = (index: number, referenceType: string) => { setValue(`${context}.${index}.reference`, '', { shouldDirty: true }); if (referenceType === 'PractitionerRole' && selectedPractitionerRole) { const reference = createReferenceObject('PractitionerRole', selectedPractitionerRole.id as string, getPractitionerRoleDisplay(selectedPractitionerRole)); setValue(`${context}.${index}.reference`, reference, { shouldDirty: true }); } else if (referenceType === 'Patient' && patientState) { const reference = createReferenceObject('Patient', patientState.id as string, formatFhirHumanName(patientState.name)); setValue(`${context}.${index}.reference`, reference, { shouldDirty: true }); } }; const addReference = () => { if (multiple || references.fields.length === 0) { references.append({ referenceType: '', reference: null }); } }; const removeReference = (index: number) => { references.remove(index); }; const isAutoSelectedType = (referenceType: string): boolean => { if (referenceType === 'PractitionerRole') { return !!selectedPractitionerRole; } if (referenceType === 'Patient') { return !!patientState; } return false; }; const shouldShowPatientSearch = (referenceType: string): boolean => { return referenceType === 'Patient' && !patientState; }; const getAutoSelectedDisplayName = (referenceType: string): string => { if (referenceType === 'PractitionerRole' && selectedPractitionerRole) { return getPractitionerRoleDisplay(selectedPractitionerRole); } if (referenceType === 'Patient' && patientState) { return formatFhirHumanName(patientState.name) || `Patient ${patientState.id}`; } return ''; }; const getCurrentReferenceType = (index: number): string => { return watch(`${context}.${index}.referenceType`) || ''; }; const canAddReference = (): boolean => { return multiple || references.fields.length === 0; }; const handlePatientSelected = (index: number, patient: any) => { if (patient) { const reference = createReferenceObject('Patient', patient.id as string, formatFhirHumanName(patient.name)); setValue(`${context}.${index}.reference`, reference, { shouldDirty: true }); } else { setValue(`${context}.${index}.reference`, null, { shouldDirty: true }); } }; return ( <> {references.fields.length === 0 && ( <> {/* A long label on a phone would push the button past the card edge. */} {label} {!readonly && ( )} No {label.toLowerCase()} added yet. )} {references.fields.map((field: any, index: number) => { const currentReferenceType = getCurrentReferenceType(index); const isAutoSelected = isAutoSelectedType(currentReferenceType); const showPatientSearch = shouldShowPatientSearch(currentReferenceType); return (

{multiple ? `${label} ${index + 1}` : label}

{!readonly && ( )}
{ handleReferenceTypeChange(index, referenceType); }}/> {currentReferenceType && ( <> {isAutoSelected ? ( {/* This was never a button: `disabled` + hardcoded green pill styling, displaying a name. It is a status BADGE, the same shape Spencer ruled on for QuestionnairesPage — squared, font-normal, colours preserved. Converting it to a disabled )}
); })} ); }; export default FhirReferenceCollection;