'use client'; import { useEffect, useState, useTransition } from 'react'; import Image from 'next/image'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { motion, AnimatePresence } from 'motion/react'; import { RotateCw, ArrowLeft, Home, AlertTriangle, Terminal, Copy, Check, ChevronDown, ChevronUp, ShieldCheck, Server, WifiOff, LifeBuoy, Info, } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Card900, CardContent } from '@/components/ui/card'; import { toast } from 'sonner'; import { clientLogger } from '@/lib/logger-client'; interface TaskErrorProps { error: Error & { digest?: string }; reset: () => void; } export default function TaskError({ error, reset }: TaskErrorProps) { const router = useRouter(); const [isPending, startTransition] = useTransition(); const [showDetails, setShowDetails] = useState(false); const [copied, setCopied] = useState(false); const [retrying, setRetrying] = useState(false); useEffect(() => { try { console.error('[TaskPageError]', { name: error?.name, message: error?.message, digest: error?.digest, stack: error?.stack, }); if (error?.stack) { clientLogger.error( `[TaskPageError] ${error.message} (digest: ${error.digest || 'none'})\n${error.stack}` ); } } catch { // Ignore logging fallback errors } }, [error]); const rawMessage = error?.message || ''; const digest = error?.digest; // Detect if Next.js sanitized the error message in production const isProductionOmitted = rawMessage.includes('Server Components render') || rawMessage.includes('specific message is omitted in production') || rawMessage.includes('digest property is included'); const is500Error = rawMessage.includes('500') || rawMessage.toLowerCase().includes('internal server error'); const isNetworkError = rawMessage.toLowerCase().includes('fetch failed') || rawMessage.toLowerCase().includes('econnrefused') || rawMessage.toLowerCase().includes('network') || rawMessage.toLowerCase().includes('timeout'); // Context-sensitive status title and human-friendly explanation let badgeText = 'Application Error'; let headline = 'Unable to Load Task & Client Data'; let friendlyDescription = 'The system was unable to retrieve client records and task assignments from the backend service. This usually occurs when the backend server is temporarily unreachable, restarting, or under maintenance.'; if (is500Error) { badgeText = 'Backend Server Error (500)'; headline = 'Backend Service Encountered an Error'; friendlyDescription = 'The API server responded with an internal server error while fetching task records. Backend services may currently be undergoing a reload or processing a heavy task.'; } else if (isNetworkError) { badgeText = 'Network Unreachable'; headline = 'Connection to API Server Lost'; friendlyDescription = 'A secure connection to the data server could not be established. Please check your internet connectivity or re-verify that your login session is still active.'; } else if (!isProductionOmitted && rawMessage.trim().length > 0) { badgeText = 'Task Service Error'; headline = 'Failed to Retrieve Task Records'; friendlyDescription = rawMessage; } const handleRetry = () => { setRetrying(true); startTransition(() => { try { reset(); router.refresh(); } catch { window.location.reload(); } }); // Fallback hard refresh if soft reset doesn't resolve within 2.5 seconds setTimeout(() => { window.location.reload(); }, 2500); }; const handleCopyDiagnostics = async () => { const diagnosticsData = { component: 'TaskDashboardError', timestamp: new Date().toISOString(), digest: digest || 'N/A', errorName: error?.name || 'Error', errorMessage: rawMessage, detectedType: is500Error ? 'HTTP 500' : isNetworkError ? 'Network Timeout' : isProductionOmitted ? 'Production Sanitized' : 'Application Error', stack: error?.stack || 'No stack trace available', url: typeof window !== 'undefined' ? window.location.href : '', userAgent: typeof navigator !== 'undefined' ? navigator.userAgent : '', }; try { await navigator.clipboard.writeText(JSON.stringify(diagnosticsData, null, 2)); setCopied(true); toast.success('Diagnostic report copied to clipboard'); setTimeout(() => setCopied(false), 2500); } catch { toast.error('Failed to copy diagnostics'); } }; return (
{/* Top Minimal Navigation Breadcrumb */}
Task Management
{digest && (
Ref: {digest}
)}
{/* Main Hero Error Card */}
{/* 3D Illustration Container */}
Task service connection illustration
{/* Content & Action Area */}
{/* Status Pill Badge */}
{badgeText}
{isProductionOmitted && ( Production Mode )}
{/* Headline */}

{headline}

{/* Human-friendly description */}

{friendlyDescription}

{/* Actions Group */}
{/* Auto-reconnect advisory note */}

Tip: If the backend is restarting, retrying in 10-15 seconds usually resolves the issue.

{/* Collapsible Technical Diagnostics Drawer */}
{showDetails && (

Developer Information

Use this information when reporting bugs or inspecting backend server logs.

{/* Summary Metadata Key-Values */}
Error Name

{error?.name || 'Error'}

Digest ID

{digest || 'N/A'}

Classification

{is500Error ? 'HTTP 500' : isNetworkError ? 'Network Timeout' : isProductionOmitted ? 'Server Component' : 'API Failure'}

Timestamp

{new Date().toLocaleTimeString()}

{/* Error Message Box */}
Original Message:
{rawMessage || 'No specific error message provided.'}
{/* Stack Trace (if present) */} {error?.stack && (
Stack Trace:
                                        {error.stack}
                                    
)}
)}

Technical Support

If this message persists after several retries, copy the diagnostic report above and share it with the dev team.

); }