"use client" import { useEffect, useState } from "react" import { Cpu, Loader2 } from "lucide-react" import { useLoading } from "@/contexts/loading-context" export function GlobalLoading() { const { isLoading, loadingMessage } = useLoading() const [progress, setProgress] = useState(0) const [showFullScreen, setShowFullScreen] = useState(false) // Progress bar animation useEffect(() => { if (isLoading) { setProgress(0) // Fast progress to 60% const interval1 = setInterval(() => { setProgress(prev => { if (prev >= 60) { clearInterval(interval1) return prev } return prev + Math.random() * 10 }) }, 100) // Slow progress to 90% const interval2 = setInterval(() => { setProgress(prev => { if (prev >= 90) { clearInterval(interval2) return prev } return prev + Math.random() * 2 }) }, 500) // Show full screen loader only if loading takes a while (quick navigations stay minimal) const timeout = setTimeout(() => { setShowFullScreen(true) }, 800) return () => { clearInterval(interval1) clearInterval(interval2) clearTimeout(timeout) } } else { // Complete progress on finish setProgress(100) setTimeout(() => { setProgress(0) setShowFullScreen(false) }, 300) } }, [isLoading]) if (!isLoading && progress === 0) return null return ( <> {/* Top Progress Bar (Always visible during loading) */}
{/* Full Screen Overlay (Shows after 800ms) */} {showFullScreen && (
{/* Animated Logo */}
{/* Loading Message */}

{loadingMessage || "Loading..."}

{/* Progress Percentage */}

{Math.round(progress)}%

{/* Progress Bar */}
{/* Helpful Tip */}

Almost there…

)} ) }