import React, { useState, useEffect } from 'react'; import { createPortal } from 'react-dom'; import { useNavigate } from 'react-router-dom'; import { ShieldAlert, RefreshCw, LogOut, UserCheck, CheckCircle2, AlertTriangle, ArrowLeft, Mail, Building2, Fingerprint, } from 'lucide-react'; import { BrandLogo } from '../ui/BrandLogo'; import { AuthUserProfile, UserClaims } from '../../auth/AuthProvider'; import { cancelSessionExpiration, dismissSubordinateModals } from '../../services/sessionExpiration'; interface AccountActivationModalProps { user: AuthUserProfile | null; claims: UserClaims; sub: string; onSync: (force?: boolean) => Promise<{ success: boolean; isUnauthorized?: boolean; errorMessage?: string }>; onLogout: () => Promise; initialError?: string | null; } export const AccountActivationModal: React.FC = ({ user, claims, sub, onSync, onLogout, initialError, }) => { const navigate = useNavigate(); const [isSyncing, setIsSyncing] = useState(false); const [errorMessage, setErrorMessage] = useState(initialError || null); const [syncAttemptCount, setSyncAttemptCount] = useState(0); useEffect(() => { if (initialError !== undefined) { setErrorMessage(initialError); } }, [initialError]); useEffect(() => { cancelSessionExpiration(); dismissSubordinateModals(); }, []); const displayName = user?.name || (claims.name as string) || (claims.preferred_username as string) || 'Registered Partner'; const displayEmail = user?.email || (claims.email as string) || (claims['http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress'] as string) || ''; const tenant = user?.tenant || (claims.tenant as string) || (claims.org as string) || 'Partner Organisation'; const role = user?.role || (claims.role as string) || 'Partner'; const handleSyncClick = async () => { if (isSyncing) return; setIsSyncing(true); setErrorMessage(null); try { const result = await onSync(true); if (result.isUnauthorized) { await onLogout(); navigate('/', { replace: true }); return; } if (!result.success) { setSyncAttemptCount((prev) => prev + 1); setErrorMessage( result.errorMessage || 'Your partner account is not yet enabled. Please ask your funding administrator to activate your account.' ); } } catch (err: unknown) { const errObj = err as { response?: { status?: number }; message?: string }; if (errObj?.response?.status === 401) { await onLogout(); navigate('/', { replace: true }); return; } setErrorMessage( errObj?.message || 'Account activation check encountered an error. Please confirm with your administrator and try again.' ); } finally { setIsSyncing(false); } }; const handleSignOut = async () => { await onLogout(); navigate('/', { replace: true }); }; const handleNavigateLanding = () => { navigate('/'); }; const modalContent = (
e.stopPropagation()} >
Access Restricted • Activation Pending

{errorMessage?.toLowerCase().includes('tenant') ? 'Organisation Account Inactive' : 'Partner Account Pending Activation'}

Signed In Successfully

You have signed in successfully, but your partner account is waiting to be activated by the funding administrator.

Once your administrator confirms your account is active, click{' '} Check Account Status below to open your dashboard.

Your Partner Profile Pending Activation
Name {displayName}
Email {displayEmail || 'Not specified'}
Organisation / Role {tenant} ({role})
Account ID {sub || 'SSO-Principal'}
{errorMessage && (

Account Not Activated Yet

{errorMessage}

{syncAttemptCount > 0 && (

Checked #{syncAttemptCount} at {new Date().toLocaleTimeString()}

)}
)}
); return typeof document !== 'undefined' ? createPortal(modalContent, document.body) : modalContent; };