import React from 'react'; import { Truncate } from "../components/ui/truncate"; import { HumanName } from 'fhir/r4'; import { Text } from '../components/ui/text'; interface FHIRHumanNameDisplayProps { name: HumanName | HumanName[] | undefined | null; truncate?: boolean; use?: string; format?: 'full' | 'family-given' | 'given-family'; separator?: string; emptyText?: string; className?: string; } const formatSingleHumanName = (name: HumanName, format: 'full' | 'family-given' | 'given-family' = 'full'): string => { if (!name) { return ''; } if (name.text) { return name.text; } if (format === 'full') { const parts: string[] = []; if (name.prefix?.length) { parts.push(name.prefix.join(' ')); } if (name.given?.length) { parts.push(name.given.join(' ')); } if (name.family) { parts.push(name.family); } if (name.suffix?.length) { parts.push(name.suffix.join(' ')); } return parts.filter(part => part).join(' '); } else if (format === 'family-given') { const parts: string[] = []; if (name.family) { parts.push(name.family); } if (name.given?.length) { parts.push(name.given.join(' ')); } return parts.filter(part => part).join(', '); } else { const parts: string[] = []; if (name.given?.length) { parts.push(name.given.join(' ')); } if (name.family) { parts.push(name.family); } return parts.filter(part => part).join(' '); } }; const getHumanNameDisplayName = (name: HumanName): string => { if (name.use === 'official') { return 'Official'; } if (name.use === 'usual') { return 'Usual'; } if (name.use === 'temp') { return 'Temporary'; } if (name.use === 'nickname') { return 'Nickname'; } if (name.use === 'anonymous') { return 'Anonymous'; } if (name.use === 'old') { return 'Old'; } if (name.use === 'maiden') { return 'Maiden'; } return 'Name'; }; export const formatFhirHumanName = ( value: HumanName | HumanName[] | undefined | null, options: { use?: string; format?: 'full' | 'family-given' | 'given-family'; separator?: string; emptyText?: string; } = {} ): string => { const { use, format = 'full', separator = ', ', emptyText = '' } = options; if (value == null) { return emptyText; } const names = Array.isArray(value) ? value : [value]; const filteredNames = use ? names.filter(name => name?.use === use) : names; if (filteredNames.length === 0) { return emptyText; } return filteredNames .map(name => formatSingleHumanName(name, format)) .filter(name => name) .join(separator); }; export const useFhirHumanName = ( value: HumanName | HumanName[] | undefined | null, options: { use?: string; format?: 'full' | 'family-given' | 'given-family'; separator?: string; emptyText?: string; } = {} ): string => { return formatFhirHumanName(value, options); }; export const getFirstHumanNameByUse = ( value: HumanName | HumanName[] | undefined | null, use: string ): HumanName | undefined => { if (value == null) { return undefined; } const names = Array.isArray(value) ? value : [value]; return names.find(name => name?.use === use); }; export const getOfficialName = ( value: HumanName | HumanName[] | undefined | null ): HumanName | undefined => { if (value == null) { return undefined; } const names = Array.isArray(value) ? value : [value]; const officialName = names.find(name => name?.use === 'official'); return officialName || names[0]; }; export const getDisplayName = ( value: HumanName | HumanName[] | undefined | null, format: 'full' | 'family-given' | 'given-family' = 'full' ): string => { const officialName = getOfficialName(value); if (!officialName) { return ''; } return formatSingleHumanName(officialName, format); }; export const getGivenNames = (name: HumanName | undefined | null): string => { if (!name || !name.given || name.given.length === 0) { return ''; } return name.given.join(' '); }; export const getFamilyName = (name: HumanName | undefined | null): string => { if (!name || !name.family) { return ''; } return name.family; }; export const FhirHumanNameDisplay: React.FC = ({ name, truncate, use, format = 'full', separator = ', ', emptyText = '', className }) => { if (!name || (Array.isArray(name) && name.length === 0)) { return {emptyText || '-'}; } const nameArray = Array.isArray(name) ? name : [name]; const filteredNames = use ? nameArray.filter(n => n?.use === use) : nameArray; if (filteredNames.length === 0) { return {emptyText || '-'}; } if (filteredNames.length > 1 && !use) { const stackedNames = (
{filteredNames.map((singleName, index) => { const displayName = getHumanNameDisplayName(singleName); const formattedName = formatSingleHumanName(singleName, format); if (!formattedName) return null; return (
{displayName}: {formattedName}
); })}
); if (truncate) { return ( {stackedNames} ); } return stackedNames; } const formattedName = formatFhirHumanName(name, { use, format, separator, emptyText }); if (truncate) { return ( {formattedName} ); } return ( {formattedName} ); };