'use client'; import React, { useEffect, useState } from 'react'; import Link from 'next/link'; import { onAuthStateChanged } from 'firebase/auth'; import { firebaseAuth } from '../../lib/firebase-client'; interface OperationsErrorStateProps { status: 401 | 403 | 404 | 503 | 'disabled' | 'network' | number; message?: string; onRetry?: () => void; } export function OperationsErrorState({ status, message, onRetry, }: OperationsErrorStateProps) { const [authenticating, setAuthenticating] = useState(false); useEffect(() => { if (status !== 401) return; const unsubscribe = onAuthStateChanged(firebaseAuth, async (user) => { if (user) { setAuthenticating(true); try { const idToken = await user.getIdToken(); const res = await fetch('/api/auth/firebase-login', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ id_token: idToken }), }); if (res.ok) { window.location.reload(); return; } } catch { // Ignore and remain on error state } finally { setAuthenticating(false); } } }); return () => unsubscribe(); }, [status]); let title = 'Operations Error'; let description = authenticating ? 'Verifying operator credentials and activating session...' : (message ?? 'An unexpected error occurred in the Operations console.'); let icon = authenticating ? 'sync' : 'error'; let showSignIn = false; let showRetry = true; if (status === 401) { title = authenticating ? 'Authenticating Operator...' : 'Authentication Required'; description = authenticating ? 'Verifying operator credentials and establishing secure session...' : (message ?? 'Your Operations session has expired or is unauthenticated.'); icon = authenticating ? 'sync' : 'lock'; showSignIn = !authenticating; showRetry = false; } else if (status === 403) { title = 'Access Restricted'; description = message ?? 'You do not have sufficient authority to access Operations.'; icon = 'gavel'; showSignIn = false; showRetry = false; } else if (status === 404) { title = 'Resource Not Found'; description = message ?? 'The requested Operations resource does not exist.'; icon = 'search_off'; } else if (status === 503 || status === 'network') { title = 'Operations Temporarily Unavailable'; description = message ?? 'Unable to communicate with the Operations API. Try again in a few moments.'; icon = 'cloud_off'; } else if (status === 'disabled') { title = 'Operations Disabled'; description = message ?? 'The Operations platform is currently disabled by system policy.'; icon = 'block'; showRetry = false; } return (
{icon}
Status {status}

{title}

{description}

{showSignIn && ( login Sign In )} {showRetry && onRetry && ( )} Return Home
); }