import { Patient, Practitioner, RelatedPerson } from 'fhir/r4';
import { Spinner } from "../../../components/ui/spinner";
import { useAuthServiceContext } from '../../../services';
import { usePatientServiceContext } from '../../../services/PatientService.tsx';
import { FhirPatientDisplay, FhirRelatedPersonDisplay } from '../../../components';
import EditPractitionerProfile from './EditProfilePage/EditPractitionerProfile/EditPractitionerProfile.tsx';
import ProofingBanner from '../../../components/ProofingBanner/ProofingBanner.tsx';
import { DismissibleAlert } from '../../../components/ui/dismissible-alert';
import { useMyProxies } from '../../../hooks/useMyProxies';
import { describeProxyCannotEditProfile, mayEditProfileAsProxy, proxyStanding } from '../../../utils/proxyFormAuthority';
const ProfilePage = () => {
const { currentUser, practitionerRoles, isLoading, actingAs } = useAuthServiceContext();
const { patientState } = usePatientServiceContext();
// Only a patient sign-in fetches this list; for anyone else it is empty.
const { proxies, listAnswered } = useMyProxies();
if (isLoading) {
return (
);
}
if (!currentUser?.fhir) {
return null;
}
if (currentUser.fhir.resourceType === 'Practitioner') {
return (
);
}
if (currentUser.fhir.resourceType === 'Patient') {
/*
* - THE SUBJECT, NOT THE LOGIN. This used to pass `currentUser.fhir`, which is ALWAYS the
* authenticated user and does not change when a proxy switches in. So while the banner read
* "Viewing 's account", this page rendered the GUARDIAN's own name, date of birth, address
* and contacts - confirmed in a browser, and byte-identical to the same page with the switch off.
*
* `patientState` is the app-wide SUBJECT: PatientService re-points it to `actingAs.subjectPatientId`
* while switched in, and to the caller's own Patient otherwise.
*
* IT FAILS CLOSED WHILE ACTING-AS, deliberately. A `?? currentUser.fhir` fallback would render the
* guardian's record under the dependent's banner for exactly as long as the subject took to load -
* which is this defect again, just briefly, and on a page carrying Edit controls. If the subject is
* not the one the grant names, show nothing rather than the wrong person.
*/
if (actingAs) {
if (patientState?.id !== actingAs.subjectPatientId) {
return (
);
}
/*
* BELOW FULL PROXY THE PROFILE IS READ-ONLY, and the page says so before anyone presses Edit.
* The editable layout offered an assist-only helper Add and Edit that the server then refused
* with a 403. The same sections render with no Edit control and no photo card.
*/
const careTier = proxyStanding(proxies, actingAs.relatedPersonId, listAnswered);
if (!mayEditProfileAsProxy(careTier)) {
return (
{describeProxyCannotEditProfile(actingAs.subjectName)}
);
}
return (
);
}
return (
);
}
if (currentUser.fhir.resourceType === 'RelatedPerson') {
return (
);
}
return null;
};
export default ProfilePage;