"use client"; import { useEffect, useState, useSyncExternalStore, } from "react"; import { loadingBus } from "@/lib/loadingBus"; import LoadingCurtain from "@/components/canvas/LoadingCurtain"; /** Stable server snapshot (must be cached — a fresh object would loop). */ const SERVER_SNAPSHOT = { progress: 0, ready: false }; function useHeavyLoad() { return useSyncExternalStore( loadingBus.subscribe, loadingBus.getSnapshot, () => SERVER_SNAPSHOT ); } function useFontsReady() { const [fontsReady, setFontsReady] = useState(false); useEffect(() => { let cancelled = false; const fonts = (document as any).fonts; if (!fonts) { setFontsReady(true); return; } fonts.ready.then(() => { if (!cancelled) setFontsReady(true); }); return () => { cancelled = true; }; }, []); return fontsReady; } const clamp = (v: number, min: number, max: number) => Math.min(max, Math.max(min, v)); /** * Heavy-asset readiness for the camera intro dolly (original cV's `p`: * models + image layers ready — fonts are NOT part of this gate). */ export function useAppReady(): boolean { const heavy = useHeavyLoad(); // Safety valve: a stalled asset must never soft-lock the page. useEffect(() => { const safety = window.setTimeout(() => loadingBus.forceReady(), 6000); return () => window.clearTimeout(safety); }, []); return heavy.ready; } /** * Entry loading flow — port of the original route-transition controller * (module 34841) for the entry case on "/": * * 1. entry:wait — curtain fully closed (initialProgress 1), centered * 140px bar shows 50%·fontsReady + 50%·heavyLoadProgress. * 2. readyToLoadHeavy && fontsReady → hold 250ms → bar fades (250ms) * → 250ms later the curtain opens (0.8s customCubic radial hole + * dot mask) — reveal phase. * 3. Curtain reveal completes (+0.9s grace) → idle: unmount everything. */ export function LoadingOverlay() { const heavy = useHeavyLoad(); const fontsReady = useFontsReady(); // wait → fading → reveal → (unmount when reveal completes) const [phase, setPhase] = useState<"wait" | "fading" | "reveal">("wait"); const [mounted, setMounted] = useState(true); const progress = Math.round(clamp(50 * +fontsReady + heavy.progress / 2, 0, 100)); const assetsDone = heavy.ready && fontsReady; useEffect(() => { if (!assetsDone || phase !== "wait") return; // Original: 250ms hold → fade the bar → 250ms later open the curtain. const t1 = window.setTimeout(() => setPhase("fading"), 250); return () => window.clearTimeout(t1); }, [assetsDone, phase]); useEffect(() => { if (phase !== "fading") return; const t2 = window.setTimeout(() => setPhase("reveal"), 250); return () => window.clearTimeout(t2); }, [phase]); if (!mounted) return null; return ( <> { if (open) { // Reveal finished — a beat later the curtain canvas unmounts. window.setTimeout(() => setMounted(false), 900); } }} /> {phase !== "reveal" && ( )} ); } /** Centered 140px progress bar (original `b`). */ function ProgressBar({ progress, fading }: { progress: number; fading: boolean }) { const clamped = clamp(progress, 0, 100); return (