|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { authClient } from "@databuddy/auth/client"; |
| 4 | +import { GearIcon, SignOutIcon } from "@phosphor-icons/react"; |
| 5 | +import { useRouter } from "next/navigation"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { toast } from "sonner"; |
| 8 | +import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; |
| 9 | +import { |
| 10 | + DropdownMenu, |
| 11 | + DropdownMenuContent, |
| 12 | + DropdownMenuItem, |
| 13 | + DropdownMenuLabel, |
| 14 | + DropdownMenuSeparator, |
| 15 | + DropdownMenuTrigger, |
| 16 | +} from "@/components/ui/dropdown-menu"; |
| 17 | +import { |
| 18 | + Tooltip, |
| 19 | + TooltipContent, |
| 20 | + TooltipTrigger, |
| 21 | +} from "@/components/ui/tooltip"; |
| 22 | + |
| 23 | +export function ProfileButton() { |
| 24 | + const [isLoggingOut, setIsLoggingOut] = useState(false); |
| 25 | + const [isOpen, setIsOpen] = useState(false); |
| 26 | + const router = useRouter(); |
| 27 | + const { data: session, isPending: isSessionLoading } = |
| 28 | + authClient.useSession(); |
| 29 | + |
| 30 | + const handleLogout = async () => { |
| 31 | + setIsLoggingOut(true); |
| 32 | + setIsOpen(false); |
| 33 | + await authClient.signOut({ |
| 34 | + fetchOptions: { |
| 35 | + onSuccess: () => { |
| 36 | + toast.success("Logged out successfully"); |
| 37 | + router.push("/login"); |
| 38 | + }, |
| 39 | + onError: (error) => { |
| 40 | + router.push("/login"); |
| 41 | + toast.error(error.error.message || "Failed to log out"); |
| 42 | + }, |
| 43 | + }, |
| 44 | + }); |
| 45 | + setIsLoggingOut(false); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleSettings = () => { |
| 49 | + setIsOpen(false); |
| 50 | + router.push("/settings/account"); |
| 51 | + }; |
| 52 | + |
| 53 | + const user = session?.user; |
| 54 | + const userInitials = user?.name |
| 55 | + ? user.name |
| 56 | + .split(" ") |
| 57 | + .map((n) => n[0]) |
| 58 | + .join("") |
| 59 | + .toUpperCase() |
| 60 | + .slice(0, 2) |
| 61 | + : user?.email?.[0]?.toUpperCase() || "U"; |
| 62 | + |
| 63 | + return ( |
| 64 | + <DropdownMenu onOpenChange={setIsOpen} open={isOpen}> |
| 65 | + <Tooltip> |
| 66 | + <TooltipTrigger asChild> |
| 67 | + <DropdownMenuTrigger |
| 68 | + aria-label="Profile menu" |
| 69 | + className="flex size-8 items-center justify-center rounded-full outline-hidden transition-opacity hover:opacity-80 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2" |
| 70 | + disabled={isLoggingOut || isSessionLoading} |
| 71 | + > |
| 72 | + <Avatar className="size-8"> |
| 73 | + <AvatarImage |
| 74 | + alt={user?.name || "User"} |
| 75 | + src={user?.image || undefined} |
| 76 | + /> |
| 77 | + <AvatarFallback className="bg-primary text-primary-foreground text-xs"> |
| 78 | + {userInitials} |
| 79 | + </AvatarFallback> |
| 80 | + </Avatar> |
| 81 | + </DropdownMenuTrigger> |
| 82 | + </TooltipTrigger> |
| 83 | + <TooltipContent side="right"> |
| 84 | + <p>Profile menu</p> |
| 85 | + </TooltipContent> |
| 86 | + </Tooltip> |
| 87 | + |
| 88 | + <DropdownMenuContent align="start" className="w-56" side="right"> |
| 89 | + <DropdownMenuLabel> |
| 90 | + <div className="flex flex-col space-y-1"> |
| 91 | + <p className="font-medium text-sm leading-none">{user?.name}</p> |
| 92 | + <p className="text-muted-foreground text-xs leading-none"> |
| 93 | + {user?.email} |
| 94 | + </p> |
| 95 | + </div> |
| 96 | + </DropdownMenuLabel> |
| 97 | + <DropdownMenuSeparator /> |
| 98 | + <DropdownMenuItem onClick={handleSettings}> |
| 99 | + <GearIcon weight="duotone" /> |
| 100 | + Settings |
| 101 | + </DropdownMenuItem> |
| 102 | + <DropdownMenuSeparator /> |
| 103 | + <DropdownMenuItem |
| 104 | + disabled={isLoggingOut} |
| 105 | + onClick={handleLogout} |
| 106 | + variant="destructive" |
| 107 | + > |
| 108 | + <SignOutIcon weight="duotone" /> |
| 109 | + {isLoggingOut ? "Signing out..." : "Sign out"} |
| 110 | + </DropdownMenuItem> |
| 111 | + </DropdownMenuContent> |
| 112 | + </DropdownMenu> |
| 113 | + ); |
| 114 | +} |
0 commit comments