mirror of
https://github.com/nicetry247/offlineacademy.git
synced 2026-07-26 11:58:44 +00:00
perf: make splash non-blocking and session-aware
This commit is contained in:
+27
-6
@@ -5,23 +5,44 @@ import { ThemeProvider } from '@/components/ThemeToggle'
|
||||
import { ToastProvider } from '@/hooks/use-toast'
|
||||
import { SplashScreen } from '@/components/SplashScreen'
|
||||
|
||||
const SPLASH_SESSION_KEY = 'offlineacademy:splash-seen'
|
||||
|
||||
export function Providers({ children }: { children: React.ReactNode }) {
|
||||
const [showSplash, setShowSplash] = React.useState(true)
|
||||
|
||||
// Safety fallback: force hide after 5 seconds max
|
||||
React.useLayoutEffect(() => {
|
||||
try {
|
||||
if (sessionStorage.getItem(SPLASH_SESSION_KEY) === 'true') {
|
||||
setShowSplash(false)
|
||||
}
|
||||
} catch {
|
||||
setShowSplash(false)
|
||||
}
|
||||
}, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
const timer = setTimeout(() => setShowSplash(false), 5000)
|
||||
return () => clearTimeout(timer)
|
||||
if (!showSplash) return
|
||||
const timer = window.setTimeout(() => setShowSplash(false), 2500)
|
||||
return () => window.clearTimeout(timer)
|
||||
}, [showSplash])
|
||||
|
||||
const completeSplash = React.useCallback(() => {
|
||||
try {
|
||||
sessionStorage.setItem(SPLASH_SESSION_KEY, 'true')
|
||||
} catch {
|
||||
// Storage can be unavailable in hardened/private browser modes.
|
||||
}
|
||||
setShowSplash(false)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<ThemeProvider>
|
||||
<ToastProvider>
|
||||
{children}
|
||||
{showSplash && (
|
||||
<SplashScreen onComplete={() => setShowSplash(false)} minDuration={1500} />
|
||||
<SplashScreen onComplete={completeSplash} minDuration={550} />
|
||||
)}
|
||||
{!showSplash && children}
|
||||
</ToastProvider>
|
||||
</ThemeProvider>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user