Skip to content

Commit 983581b

Browse files
committed
v2.461 - Fix Glitch 2
1 parent 27b3671 commit 983581b

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

frontend/src/components/app/AppShell.tsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"use client"
22

33
import { lazy, Suspense, useCallback, useEffect, useRef, useState } from "react"
4+
5+
/** Delay before showing unauthenticated overlay/redirect so auth persistence can restore (avoids sidebar flash). */
6+
const AUTH_SETTLE_MS = 220
47
import { usePathname } from "next/navigation"
58
import { Sidebar } from "@/components/app/Sidebar"
69
import { ErrorBoundary } from "@/components/app/ErrorBoundary"
@@ -34,11 +37,29 @@ function readCachedOnboardingSnapshot(): { required: boolean; memory: Record<str
3437
function AuthGate({ children }: { children: React.ReactNode }) {
3538
const COLORS = useThemeColors()
3639
const { user, loading } = useAuth()
40+
const [showUnauthOverlay, setShowUnauthOverlay] = useState(false)
41+
const settleTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
3742

3843
useEffect(() => {
39-
if (!loading && !user) {
44+
if (loading || user) {
45+
if (settleTimerRef.current) {
46+
clearTimeout(settleTimerRef.current)
47+
settleTimerRef.current = null
48+
}
49+
setShowUnauthOverlay(false)
50+
return
51+
}
52+
settleTimerRef.current = setTimeout(() => {
53+
settleTimerRef.current = null
54+
setShowUnauthOverlay(true)
4055
try { sessionStorage.setItem("fp-auth-redirect", window.location.pathname) } catch {}
4156
window.location.href = "/"
57+
}, AUTH_SETTLE_MS)
58+
return () => {
59+
if (settleTimerRef.current) {
60+
clearTimeout(settleTimerRef.current)
61+
settleTimerRef.current = null
62+
}
4263
}
4364
}, [loading, user])
4465

@@ -67,13 +88,13 @@ function AuthGate({ children }: { children: React.ReactNode }) {
6788
</div>
6889
)
6990

70-
// Always render the shell (sidebar, main, topbar, input) so it stays in the DOM and cached
71-
// no unmount on auth loading, so no black flash or sidebar disappearing when switching pages.
72-
// Only overlay the redirect message when we're sure the user is not authenticated.
91+
// Always render the shell (sidebar, main, topbar, input) so it stays visible and cached.
92+
// Only show redirect overlay after auth has "settled" as unauthenticated (short delay
93+
// so Firebase persistence can restore the session and we don't flash-hide the sidebar).
7394
return (
7495
<>
7596
{children}
76-
{!loading && !user ? renderGateStatus("Redirecting to sign-in...") : null}
97+
{showUnauthOverlay ? renderGateStatus("Redirecting to sign-in...") : null}
7798
</>
7899
)
79100
}

0 commit comments

Comments
 (0)