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); // Sync errorMessage when initialError updates useEffect(() => { if (initialError !== undefined) { setErrorMessage(initialError); } }, [initialError]); // Clear any existing session prompt and close background dialogs useEffect(() => { cancelSessionExpiration(); dismissSubordinateModals(); }, []); const displayName = user?.name || (claims.name as string) || (claims.preferred_username as string) || 'Registered User'; 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) || 'Standard Enterprise'; const role = user?.role || (claims.role as string) || 'User'; const handleSyncClick = async () => { if (isSyncing) return; setIsSyncing(true); setErrorMessage(null); try { const result = await onSync(true); if (result.isUnauthorized) { // HTTP 401: Routine process of routing user to landing page await onLogout(); navigate('/', { replace: true }); return; } if (!result.success) { setSyncAttemptCount((prev) => prev + 1); setErrorMessage( result.errorMessage || 'Account is not yet enabled. The administrator has not activated your account in the system directory.' ); } // If result.success is true, AuthProvider updates state, and this modal unmounts automatically! } 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 isTenantIssue = !!errorMessage?.toLowerCase().includes('tenant'); const modalContent = (
e.stopPropagation()} > {/* Subtle accent top border */}
{/* Header */}

{isTenantIssue ? 'Tenant Access Restricted' : 'Account Pending Activation'}

{isTenantIssue ? 'The assigned tenant partition is currently inactive or restricted.' : 'Your account is authenticated and awaiting administrator enablement.'}

{/* Body */}
{/* User Profile Summary */}
User {displayName}
Email {displayEmail || '—'}
Tenant {tenant}
Role {role}
{/* Feedback notice if sync failed */} {errorMessage && (
Notice:

{errorMessage}

)}

Once your administrator enables your account, click Sync Account to enter the console.

{/* Footer Actions */}
); return typeof document !== 'undefined' ? createPortal(modalContent, document.body) : modalContent; };