实现个人中心下拉菜单;更新部分 eslint 规则
This commit is contained in:
74
src/components/composites/user-center/index.tsx
Normal file
74
src/components/composites/user-center/index.tsx
Normal file
@@ -0,0 +1,74 @@
|
||||
'use client'
|
||||
import {useProfileStore} from '@/components/providers/StoreProvider'
|
||||
import {Button} from '@/components/ui/button'
|
||||
import {DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger} from '@/components/ui/dropdown-menu'
|
||||
import {Avatar, AvatarFallback, AvatarImage} from '@/components/ui/avatar'
|
||||
import {LogOutIcon, UserIcon, UserPenIcon} from 'lucide-react'
|
||||
import {usePathname, useRouter} from 'next/navigation'
|
||||
import {logout} from '@/actions/auth'
|
||||
import {useState} from 'react'
|
||||
import {HoverCard, HoverCardContent, HoverCardTrigger} from '@/components/ui/hover-card'
|
||||
|
||||
export default function UserCenter() {
|
||||
const router = useRouter()
|
||||
|
||||
// 登录控制
|
||||
const profile = useProfileStore(store => store.profile)
|
||||
const refreshProfile = useProfileStore(store => store.refreshProfile)
|
||||
const doLogout = async () => {
|
||||
const resp = await logout()
|
||||
if (resp.success) {
|
||||
refreshProfile().then()
|
||||
router.replace('/')
|
||||
}
|
||||
}
|
||||
|
||||
// 展示与跳转
|
||||
const pathname = usePathname()
|
||||
const toAdminPage = () => {
|
||||
if (!pathname.startsWith('/admin')) {
|
||||
router.push('/admin')
|
||||
}
|
||||
}
|
||||
|
||||
return !profile ? (
|
||||
<Button
|
||||
theme="fail"
|
||||
onClick={() => router.push('/login')}>
|
||||
去登录
|
||||
</Button>
|
||||
) : (
|
||||
<HoverCard openDelay={150} closeDelay={150}>
|
||||
<HoverCardTrigger>
|
||||
<Button
|
||||
theme="ghost"
|
||||
className="flex gap-2 items-center h-12"
|
||||
onClick={toAdminPage}
|
||||
>
|
||||
<Avatar>
|
||||
<AvatarImage src={profile.avatar} alt="avatar"/>
|
||||
<AvatarFallback className="bg-primary-muted"><UserIcon/></AvatarFallback>
|
||||
</Avatar>
|
||||
<span>{profile.name}</span>
|
||||
</Button>
|
||||
</HoverCardTrigger>
|
||||
<HoverCardContent className="w-40 p-1" align="end">
|
||||
<Button
|
||||
theme="ghost"
|
||||
className="w-full justify-start"
|
||||
onClick={() => router.push('/admin/profile')}>
|
||||
<UserPenIcon/>
|
||||
个人中心
|
||||
</Button>
|
||||
<Button
|
||||
theme="ghost"
|
||||
color="fail"
|
||||
className="w-full justify-start"
|
||||
onClick={doLogout}>
|
||||
<LogOutIcon/>
|
||||
退出登录
|
||||
</Button>
|
||||
</HoverCardContent>
|
||||
</HoverCard>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user