"use client"; import React, { useState, useEffect } from 'react'; import { usePathname } from 'next/navigation'; export default function PageLoader({ children }: { children: React.ReactNode }) { const pathname = usePathname(); const [isTransitioning, setIsTransitioning] = useState(false); const [loadProgress, setLoadProgress] = useState(0); const [isLoaded, setIsLoaded] = useState(true); const initialMount = React.useRef(true); useEffect(() => { if (initialMount.current && pathname === '/') { initialMount.current = false; return; } initialMount.current = false; setIsTransitioning(true); setIsLoaded(false); setLoadProgress(0); let progress = 0; // Safety net: force complete after 2500ms even if __PAGE_LOADED never fires // (e.g. the 404 page, or any page that doesn't set the flag) const safetyTimeout = setTimeout(() => { (window as any).__PAGE_LOADED = true; }, 2500); const interval = setInterval(() => { if (progress < 90) { progress += Math.random() * 15 + 5; } else if ((window as any).__PAGE_LOADED) { progress = 100; } if (progress >= 100) { progress = 100; clearInterval(interval); setIsLoaded(true); setTimeout(() => setIsTransitioning(false), 800); } setLoadProgress(progress); }, 80); return () => { clearInterval(interval); clearTimeout(safetyTimeout); // Always reset the flag on cleanup so next page starts fresh (window as any).__PAGE_LOADED = false; }; }, [pathname]); return ( <> {isTransitioning && (