|
| 1 | +import { Link } from '@inertiajs/react'; |
| 2 | +import { SignOut, CaretUpDown } from "@phosphor-icons/react"; |
| 3 | +import { |
| 4 | + DropdownMenu, |
| 5 | + DropdownMenuContent, |
| 6 | + DropdownMenuItem, |
| 7 | + DropdownMenuTrigger, |
| 8 | +} from "@/components/ui/dropdown-menu"; |
| 9 | +import { Avatar, AvatarFallback } from "@/components/ui/avatar"; |
| 10 | +import { SidebarMenu, SidebarMenuItem, useSidebar } from "@/components/ui/sidebar"; |
| 11 | +import { cn } from "@/lib/utils"; |
| 12 | + |
| 13 | +interface UserNavProps { |
| 14 | + user: { |
| 15 | + name: string; |
| 16 | + email: string; |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function getInitials(name: string): string { |
| 21 | + const names = name.trim().split(' '); |
| 22 | + if (names.length >= 2) { |
| 23 | + return (names[0][0] + names[names.length - 1][0]).toUpperCase(); |
| 24 | + } |
| 25 | + return name.substring(0, 2).toUpperCase(); |
| 26 | +} |
| 27 | + |
| 28 | +export function UserNav({ user }: UserNavProps) { |
| 29 | + const { state } = useSidebar(); |
| 30 | + const initials = getInitials(user.name); |
| 31 | + |
| 32 | + return ( |
| 33 | + <SidebarMenu> |
| 34 | + <SidebarMenuItem> |
| 35 | + <DropdownMenu> |
| 36 | + <DropdownMenuTrigger asChild> |
| 37 | + <button |
| 38 | + className={cn( |
| 39 | + "peer/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm outline-hidden ring-sidebar-ring transition-[width,height,padding]", |
| 40 | + "hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus-visible:ring-2", |
| 41 | + "active:bg-sidebar-accent active:text-sidebar-accent-foreground", |
| 42 | + "data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground", |
| 43 | + "h-12 group-data-[collapsible=icon]:p-0", |
| 44 | + "group-data-[collapsible=icon]:size-8" |
| 45 | + )} |
| 46 | + > |
| 47 | + <Avatar className="size-8 rounded-lg"> |
| 48 | + <AvatarFallback className="rounded-lg"> |
| 49 | + {initials} |
| 50 | + </AvatarFallback> |
| 51 | + </Avatar> |
| 52 | + <div className="grid flex-1 text-left text-sm leading-tight"> |
| 53 | + <span className="truncate font-semibold">{user.name}</span> |
| 54 | + <span className="truncate text-xs">{user.email}</span> |
| 55 | + </div> |
| 56 | + <CaretUpDown className="ml-auto size-4" /> |
| 57 | + </button> |
| 58 | + </DropdownMenuTrigger> |
| 59 | + <DropdownMenuContent |
| 60 | + className="w-56" |
| 61 | + align="end" |
| 62 | + side={state === "collapsed" ? "right" : "top"} |
| 63 | + > |
| 64 | + <DropdownMenuItem asChild> |
| 65 | + <Link |
| 66 | + href={route('logout')} |
| 67 | + method="post" |
| 68 | + as="button" |
| 69 | + className="cursor-pointer w-full" |
| 70 | + > |
| 71 | + <SignOut className="mr-2 size-4" /> |
| 72 | + <span>Log out</span> |
| 73 | + </Link> |
| 74 | + </DropdownMenuItem> |
| 75 | + </DropdownMenuContent> |
| 76 | + </DropdownMenu> |
| 77 | + </SidebarMenuItem> |
| 78 | + </SidebarMenu> |
| 79 | + ); |
| 80 | +} |
0 commit comments