|
| 1 | +import { languages, SupportedLanguage } from "@/lib/i18n/locales"; |
| 2 | +import { |
| 3 | + DropdownMenu, |
| 4 | + DropdownMenuContent, |
| 5 | + DropdownMenuItem, |
| 6 | + DropdownMenuLabel, |
| 7 | + DropdownMenuPortal, |
| 8 | + DropdownMenuSeparator, |
| 9 | + DropdownMenuSub, |
| 10 | + DropdownMenuSubContent, |
| 11 | + DropdownMenuSubTrigger, |
| 12 | + DropdownMenuTrigger, |
| 13 | +} from "../ui/dropdown-menu"; |
| 14 | +import { useState } from "react"; |
| 15 | +import i18n from "@/lib/i18n/i18n"; |
| 16 | +import { useUserContext } from "@/context/user-context"; |
| 17 | +import { ScrollArea } from "../ui/scroll-area"; |
| 18 | +import { useTheme } from "../providers/theme-provider"; |
| 19 | +import { |
| 20 | + Check, |
| 21 | + DoorOpenIcon, |
| 22 | + Languages, |
| 23 | + Monitor, |
| 24 | + Moon, |
| 25 | + Palette, |
| 26 | + Settings, |
| 27 | + Sun, |
| 28 | +} from "lucide-react"; |
| 29 | +import { useTranslation } from "react-i18next"; |
| 30 | +import { useLocation } from "react-router"; |
| 31 | +import { useRef } from "react"; |
| 32 | +import { |
| 33 | + useScreenParams, |
| 34 | + recompileScreenParams, |
| 35 | +} from "@/lib/hooks/screen-params"; |
| 36 | +import { useMutation } from "@tanstack/react-query"; |
| 37 | +import axios from "axios"; |
| 38 | +import { toast } from "sonner"; |
| 39 | +import { useEffect } from "react"; |
| 40 | + |
| 41 | +function Avatar({ initial }: { initial: string }) { |
| 42 | + return ( |
| 43 | + <span className="group relative grid size-10 place-items-center rounded-full"> |
| 44 | + <span className="absolute inset-0 overflow-hidden rounded-full bg-linear-to-b from-neutral-50 to-neutral-100 dark:from-neutral-700 dark:to-neutral-950 shadow-lg"></span> |
| 45 | + <span className="relative text-sm font-semibold text-primary"> |
| 46 | + {initial} |
| 47 | + </span> |
| 48 | + </span> |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +export const QuickActions = () => { |
| 53 | + const { auth } = useUserContext(); |
| 54 | + const { theme, setTheme } = useTheme(); |
| 55 | + const { t } = useTranslation(); |
| 56 | + const { search } = useLocation(); |
| 57 | + |
| 58 | + const [language, setLanguage] = useState<SupportedLanguage>( |
| 59 | + i18n.language as SupportedLanguage, |
| 60 | + ); |
| 61 | + |
| 62 | + const redirectTimer = useRef<number | null>(null); |
| 63 | + const searchParams = new URLSearchParams(search); |
| 64 | + const screenParams = useScreenParams(searchParams); |
| 65 | + const compiledParams = recompileScreenParams(screenParams); |
| 66 | + |
| 67 | + const logoutMutation = useMutation({ |
| 68 | + mutationFn: () => axios.post("/api/user/logout"), |
| 69 | + mutationKey: ["logout"], |
| 70 | + onSuccess: () => { |
| 71 | + toast.success(t("logoutSuccessTitle"), { |
| 72 | + description: t("logoutSuccessSubtitle"), |
| 73 | + }); |
| 74 | + |
| 75 | + redirectTimer.current = window.setTimeout(() => { |
| 76 | + window.location.replace(`/login${compiledParams}`); |
| 77 | + }, 500); |
| 78 | + }, |
| 79 | + onError: () => { |
| 80 | + toast.error(t("logoutFailTitle"), { |
| 81 | + description: t("logoutFailSubtitle"), |
| 82 | + }); |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + useEffect(() => { |
| 87 | + return () => { |
| 88 | + if (redirectTimer.current) { |
| 89 | + clearTimeout(redirectTimer.current); |
| 90 | + } |
| 91 | + }; |
| 92 | + }, [redirectTimer]); |
| 93 | + |
| 94 | + const initial = auth.authenticated |
| 95 | + ? (auth.name[0] || "U").toUpperCase() |
| 96 | + : null; |
| 97 | + |
| 98 | + const handleSelect = (option: string) => { |
| 99 | + setLanguage(option as SupportedLanguage); |
| 100 | + i18n.changeLanguage(option as SupportedLanguage); |
| 101 | + }; |
| 102 | + |
| 103 | + const themes = [ |
| 104 | + { key: "light", label: t("quickActionsThemeLight"), icon: Sun }, |
| 105 | + { key: "dark", label: t("quickActionsThemeDark"), icon: Moon }, |
| 106 | + { key: "system", label: t("quickActionsThemeSystem"), icon: Monitor }, |
| 107 | + ] as const; |
| 108 | + |
| 109 | + return ( |
| 110 | + <DropdownMenu> |
| 111 | + <DropdownMenuTrigger asChild> |
| 112 | + <button |
| 113 | + aria-label={t("quickActionsTitle")} |
| 114 | + className="rounded-full transition-transform duration-200 will-change-transform hover:scale-105 hover:cursor-pointer focus:ring-0 focus:outline-3 focus:outline-ring/50" |
| 115 | + > |
| 116 | + {auth.authenticated ? ( |
| 117 | + <Avatar initial={initial!} /> |
| 118 | + ) : ( |
| 119 | + <span className="bg-card text-primary border-border size-10 flex items-center justify-center rounded-full border shadow-lg"> |
| 120 | + <Settings className="size-4" /> |
| 121 | + </span> |
| 122 | + )} |
| 123 | + </button> |
| 124 | + </DropdownMenuTrigger> |
| 125 | + |
| 126 | + <DropdownMenuContent |
| 127 | + align="end" |
| 128 | + sideOffset={8} |
| 129 | + className="rounded-xl p-1" |
| 130 | + > |
| 131 | + {auth.authenticated && ( |
| 132 | + <> |
| 133 | + <DropdownMenuLabel className="flex items-center gap-3 p-2"> |
| 134 | + <div className="bg-foreground text-background flex size-9 shrink-0 items-center justify-center rounded-full text-sm font-medium"> |
| 135 | + {initial} |
| 136 | + </div> |
| 137 | + <div className="flex min-w-0 flex-col"> |
| 138 | + <span className="truncate text-sm font-medium"> |
| 139 | + {auth.name} |
| 140 | + </span> |
| 141 | + <span className="text-muted-foreground truncate text-xs font-normal"> |
| 142 | + {auth.email} |
| 143 | + </span> |
| 144 | + </div> |
| 145 | + </DropdownMenuLabel> |
| 146 | + |
| 147 | + <DropdownMenuSeparator /> |
| 148 | + </> |
| 149 | + )} |
| 150 | + |
| 151 | + <DropdownMenuSub> |
| 152 | + <DropdownMenuSubTrigger> |
| 153 | + <Languages className="size-4" /> |
| 154 | + {t("quickActionsLanguage")} |
| 155 | + </DropdownMenuSubTrigger> |
| 156 | + <DropdownMenuPortal> |
| 157 | + <DropdownMenuSubContent sideOffset={8} className="rounded-xl p-1"> |
| 158 | + <ScrollArea className="h-80"> |
| 159 | + {Object.entries(languages).map(([key, value]) => ( |
| 160 | + <DropdownMenuItem |
| 161 | + key={key} |
| 162 | + onSelect={() => handleSelect(key)} |
| 163 | + > |
| 164 | + {value} |
| 165 | + {language === key && <Check className="size-4" />} |
| 166 | + </DropdownMenuItem> |
| 167 | + ))} |
| 168 | + </ScrollArea> |
| 169 | + </DropdownMenuSubContent> |
| 170 | + </DropdownMenuPortal> |
| 171 | + </DropdownMenuSub> |
| 172 | + |
| 173 | + <DropdownMenuSub> |
| 174 | + <DropdownMenuSubTrigger> |
| 175 | + <Palette className="size-4" /> |
| 176 | + {t("quickActionsTheme")} |
| 177 | + </DropdownMenuSubTrigger> |
| 178 | + <DropdownMenuPortal> |
| 179 | + <DropdownMenuSubContent className="rounded-xl p-1" sideOffset={8}> |
| 180 | + {themes.map(({ key, label, icon: Icon }) => ( |
| 181 | + <DropdownMenuItem key={key} onClick={() => setTheme(key)}> |
| 182 | + <span className="flex items-center gap-2"> |
| 183 | + <Icon className="size-4" /> |
| 184 | + {label} |
| 185 | + </span> |
| 186 | + {theme === key && <Check className="size-4" />} |
| 187 | + </DropdownMenuItem> |
| 188 | + ))} |
| 189 | + </DropdownMenuSubContent> |
| 190 | + </DropdownMenuPortal> |
| 191 | + </DropdownMenuSub> |
| 192 | + |
| 193 | + {auth.authenticated && ( |
| 194 | + <> |
| 195 | + <DropdownMenuSeparator /> |
| 196 | + <DropdownMenuItem |
| 197 | + onSelect={() => logoutMutation.mutate()} |
| 198 | + className="text-destructive" |
| 199 | + > |
| 200 | + <DoorOpenIcon className="size-4" /> |
| 201 | + {t("quickActionsLogout")} |
| 202 | + </DropdownMenuItem> |
| 203 | + </> |
| 204 | + )} |
| 205 | + </DropdownMenuContent> |
| 206 | + </DropdownMenu> |
| 207 | + ); |
| 208 | +}; |
0 commit comments