Become a Pro / website · becomeapro.ai / src / components / user-menu.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import Link from "next/link"; |
| 4 | import { Bookmark, LogOut, User as UserIcon } from "lucide-react"; |
| 5 | import { |
| 6 | DropdownMenu, |
| 7 | DropdownMenuContent, |
| 8 | DropdownMenuItem, |
| 9 | DropdownMenuLabel, |
| 10 | DropdownMenuSeparator, |
| 11 | DropdownMenuTrigger, |
| 12 | } from "@/components/ui/dropdown-menu"; |
| 13 | import { displayName } from "@/lib/display-name"; |
| 14 | import { authClient } from "@/lib/auth-client"; |
| 15 | import { useUser } from "@/lib/use-user"; |
| 16 | |
| 17 | export function UserMenu() { |
| 18 | const { user, loading } = useUser(); |
| 19 | |
| 20 | if (loading) return <div className="h-9 w-16" />; |
| 21 | |
| 22 | if (!user) { |
| 23 | return ( |
| 24 | <Link href="/login" className="text-[16px] underline"> |
| 25 | Sign in |
| 26 | </Link> |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | const name = displayName(user); |
| 31 | const avatar = user.image; |
| 32 | |
| 33 | const signOut = async () => { |
| 34 | await authClient.signOut(); |
| 35 | window.location.href = "/"; |
| 36 | }; |
| 37 | |
| 38 | return ( |
| 39 | <DropdownMenu> |
| 40 | <DropdownMenuTrigger |
| 41 | className="flex size-[40px] items-center justify-center overflow-hidden rounded-full border border-foreground/40 text-sm font-semibold" |
| 42 | aria-label="Account menu" |
| 43 | > |
| 44 | {avatar ? ( |
| 45 | // eslint-disable-next-line @next/next/no-img-element |
| 46 | <img src={avatar} alt="" className="h-full w-full object-cover" referrerPolicy="no-referrer" /> |
| 47 | ) : ( |
| 48 | name[0]?.toUpperCase() |
| 49 | )} |
| 50 | </DropdownMenuTrigger> |
| 51 | <DropdownMenuContent align="end" className="w-56"> |
| 52 | <DropdownMenuLabel className="truncate font-normal text-muted-foreground">{name}</DropdownMenuLabel> |
| 53 | <DropdownMenuSeparator /> |
| 54 | <DropdownMenuItem asChild> |
| 55 | <Link href="/account"> |
| 56 | <UserIcon className="mr-2 h-4 w-4" /> Account |
| 57 | </Link> |
| 58 | </DropdownMenuItem> |
| 59 | <DropdownMenuItem asChild> |
| 60 | <Link href="/account#saved"> |
| 61 | <Bookmark className="mr-2 h-4 w-4" /> Saved lessons |
| 62 | </Link> |
| 63 | </DropdownMenuItem> |
| 64 | <DropdownMenuSeparator /> |
| 65 | <DropdownMenuItem onSelect={signOut}> |
| 66 | <LogOut className="mr-2 h-4 w-4" /> Sign out |
| 67 | </DropdownMenuItem> |
| 68 | </DropdownMenuContent> |
| 69 | </DropdownMenu> |
| 70 | ); |
| 71 | } |