import { useEffect, useState } from "react"; import { Button } from "../ui/button.tsx"; import { useAuthServiceContext } from "../../services"; import { useOrganizationOwnerService } from "../../services/OrganizationOwnerService.tsx"; import { referencesPractitioner } from "../OrganizationOwnerNotice/ownerReference"; import { requestSupportTicket, type SupportTicketOutcome } from "./supportTicketRequest.ts"; /** * Only the current practice owner can open help for a held invitation. * The reference carries no inbox, link or code; opening help sends nothing and leaves the invitation held. */ export const HeldInvitationHelp = ({ reference, opening }: { reference: string; opening: string }) => { const { currentUser, currentOrganization } = useAuthServiceContext(); const { fetchOwner } = useOrganizationOwnerService(); const viewer = currentUser?.fhir?.resourceType === "Practitioner" ? currentUser.fhir.id : undefined; const organizationId = currentOrganization?.id; const [owner, setOwner] = useState<{ key: string; isOwner: boolean } | undefined>(); const [opened, setOpened] = useState<{ key: string; outcome: SupportTicketOutcome | "no-widget" | "waiting" } | undefined>(); // Whatever is read or pressed belongs to this viewer, practice and reference; a stale answer is never shown. const key = `${viewer ?? ""}|${organizationId ?? ""}|${reference}|${opening}`; useEffect(() => { if (!viewer || !organizationId) return; let live = true; fetchOwner(organizationId) .then((state) => live && setOwner({ key, isOwner: state.status === "accepted" && referencesPractitioner(state.ownerPractitionerId, viewer) })) .catch(() => live && setOwner({ key, isOwner: false })); return () => { live = false; }; }, [key, viewer, organizationId, fetchOwner]); const isOwner = owner?.key === key ? owner.isOwner : undefined; const pressed = opened?.key === key ? opened : undefined; return (
Reference for support: {reference}
{isOwner === true && viewer && organizationId && ( )} {isOwner === false &&Your practice owner can ask us for help with this.
} {pressed?.outcome === "filled" && (The help panel is open with this reference filled in. Nothing is sent until you send it there.
)} {pressed?.outcome === "existing-draft" && (A ticket you started is already open in the help panel, so it was left as it was. Quote this reference in it.
)} {pressed?.outcome === "refused" && (The help panel cannot open during a call. Afterwards, open the assistant, choose to submit a ticket, and quote this reference.
)} {pressed?.outcome === "no-widget" && (Open the assistant, choose to submit a ticket, and quote this reference.
)}