"use client"; import { Component, ReactNode, useState, useEffect, Suspense } from "react"; import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; import { AlertCircle, RefreshCw } from "lucide-react"; import { Skeleton } from "@/components/ui/skeleton"; import { Card, CardContent, CardHeader } from "@/components/ui/card"; // Error Boundary Component interface ErrorBoundaryState { hasError: boolean; error: Error | null; } interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; componentName?: string; } class ErrorBoundary extends Component { private retryCount = 0; private maxRetries = 2; constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error(`Error loading ${this.props.componentName || 'component'}:`, error, errorInfo); } handleRetry = () => { if (this.retryCount < this.maxRetries) { this.retryCount++; this.setState({ hasError: false, error: null }); } else { window.location.reload(); } }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return ( Failed to load {this.props.componentName || 'component'}

{this.state.error?.message || 'An unexpected error occurred while loading this component.'}

{this.retryCount < this.maxRetries && (

Retry attempt {this.retryCount + 1} of {this.maxRetries + 1}

)}
); } return this.props.children; } } // Suspense wrapper with timeout function SuspenseWithTimeout({ children, fallback, timeout = 5000, timeoutFallback }: { children: ReactNode; fallback: ReactNode; timeout?: number; timeoutFallback?: ReactNode; }) { const [showTimeoutWarning, setShowTimeoutWarning] = useState(false); useEffect(() => { const timer = setTimeout(() => { setShowTimeoutWarning(true); }, timeout); return () => clearTimeout(timer); }, [timeout]); return ( {children} ); } // Loading skeleton with timeout warning function DashboardContentSkeletonWithWarning() { return (
Taking longer than expected The dashboard is still loading. This may be due to a slow connection. Please wait...
{[...Array(4)].map((_, i) => ( ))}
); } // Import the skeleton from the page function DashboardContentSkeleton() { return (
{/* Subtle loading indicator */}
{/* Header skeleton */}
{/* Stats cards skeleton */}
{[...Array(4)].map((_, i) => ( ))}
{/* Best selling products skeleton */}
{[...Array(5)].map((_, i) => (
))}
); } export default function DashboardContentWrapper({ children }: { children: ReactNode }) { return ( } timeout={5000} timeoutFallback={} > {children} ); }