import React, { useCallback, useEffect, useState } from 'react'; import { FileSignature } from 'lucide-react'; import { useAuthServiceContext } from '../../services'; import { environment } from '../../environments/environment'; import { acceptContractorTermsVersion, findAgencyAccessCause, type AgencyAccessCause, } from '../../services/agencyAccessCause'; import { AgreementBlocks } from '../FirstSignInGate/AgreementBlocks'; /** * Agency staff refused their own profile at sign-in, while their token holds this practice's agency * grant. The refusal itself says nothing, so this asks the reads that stay open (agencyAccessCause.ts) and * says what they answer: contractor Terms owed, which can be accepted here; a practice that is shut, in its * own words; or neither. It keeps the session instead of ending it. */ export const AgencyTermsGate: React.FC = () => { const { logout, wpOrganization, getValidToken } = useAuthServiceContext(); const practice = wpOrganization?.current?.name || 'this practice'; const organizationId = wpOrganization?.current?.id; const [cause, setCause] = useState('checking'); const [busy, setBusy] = useState(false); const [note, setNote] = useState(); const check = useCallback(async () => { const token = await getValidToken?.().catch(() => null); setCause(token ? await findAgencyAccessCause(token, organizationId) : { kind: 'unreadable' }); }, [getValidToken, organizationId]); useEffect(() => { void check(); }, [check]); const accept = async () => { if (cause === 'checking' || cause.kind !== 'terms-owed') return; setBusy(true); setNote(undefined); const token = await getValidToken?.().catch(() => null); const outcome = token ? await acceptContractorTermsVersion(token, cause.document.version) : 'failed'; if (outcome === 'accepted') { window.location.reload(); return; } setBusy(false); if (outcome === 'changed') { setNote('The Terms changed while this page was open. Read the current version below, then accept it.'); await check(); } else { setNote('We could not record your acceptance just now. Please try again.'); } }; const shut = cause !== 'checking' && cause.kind === 'practice-shut'; const title = cause === 'checking' ? 'Checking why your account did not open' : cause.kind === 'terms-owed' ? 'Accept the contractor Terms to continue' : cause.kind === 'practice-shut' ? `${practice} cannot be used yet` : cause.kind === 'not-terms' ? `Your account did not open at ${practice}` : 'We could not check why your account did not open'; return (

You work at {practice} through a contractor, and the practice did not open your account. You are still signed in here.

{note &&

{note}

} {cause !== 'checking' && cause.kind === 'terms-owed' && ( <>

Akashic has published contractor Terms ({cause.document.version}) that you have not accepted yet. Read them and accept to continue.

)} {shut &&

{cause.message}

} {cause !== 'checking' && cause.kind === 'not-terms' && (

It is not the contractor Terms: yours are in order. Ask {practice}'s administrator to check the role your contractor was given, then try again.

)} {cause !== 'checking' && cause.kind === 'unreadable' && (

Could not check whether your contractor account owes anything. Try again in a moment, or check the contractors console.

)}
{cause !== 'checking' && cause.kind === 'terms-owed' && ( )} {(cause === 'checking' || cause.kind !== 'terms-owed') && ( Open the contractors console )}
); }; export default AgencyTermsGate;