|
| 1 | +import { Link as RouterLink } from "@tanstack/react-router" |
| 2 | +import { ChevronsUpDown, LogOut, Settings } from "lucide-react" |
| 3 | + |
| 4 | +import { Avatar, AvatarFallback } from "@/components/ui/avatar" |
| 5 | +import { |
| 6 | + DropdownMenu, |
| 7 | + DropdownMenuContent, |
| 8 | + DropdownMenuItem, |
| 9 | + DropdownMenuLabel, |
| 10 | + DropdownMenuSeparator, |
| 11 | + DropdownMenuTrigger, |
| 12 | +} from "@/components/ui/dropdown-menu" |
| 13 | +import { |
| 14 | + SidebarMenu, |
| 15 | + SidebarMenuButton, |
| 16 | + SidebarMenuItem, |
| 17 | + useSidebar, |
| 18 | +} from "@/components/ui/sidebar" |
| 19 | +import useAuth from "@/hooks/useAuth" |
| 20 | +import { getInitials } from "@/utils" |
| 21 | + |
| 22 | +interface UserInfoProps { |
| 23 | + fullName?: string |
| 24 | + email?: string |
| 25 | +} |
| 26 | + |
| 27 | +function UserInfo({ fullName, email }: UserInfoProps) { |
| 28 | + return ( |
| 29 | + <div className="flex items-center gap-2.5 w-full min-w-0"> |
| 30 | + <Avatar className="size-8"> |
| 31 | + <AvatarFallback className="bg-zinc-600 text-white"> |
| 32 | + {getInitials(fullName || "User")} |
| 33 | + </AvatarFallback> |
| 34 | + </Avatar> |
| 35 | + <div className="flex flex-col items-start min-w-0"> |
| 36 | + <p className="text-sm font-medium truncate w-full">{fullName}</p> |
| 37 | + <p className="text-xs text-muted-foreground truncate w-full">{email}</p> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +export function User({ user }: { user: any }) { |
| 44 | + const { logout } = useAuth() |
| 45 | + const { isMobile, setOpenMobile } = useSidebar() |
| 46 | + |
| 47 | + if (!user) return null |
| 48 | + |
| 49 | + const handleMenuClick = () => { |
| 50 | + if (isMobile) { |
| 51 | + setOpenMobile(false) |
| 52 | + } |
| 53 | + } |
| 54 | + const handleLogout = async () => { |
| 55 | + logout() |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <SidebarMenu> |
| 60 | + <SidebarMenuItem> |
| 61 | + <DropdownMenu> |
| 62 | + <DropdownMenuTrigger asChild> |
| 63 | + <SidebarMenuButton |
| 64 | + size="lg" |
| 65 | + className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground" |
| 66 | + data-testid="user-menu" |
| 67 | + > |
| 68 | + <UserInfo fullName={user?.full_name} email={user?.email} /> |
| 69 | + <ChevronsUpDown className="ml-auto size-4 text-muted-foreground" /> |
| 70 | + </SidebarMenuButton> |
| 71 | + </DropdownMenuTrigger> |
| 72 | + <DropdownMenuContent |
| 73 | + className="w-(--radix-dropdown-menu-trigger-width) min-w-56 rounded-lg" |
| 74 | + side={isMobile ? "bottom" : "right"} |
| 75 | + align="end" |
| 76 | + sideOffset={4} |
| 77 | + > |
| 78 | + <DropdownMenuLabel className="p-0 font-normal"> |
| 79 | + <UserInfo fullName={user?.full_name} email={user?.email} /> |
| 80 | + </DropdownMenuLabel> |
| 81 | + <DropdownMenuSeparator /> |
| 82 | + <RouterLink to="/settings" onClick={handleMenuClick}> |
| 83 | + <DropdownMenuItem> |
| 84 | + <Settings /> |
| 85 | + User Settings |
| 86 | + </DropdownMenuItem> |
| 87 | + </RouterLink> |
| 88 | + <DropdownMenuItem |
| 89 | + onClick={handleLogout} |
| 90 | + > |
| 91 | + <LogOut /> |
| 92 | + Log Out |
| 93 | + </DropdownMenuItem> |
| 94 | + </DropdownMenuContent> |
| 95 | + </DropdownMenu> |
| 96 | + </SidebarMenuItem> |
| 97 | + </SidebarMenu> |
| 98 | + ) |
| 99 | +} |
0 commit comments