hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits
| 1 | import { Show, createSignal } from "solid-js"; |
| 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 3 | import type { Locale, MessageKey } from "@/i18n/messages"; |
| 4 | import { RoleBadge } from "@/components/layout/role-badge"; |
| 5 | import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; |
| 6 | import { |
| 7 | DropdownMenu, |
| 8 | DropdownMenuContent, |
| 9 | DropdownMenuItem, |
| 10 | DropdownMenuSeparator, |
| 11 | DropdownMenuSub, |
| 12 | DropdownMenuSubContent, |
| 13 | DropdownMenuSubTrigger, |
| 14 | DropdownMenuTrigger, |
| 15 | } from "@/components/ui/dropdown-menu"; |
| 16 | import { IconChevronDown, IconGlobe, IconLogout, IconMoon, IconSettings, IconSun } from "@/components/ui/icons"; |
| 17 | import { ThemeModeControl } from "@/components/layout/theme-mode-control"; |
| 18 | import { useAuth } from "@/stores/auth-context"; |
| 19 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 20 | import { cn } from "@/lib/cn"; |
| 21 | import { personInitials } from "@/lib/person"; |
| 22 | import type { User } from "@/api/client"; |
| 23 | |
| 24 | function titleCase(value: string) { |
| 25 | return value |
| 26 | .trim() |
| 27 | .split(/\s+/) |
| 28 | .filter(Boolean) |
| 29 | .map((part) => part.charAt(0).toLocaleUpperCase("tr-TR") + part.slice(1)) |
| 30 | .join(" "); |
| 31 | } |
| 32 | |
| 33 | function displayName(user: User) { |
| 34 | const fullName = [user.name, user.surname].filter(Boolean).join(" "); |
| 35 | return titleCase(fullName || user.username); |
| 36 | } |
| 37 | |
| 38 | function ChoiceButton(props: { |
| 39 | active?: boolean; |
| 40 | label: string; |
| 41 | icon: any; |
| 42 | onSelect: () => void; |
| 43 | }) { |
| 44 | return ( |
| 45 | <button |
| 46 | type="button" |
| 47 | role="menuitemradio" |
| 48 | aria-checked={props.active} |
| 49 | class={cn( |
| 50 | "flex min-h-16 w-full flex-col items-center justify-center gap-1.5 rounded-md border px-2 py-2.5 text-center text-xs font-medium transition-colors", |
| 51 | "outline-hidden focus-visible:ring-2 focus-visible:ring-ring", |
| 52 | props.active |
| 53 | ? "border-primary bg-primary text-primary-foreground shadow-xs" |
| 54 | : "border-border bg-background text-foreground hover:bg-accent hover:text-accent-foreground", |
| 55 | )} |
| 56 | onClick={props.onSelect} |
| 57 | > |
| 58 | <span class="flex h-7 w-7 items-center justify-center">{props.icon}</span> |
| 59 | <span class="leading-tight">{props.label}</span> |
| 60 | </button> |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | export function UserMenu() { |
| 65 | const auth = useAuth(); |
| 66 | const prefs = usePreferences(); |
| 67 | const t = useT(); |
| 68 | const navigate = useNavigate(); |
| 69 | const [profileOpen, setProfileOpen] = createSignal(false); |
| 70 | |
| 71 | const onLogout = async () => { |
| 72 | await auth.logout(); |
| 73 | void navigate({ to: "/login" }); |
| 74 | }; |
| 75 | |
| 76 | return ( |
| 77 | <> |
| 78 | <Show when={auth.user()}> |
| 79 | {(u) => ( |
| 80 | <DropdownMenu placement="bottom-end" gutter={8}> |
| 81 | <DropdownMenuTrigger |
| 82 | class={cn( |
| 83 | "inline-flex h-9 max-w-[16rem] items-center gap-2.5 rounded-lg border border-input bg-background px-2.5 pr-3 text-sm shadow-xs outline-hidden transition-colors sm:max-w-88", |
| 84 | "hover:bg-accent hover:text-accent-foreground", |
| 85 | "focus-visible:ring-2 focus-visible:ring-ring", |
| 86 | "data-expanded:bg-accent data-expanded:text-accent-foreground", |
| 87 | )} |
| 88 | aria-label={t("nav.account")} |
| 89 | > |
| 90 | <span class="flex h-8 w-8 shrink-0 items-center justify-center rounded-md bg-primary text-xs font-semibold text-primary-foreground shadow-xs"> |
| 91 | {personInitials(displayName(u()))} |
| 92 | </span> |
| 93 | <span class="hidden min-w-0 flex-col text-left leading-tight sm:flex"> |
| 94 | <span class="truncate font-semibold">{displayName(u())}</span> |
| 95 | <span class="truncate text-[11px] font-medium text-muted-foreground">{t(`role.${u().role}` as MessageKey)}</span> |
| 96 | </span> |
| 97 | <IconChevronDown class="h-4 w-4 shrink-0 opacity-50" /> |
| 98 | </DropdownMenuTrigger> |
| 99 | |
| 100 | <DropdownMenuContent class="w-[min(20rem,calc(100vw-1.5rem))]"> |
| 101 | <div class="flex items-center gap-3 p-3"> |
| 102 | <span class="flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-primary text-sm font-semibold text-primary-foreground shadow-xs"> |
| 103 | {personInitials(displayName(u()))} |
| 104 | </span> |
| 105 | <div class="flex min-w-0 flex-1 items-start justify-between gap-2"> |
| 106 | <div class="min-w-0"> |
| 107 | <p class="truncate text-sm font-semibold leading-tight">{displayName(u())}</p> |
| 108 | <p class="truncate text-xs text-muted-foreground">@{u().username}</p> |
| 109 | </div> |
| 110 | <RoleBadge role={u().role} /> |
| 111 | </div> |
| 112 | </div> |
| 113 | |
| 114 | <DropdownMenuSeparator /> |
| 115 | |
| 116 | {/* Language submenu — centered icon grid */} |
| 117 | <DropdownMenuSub> |
| 118 | <DropdownMenuSubTrigger class="gap-2"> |
| 119 | <IconGlobe class="h-4 w-4 shrink-0" /> |
| 120 | <span class="min-w-0 flex-1 truncate text-left">{t("lang.label")}</span> |
| 121 | <span class="shrink-0 text-xs font-medium text-muted-foreground"> |
| 122 | {prefs.locale() === "tr" ? "TR" : "EN"} |
| 123 | </span> |
| 124 | </DropdownMenuSubTrigger> |
| 125 | <DropdownMenuSubContent class="w-[min(12.5rem,calc(100vw-2rem))] p-2"> |
| 126 | <div class="grid grid-cols-2 gap-2"> |
| 127 | <ChoiceButton |
| 128 | active={prefs.locale() === "tr"} |
| 129 | label="TR" |
| 130 | icon={<IconGlobe class="h-5 w-5" />} |
| 131 | onSelect={() => prefs.setLocale("tr" as Locale)} |
| 132 | /> |
| 133 | <ChoiceButton |
| 134 | active={prefs.locale() === "en"} |
| 135 | label="EN" |
| 136 | icon={<IconGlobe class="h-5 w-5" />} |
| 137 | onSelect={() => prefs.setLocale("en" as Locale)} |
| 138 | /> |
| 139 | </div> |
| 140 | <p class="mt-2 text-center text-[11px] text-muted-foreground"> |
| 141 | {prefs.locale() === "tr" ? t("lang.tr") : t("lang.en")} |
| 142 | </p> |
| 143 | </DropdownMenuSubContent> |
| 144 | </DropdownMenuSub> |
| 145 | |
| 146 | {/* Theme submenu — centered icon grid */} |
| 147 | <DropdownMenuSub> |
| 148 | <DropdownMenuSubTrigger class="gap-2"> |
| 149 | <Show when={prefs.theme() === "dark"} fallback={<IconSun class="h-4 w-4 shrink-0" />}> |
| 150 | <IconMoon class="h-4 w-4 shrink-0" /> |
| 151 | </Show> |
| 152 | <span class="min-w-0 flex-1 truncate text-left">{t("theme.toggle")}</span> |
| 153 | <span class="max-w-18 shrink-0 truncate text-xs font-medium text-muted-foreground"> |
| 154 | {prefs.theme() === "dark" ? t("theme.dark") : t("theme.light")} |
| 155 | </span> |
| 156 | </DropdownMenuSubTrigger> |
| 157 | <DropdownMenuSubContent class="w-[min(12.5rem,calc(100vw-2rem))] p-2"> |
| 158 | <ThemeModeControl variant="cards" /> |
| 159 | </DropdownMenuSubContent> |
| 160 | </DropdownMenuSub> |
| 161 | |
| 162 | <DropdownMenuSeparator /> |
| 163 | |
| 164 | <DropdownMenuItem onSelect={() => setProfileOpen(true)}> |
| 165 | <IconSettings class="h-4 w-4 shrink-0" /> |
| 166 | <span>{t("nav.settings")}</span> |
| 167 | </DropdownMenuItem> |
| 168 | |
| 169 | <DropdownMenuSeparator /> |
| 170 | |
| 171 | <DropdownMenuItem destructive onSelect={() => void onLogout()}> |
| 172 | <IconLogout class="h-4 w-4 shrink-0" /> |
| 173 | <span>{t("nav.logout")}</span> |
| 174 | </DropdownMenuItem> |
| 175 | </DropdownMenuContent> |
| 176 | </DropdownMenu> |
| 177 | )} |
| 178 | </Show> |
| 179 | <AccountProfileDialog open={profileOpen()} onOpenChange={setProfileOpen} /> |
| 180 | </> |
| 181 | ); |
| 182 | } |