import { useMemo } from "react"; import { environment } from "../environments/environment"; import { useAuthServiceContext, useCommunicationServiceContext } from "../services"; import { portalSendRecovery } from "../utils/messaging/portalSendRecovery"; import type { SendCloseAnswer, SendRecovery } from "../utils/messaging/sendRecovery"; /** What a Chat needs to find and settle an unconfirmed send after a full reload. Only senders that name their sends pass it. */ export interface ChatSendRecovery { store: SendRecovery; /** Where this page is: origin, sign-in, data stack and its API, and practice. Chat adds the signer and the conversation. */ place: readonly unknown[]; /** Asks the server; `admit` throws once the asking opening is gone, so the question never leaves after it. */ close: (operationId: string, admit?: () => void) => Promise; } export const useChatSendRecovery = (): ChatSendRecovery => { const { currentOrganization } = useAuthServiceContext(); const { closeSendOperation } = useCommunicationServiceContext(); const organizationId = currentOrganization?.id ?? null; return useMemo(() => ({ store: portalSendRecovery(), place: [window.location.origin, environment.localStoragePrefix, environment.stackStoragePrefix, environment.fhirClinicalApi, organizationId], close: closeSendOperation, }), [organizationId, closeSendOperation]); };