import React, { useMemo } from 'react'; import classNames from 'classnames'; import { GenderFemale, GenderMale } from '@carbon/react/icons'; import { age, ExtensionSlot, formatDate, getPatientName, parseDate } from '@openmrs/esm-framework'; import styles from './patient-banner.scss'; import { InlineLoading, Tag } from '@carbon/react'; import { EligibilityResponse } from '../type'; import { getEligibilityTags, maskName } from '../helper'; import { useTranslation } from 'react-i18next'; interface EnhancedPatientBannerPatientInfoProps { patient: fhir.Patient; hiePatient?: fhir.Patient; renderedFrom?: string; showSyncButton?: boolean; onSyncSuccess?: (patientUuid: string) => void; eligibilityData?: EligibilityResponse; isEligibilityLoading?: boolean; crNumber?: string; } type Gender = 'female' | 'male'; const GENDER_ICONS = { Female: , Male: , } as const; const GENDER_MAP: Record = { male: 'Male', female: 'Female', m: 'Male', f: 'Female', }; const getGender = (gender: string) => { const normalizedGender = gender.toLowerCase(); const iconKey = GENDER_MAP[normalizedGender] ?? 'Unknown'; return { displayText: iconKey, iconKey, }; }; export const EnhancedPatientBannerPatientInfo: React.FC = ({ patient, renderedFrom, eligibilityData, isEligibilityLoading = false, crNumber, }) => { const { t } = useTranslation(); const name = getPatientName(patient); const genderInfo = patient?.gender && getGender(patient.gender); const extensionState = useMemo(() => ({ patientUuid: patient.id, patient, renderedFrom }), [patient, renderedFrom]); const eligibilityTags = useMemo(() => { if (isEligibilityLoading) { return []; } if (!eligibilityData) { return []; } return getEligibilityTags(patient, eligibilityData); }, [patient, eligibilityData, isEligibilityLoading]); const shouldShowEligibilitySection = isEligibilityLoading || eligibilityTags.length > 0; return (
{maskName(name)} {genderInfo && (
{GENDER_ICONS[genderInfo.iconKey as keyof typeof GENDER_ICONS]} {genderInfo.displayText}
)}
{patient.birthDate && ( <> {age(patient.birthDate)} · {formatDate(parseDate(patient.birthDate))} · )}
{t('CRNumber', 'CR Number')}: {crNumber || '--'}
{((crNumber && crNumber !== '--') || shouldShowEligibilitySection) && ( · )} {shouldShowEligibilitySection && (
{isEligibilityLoading ? ( ) : ( eligibilityTags.map((tag, index) => ( {tag.text} )) )}
)}
); };