| 1 | "use client"; |
| 2 | |
| 3 | import type { MultiSessionAuthClient } |
| 4 | import { useAuth, useSession |
| 5 | import { useSetActiveSession } |
| 6 | import { |
| 7 | ChevronsUpDown, |
| 8 | LogIn, |
| 9 | LogOut, |
| 10 | Settings, |
| 11 | UserPlus2, |
| 12 | } from "lucide-react"; |
| 13 | import { isValidElement, type |
| 14 | |
| 15 | import { buttonVariants } |
| 16 | import { |
| 17 | DropdownMenu, |
| 18 | DropdownMenuContent, |
| 19 | DropdownMenuGroup, |
| 20 | DropdownMenuItem, |
| 21 | DropdownMenuLabel, |
| 22 | DropdownMenuSeparator, |
| 23 | DropdownMenuTrigger, |
| 24 | } from "@/components/ui/dropdown-menu"; |
| 25 | import { cn } |
| 26 | import { UserAvatar } |
| 27 | import { UserView } |
| 28 | |
| 29 | export type UserButtonLinkVisibility |
| 30 | | "authenticated" |
| 31 | | "unauthenticated" |
| 32 | | "always" |
| 33 | |
| 34 | export type UserButtonLink |
| 35 | label: ReactNode |
| 36 | href: string |
| 37 | icon?: ReactNode |
| 38 | variant?: " |
| 39 | visibility?: UserButtonLinkVisibility |
| 40 | }; |
| 41 | |
| 42 | export type UserButtonProps |
| 43 | className?: string |
| 44 | align?: " |
| 45 | side?: " |
| 46 | sideOffset?: number |
| 47 | size?: " |
| 48 | variant?: |
| 49 | | "default" |
| 50 | | "destructive" |
| 51 | | "ghost" |
| 52 | | "link" |
| 53 | | "outline" |
| 54 | | "secondary" |
| 55 | links?: ( |
| 56 | hideSettings?: boolean |
| 57 | }; |
| 58 | |
| 59 | function renderUserLink( |
| 60 | link: UserButtonLink |
| 61 | navigate: (options |
| 62 | fallbackKey: string, |
| 63 | ): ReactNode { |
| 64 | if (isValidElement(link |
| 65 | |
| 66 | const { label, |
| 67 | return ( |
| 68 | <DropdownMenuItem |
| 69 | key={fallbackKey} |
| 70 | variant={variant} |
| 71 | onClick={() => |
| 72 | > |
| 73 | {icon} |
| 74 | {label} |
| 75 | </DropdownMenuItem> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | export function UserButton({ |
| 80 | className, |
| 81 | align, |
| 82 | side, |
| 83 | sideOffset, |
| 84 | size = "default" |
| 85 | variant = "ghost" |
| 86 | links, |
| 87 | hideSettings = false, |
| 88 | }: UserButtonProps) |
| 89 | const { authClient, |
| 90 | useAuth<MultiSessionAuthClient> |
| 91 | |
| 92 | const { isPending: |
| 93 | const { data: |
| 94 | |
| 95 | const userLinks = |
| 96 | if (!isValidElement(link |
| 97 | const visibility = |
| 98 | if (visibility === |
| 99 | if (visibility === |
| 100 | } |
| 101 | return [ |
| 102 | renderUserLink(link, navigate |
| 103 | ]; |
| 104 | }); |
| 105 | |
| 106 | // Whether anything renders between the user info label and the sign-out item, so the leading separator isn't shown with nothing to separate (see #439). |
| 107 | const hasSessionMenuItems = |
| 108 | (userLinks?.length ?? |
| 109 | !hideSettings || |
| 110 | plugins.some((plugin |
| 111 | |
| 112 | return ( |
| 113 | <DropdownMenu> |
| 114 | <DropdownMenuTrigger |
| 115 | aria-label={size === |
| 116 | className={ |
| 117 | size === "icon" |
| 118 | ? cn("rounded-full" |
| 119 | : cn( |
| 120 | buttonVariants({ variant, |
| 121 | "py-2.5 h-auto font-normal", |
| 122 | className, |
| 123 | ) |
| 124 | } |
| 125 | > |
| 126 | {size === "icon |
| 127 | <UserAvatar /> |
| 128 | ) : ( |
| 129 | <> |
| 130 | {session || sessionPending |
| 131 | <UserView isPending={ |
| 132 | ) : ( |
| 133 | <> |
| 134 | <UserAvatar /> |
| 135 | |
| 136 | <div className="grid flex-1 text-left text-sm leading-tight |
| 137 | {localization.auth.account |
| 138 | </div> |
| 139 | </> |
| 140 | )} |
| 141 | |
| 142 | <ChevronsUpDown className=" |
| 143 | </> |
| 144 | )} |
| 145 | </DropdownMenuTrigger> |
| 146 | |
| 147 | <DropdownMenuContent |
| 148 | className="min-w-40 md:min-w-56 max-w-[48svw]" |
| 149 | side={side} |
| 150 | sideOffset={sideOffset} |
| 151 | align={align} |
| 152 | > |
| 153 | {session && ( |
| 154 | <> |
| 155 | <DropdownMenuGroup> |
| 156 | <DropdownMenuLabel className=" |
| 157 | <UserView /> |
| 158 | </DropdownMenuLabel> |
| 159 | </DropdownMenuGroup> |
| 160 | |
| 161 | {hasSessionMenuItems && <DropdownMenuSeparator |
| 162 | </> |
| 163 | )} |
| 164 | |
| 165 | {session ? ( |
| 166 | <> |
| 167 | {userLinks} |
| 168 | |
| 169 | {!hideSettings && ( |
| 170 | <DropdownMenuItem |
| 171 | onClick={() => |
| 172 | navigate({ |
| 173 | to: `${basePaths. |
| 174 | }) |
| 175 | } |
| 176 | > |
| 177 | <Settings className=" |
| 178 | |
| 179 | {localization.settings.settings |
| 180 | </DropdownMenuItem> |
| 181 | )} |
| 182 | |
| 183 | {plugins.flatMap(( |
| 184 | plugin.userMenuItems?.map |
| 185 | <Item key={ |
| 186 | )), |
| 187 | )} |
| 188 | |
| 189 | <DropdownMenuSeparator /> |
| 190 | |
| 191 | <DropdownMenuItem |
| 192 | onClick={() => |
| 193 | navigate({ |
| 194 | to: `${basePaths. |
| 195 | }) |
| 196 | } |
| 197 | > |
| 198 | <LogOut className=" |
| 199 | |
| 200 | {localization.auth.signOut |
| 201 | </DropdownMenuItem> |
| 202 | </> |
| 203 | ) : ( |
| 204 | <> |
| 205 | {userLinks} |
| 206 | |
| 207 | <DropdownMenuItem |
| 208 | onClick={() => |
| 209 | navigate({ |
| 210 | to: `${basePaths. |
| 211 | }) |
| 212 | } |
| 213 | > |
| 214 | <LogIn className=" |
| 215 | |
| 216 | {localization.auth.signIn |
| 217 | </DropdownMenuItem> |
| 218 | |
| 219 | <DropdownMenuItem |
| 220 | onClick={() => |
| 221 | navigate({ |
| 222 | to: `${basePaths. |
| 223 | }) |
| 224 | } |
| 225 | > |
| 226 | <UserPlus2 className=" |
| 227 | |
| 228 | {localization.auth.signUp |
| 229 | </DropdownMenuItem> |
| 230 | |
| 231 | {plugins.flatMap(( |
| 232 | plugin.userMenuItems?.map |
| 233 | <Item key={ |
| 234 | )), |
| 235 | )} |
| 236 | </> |
| 237 | )} |
| 238 | </DropdownMenuContent> |
| 239 | </DropdownMenu> |
| 240 | ); |
| 241 | } |