Skip to content

Commit c953e8b

Browse files
fix(navbar): prevent flicker during session resolution by deferring auth UI and showing skeleton while loading.
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 3f7f27a commit c953e8b

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

web/src/components/navbar/navbar.tsx

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { cn } from '@/lib/utils'
1717
import { UserDropdown } from './user-dropdown'
1818
import { Icons } from '../icons'
1919
import { Button } from '../ui/button'
20+
import { Skeleton } from '../ui/skeleton'
2021
import {
2122
DropdownMenu,
2223
DropdownMenuContent,
@@ -27,9 +28,6 @@ import {
2728
export const Navbar = () => {
2829
const { data: session, status } = useSession()
2930

30-
// Don't render auth-dependent content during loading to prevent hydration mismatch
31-
const isSessionReady = status !== 'loading'
32-
3331
return (
3432
<header className="container mx-auto p-4 flex justify-between items-center relative z-10">
3533
<Link
@@ -76,7 +74,7 @@ export const Navbar = () => {
7674
</Link>
7775

7876
{/* Only show Usage link when session is ready and user is authenticated */}
79-
{isSessionReady && session && (
77+
{status !== 'loading' && session && (
8078
<Link
8179
href="/usage"
8280
className="hover:text-blue-400 transition-colors font-medium px-2 py-1 rounded-md hover:bg-blue-50 dark:hover:bg-blue-900/20"
@@ -125,15 +123,15 @@ export const Navbar = () => {
125123
</DropdownMenuItem>
126124

127125
{/* Only show Usage and Login links when session is ready */}
128-
{isSessionReady && session && (
126+
{status !== 'loading' && session && (
129127
<DropdownMenuItem asChild>
130128
<Link href="/usage" className="flex items-center">
131129
<BarChart2 className="mr-2 h-4 w-4" />
132130
Usage
133131
</Link>
134132
</DropdownMenuItem>
135133
)}
136-
{isSessionReady && !session && (
134+
{status !== 'loading' && !session && (
137135
<DropdownMenuItem asChild>
138136
<Link href="/login" className="flex items-center">
139137
<LogIn className="mr-2 h-4 w-4" />
@@ -144,29 +142,29 @@ export const Navbar = () => {
144142
</DropdownMenuContent>
145143
</DropdownMenu>
146144

147-
{/* Authentication section - only render when session is ready */}
148-
{isSessionReady &&
149-
(session ? (
150-
<UserDropdown session={session} />
151-
) : (
152-
<Link
153-
href="/login"
154-
className="hidden md:inline-block relative group"
145+
{/* Authentication section with loading state */}
146+
{status === 'loading' ? (
147+
<div className="hidden md:flex items-center">
148+
<Skeleton className="h-9 w-16 rounded-md" />
149+
</div>
150+
) : session ? (
151+
<UserDropdown session={session} />
152+
) : (
153+
<Link href="/login" className="hidden md:inline-block relative group">
154+
<div className="absolute inset-0 bg-[rgb(255,110,11)] translate-x-0.5 -translate-y-0.5" />
155+
<Button
156+
className={cn(
157+
'relative',
158+
'bg-white text-black hover:bg-white',
159+
'border border-white/50',
160+
'transition-all duration-300',
161+
'group-hover:-translate-x-0.5 group-hover:translate-y-0.5'
162+
)}
155163
>
156-
<div className="absolute inset-0 bg-[rgb(255,110,11)] translate-x-0.5 -translate-y-0.5" />
157-
<Button
158-
className={cn(
159-
'relative',
160-
'bg-white text-black hover:bg-white',
161-
'border border-white/50',
162-
'transition-all duration-300',
163-
'group-hover:-translate-x-0.5 group-hover:translate-y-0.5'
164-
)}
165-
>
166-
Log in
167-
</Button>
168-
</Link>
169-
))}
164+
Log in
165+
</Button>
166+
</Link>
167+
)}
170168
{/* <ThemeSwitcher /> */}
171169
</div>
172170
</header>

0 commit comments

Comments
 (0)