user-menu.tsx104 lines, 3.2 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { signOut } from "@/lib/auth-client"; |
| 4 | import { useRouter } from "next/navigation"; |
| 5 | import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; |
| 6 | import { Button } from "@/components/ui/button"; |
| 7 | import { |
| 8 | DropdownMenu, |
| 9 | DropdownMenuContent, |
| 10 | DropdownMenuItem, |
| 11 | DropdownMenuLabel, |
| 12 | DropdownMenuSeparator, |
| 13 | DropdownMenuTrigger, |
| 14 | } from "@/components/ui/dropdown-menu"; |
| 15 | import { LogOut, Settings, User, ChevronDown } from "lucide-react"; |
| 16 | import { use } from "react"; |
| 17 | |
| 18 | interface UserProps { |
| 19 | id: string; |
| 20 | name: string; |
| 21 | email: string; |
| 22 | image?: string | null | undefined; |
| 23 | } |
| 24 | |
| 25 | export function UserMenu({ user }: { user: UserProps }) { |
| 26 | const router = useRouter(); |
| 27 | |
| 28 | const handleSignOut = async () => { |
| 29 | await signOut(); |
| 30 | router.push("/"); |
| 31 | }; |
| 32 | |
| 33 | const initials = user.name |
| 34 | ? user.name |
| 35 | .split(" ") |
| 36 | .map((n) => n[0]) |
| 37 | .join("") |
| 38 | .toUpperCase() |
| 39 | .slice(0, 2) |
| 40 | : (user.email[0].toUpperCase() ?? "U"); |
| 41 | |
| 42 | return ( |
| 43 | <DropdownMenu> |
| 44 | <DropdownMenuTrigger asChild> |
| 45 | <Button variant={"ghost"} className="h-9 gap-2 px-2 hover:bg-muted/80"> |
| 46 | <Avatar className="size-7 ring-1 ring-border"> |
| 47 | <AvatarImage |
| 48 | src={user.image ?? undefined} |
| 49 | alt={user.name ?? "User"} |
| 50 | /> |
| 51 | <AvatarFallback className="text-sm font-medium bg-primary/10 text-primary"> |
| 52 | {initials} |
| 53 | </AvatarFallback> |
| 54 | </Avatar> |
| 55 | <span className="hidden sm:inline-block text-sm font-medium max-w-25 truncate"> |
| 56 | {user.name ?? "User"} |
| 57 | </span> |
| 58 | <ChevronDown className="size-3.5 text-muted-foreground" /> |
| 59 | </Button> |
| 60 | </DropdownMenuTrigger> |
| 61 | |
| 62 | <DropdownMenuContent align="end" className="w-64"> |
| 63 | <div className="px-3 py-3"> |
| 64 | <div className="flex items-center gap-3"> |
| 65 | <Avatar className="size-10 ring-1 ring-border"> |
| 66 | <AvatarImage |
| 67 | src={user.image ?? undefined} |
| 68 | alt={user.name ?? "User"} |
| 69 | /> |
| 70 | <AvatarFallback className="text-sm font-medium bg-primary/10 text-primary"> |
| 71 | {initials} |
| 72 | </AvatarFallback> |
| 73 | </Avatar> |
| 74 | <div className="flex flex-col min-w-0"> |
| 75 | <span className="text-sm font-medium truncate"> |
| 76 | {user.name ?? "User"} |
| 77 | </span> |
| 78 | <span className="text-xs text-muted-foreground truncate"> |
| 79 | {user.email ?? "Email"} |
| 80 | </span> |
| 81 | </div> |
| 82 | </div> |
| 83 | </div> |
| 84 | <DropdownMenuSeparator /> |
| 85 | <DropdownMenuItem className="gap-2 py-2 cursor-pointer" disabled> |
| 86 | <User className="size-4" /> |
| 87 | Profile |
| 88 | </DropdownMenuItem> |
| 89 | <DropdownMenuItem className="gap-2 py-2 cursor-pointer" disabled> |
| 90 | <Settings className="size-4" /> |
| 91 | Settings |
| 92 | </DropdownMenuItem> |
| 93 | <DropdownMenuSeparator /> |
| 94 | <DropdownMenuItem |
| 95 | onClick={handleSignOut} |
| 96 | className="gap-2 py-2 cursor-pointer text-destructive focus:text-destructive focus:bg-destructive/10" |
| 97 | > |
| 98 | <LogOut className="size-4" /> |
| 99 | Log Out |
| 100 | </DropdownMenuItem> |
| 101 | </DropdownMenuContent> |
| 102 | </DropdownMenu> |
| 103 | ); |
| 104 | } |