import { useCallback } from "react"; import { environment } from "../environments/environment.ts"; import { useHttpService } from "./HttpService.tsx"; // Share access by code. The code and claim link come back once, when shown; the server keeps only // their hashes, so a reopened screen shows a new code rather than the old one. const API_URL = environment.fhirClinicalApi; export type ShareState = "none" | "minted" | "claimed" | "approving" | "approved" | "declined" | "cancelled" | "expired"; export interface ShownCode { code: string; claimUrl: string; expiresAt: string; offerRef: string; } export interface ShareClaimant { registryName: string; registryTaxonomy: string | null; npi: string; accountName: string; accountEmail: string; namesAgree: boolean; } export interface ShareDocument { version: string; text: string; hash: string; } export interface ShareStatus { state: ShareState; version?: number; expiresAt?: string; offerRef?: string; claimRef?: string; claimant?: ShareClaimant; document?: ShareDocument; /** The hash of the document already signed, while that approval is being finished. */ documentHash?: string; } export interface SignedAuthorization { offerRef: string; state: "approving" | "approved"; signedAt: string; approvedAt?: string; recipient: { registryName: string; npi: string }; signedText: string; teamId: string; } /** The patient's retained signed copies, and the server's word on what sharing serves now, whatever was signed. */ export interface SignedAuthorizations { authorizations: SignedAuthorization[]; availability?: string; } // Every decision names the offer it is about; approve and decline also name the claim shown. export type ShareDecision = | { decision: "approve"; offerRef: string; claimRef: string; version: number; documentHash: string } | { decision: "decline"; offerRef: string; claimRef: string; version: number } | { decision: "cancel"; offerRef: string }; export const useShareAccessService = () => { const { get, post } = useHttpService(); const showCode = useCallback((): Promise => post(`${API_URL}/share-access`, {}), [post]); const readStatus = useCallback((): Promise => get(`${API_URL}/share-access`), [get]); const decide = useCallback( (decision: ShareDecision): Promise<{ state: ShareState; teamId?: string }> => post(`${API_URL}/share-access-decision`, decision), [post], ); const readAuthorizations = useCallback( (): Promise => get(`${API_URL}/share-access?view=authorizations`), [get], ); return { showCode, readStatus, decide, readAuthorizations }; }; export type ShareAccessService = ReturnType;