fix: improve active state logic for navigation buttons

This commit is contained in:
codytseng
2025-08-28 21:48:36 +08:00
parent 3dd0ecd970
commit 3878b84f4c
4 changed files with 17 additions and 12 deletions

View File

@@ -9,32 +9,31 @@ import { useMemo } from 'react'
import BottomNavigationBarItem from './BottomNavigationBarItem'
export default function AccountButton() {
const { navigate, current } = usePrimaryPage()
const { navigate, current, display } = usePrimaryPage()
const { pubkey, profile } = useNostr()
const defaultAvatar = useMemo(
() => (profile?.pubkey ? generateImageByPubkey(profile.pubkey) : ''),
[profile]
)
const active = useMemo(() => current === 'me' && display, [display, current])
return (
<BottomNavigationBarItem
onClick={() => {
navigate('me')
}}
active={current === 'me'}
active={active}
>
{pubkey ? (
profile ? (
<Avatar className={cn('w-7 h-7', current === 'me' ? 'ring-primary ring-1' : '')}>
<Avatar className={cn('w-7 h-7', active ? 'ring-primary ring-1' : '')}>
<AvatarImage src={profile.avatar} className="object-cover object-center" />
<AvatarFallback>
<img src={defaultAvatar} />
</AvatarFallback>
</Avatar>
) : (
<Skeleton
className={cn('w-7 h-7 rounded-full', current === 'me' ? 'ring-primary ring-1' : '')}
/>
<Skeleton className={cn('w-7 h-7 rounded-full', active ? 'ring-primary ring-1' : '')} />
)
) : (
<UserRound />

View File

@@ -3,10 +3,13 @@ import { Compass } from 'lucide-react'
import BottomNavigationBarItem from './BottomNavigationBarItem'
export default function ExploreButton() {
const { navigate, current } = usePrimaryPage()
const { navigate, current, display } = usePrimaryPage()
return (
<BottomNavigationBarItem active={current === 'explore'} onClick={() => navigate('explore')}>
<BottomNavigationBarItem
active={current === 'explore' && display}
onClick={() => navigate('explore')}
>
<Compass />
</BottomNavigationBarItem>
)

View File

@@ -3,10 +3,13 @@ import { Home } from 'lucide-react'
import BottomNavigationBarItem from './BottomNavigationBarItem'
export default function HomeButton() {
const { navigate, current } = usePrimaryPage()
const { navigate, current, display } = usePrimaryPage()
return (
<BottomNavigationBarItem active={current === 'home'} onClick={() => navigate('home')}>
<BottomNavigationBarItem
active={current === 'home' && display}
onClick={() => navigate('home')}
>
<Home />
</BottomNavigationBarItem>
)

View File

@@ -6,12 +6,12 @@ import BottomNavigationBarItem from './BottomNavigationBarItem'
export default function NotificationsButton() {
const { checkLogin } = useNostr()
const { navigate, current } = usePrimaryPage()
const { navigate, current, display } = usePrimaryPage()
const { hasNewNotification } = useNotification()
return (
<BottomNavigationBarItem
active={current === 'notifications'}
active={current === 'notifications' && display}
onClick={() => checkLogin(() => navigate('notifications'))}
>
<div className="relative">