import { useMemo } from 'react'; import type { Reference } from 'fhir/r4'; import { Card } from '../../../../components/ui/card'; import { useAuthServiceContext, usePractitionerServiceContext, useStaffPermissionServiceContext } from '../../../../services'; import { useFhirHealthcareServices } from '../../../../hooks'; import { getRefSelfBookableOverride, overrideToChoice } from '../../../../config/serviceSelfBookable'; import { getRefTelehealthEligibleOverride, overrideToTelehealthChoice } from '../../../../config/serviceTelehealthEligible'; import StaffServicesOfferedPage from './StaffServicesOfferedPage/StaffServicesOfferedPage'; import { SELF_BOOKABLE_CHOICE_LABELS, TELEHEALTH_CHOICE_LABELS } from './StaffServicesOfferedPage/offeredServiceLabels'; import { EmptyState } from '../../../../components/ui/empty-state'; const refId = (ref: Reference): string | undefined => ref.reference?.split('/').pop(); // The Services Offered tab. staff:manage gets the editor; staff:view reads the same per-practice set // and its two overrides as text, with no picker, no Remove and no Save. const StaffRecordServices = () => { const { canManage } = useStaffPermissionServiceContext(); const { practitionerState } = usePractitionerServiceContext(); const { currentOrganization } = useAuthServiceContext(); const { healthcareServicesList } = useFhirHealthcareServices(); const orgRoles = useMemo( () => (practitionerState?.roles ?? []).filter( (role) => Boolean(role.organization && refId(role.organization) === currentOrganization?.id), ), [practitionerState?.roles, currentOrganization?.id], ); const offered = useMemo(() => { const byId = new Map(); for (const role of orgRoles) { for (const ref of role.healthcareService ?? []) { const id = refId(ref); if (id && !byId.has(id)) byId.set(id, ref); } } return Array.from(byId); }, [orgRoles]); if (canManage('staff')) return ; if (!practitionerState) return null; // The catalog can be out of a viewer's reach; the name written onto the role reference stands in. const serviceName = (id: string, ref: Reference) => healthcareServicesList.find((service) => service.id === id)?.name ?? ref.display ?? id; return (

Services Offered

Services this person offers at {currentOrganization?.name ?? 'this practice'}.

{orgRoles.length === 0 ? ( ) : offered.length === 0 ? ( ) : (
{offered.map(([id, ref]) => (
{serviceName(id, ref)}
Patient self-booking
{SELF_BOOKABLE_CHOICE_LABELS[overrideToChoice(getRefSelfBookableOverride(ref))]}
Delivered by video
{TELEHEALTH_CHOICE_LABELS[overrideToTelehealthChoice(getRefTelehealthEligibleOverride(ref))]}
))}
)}
); }; export default StaffRecordServices;