hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { useNavigate } from "@tanstack/solid-router"; |
| 2 | import { Show, createSignal } from "solid-js"; |
| 3 | import { createResource } from "@/lib/create-resource"; |
| 4 | import type { User } from "@/api/client"; |
| 5 | import { getMyProfile } from "@/api/users"; |
| 6 | import { avatarRevision } from "@/lib/avatar"; |
| 7 | import type { MessageKey } from "@/i18n/messages"; |
| 8 | import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; |
| 9 | import { |
| 10 | DropdownMenu, |
| 11 | DropdownMenuContent, |
| 12 | DropdownMenuItem, |
| 13 | DropdownMenuSeparator, |
| 14 | DropdownMenuTrigger, |
| 15 | } from "@/components/ui/dropdown-menu"; |
| 16 | import { IconChevronRight, IconGlobe, IconGuide, IconLogout, IconSettings, IconUserCircle } from "@/components/ui/icons"; |
| 17 | import { ThemeModeControl } from "@/components/layout/theme-mode-control"; |
| 18 | import { UserAvatar } from "@/components/users/user-avatar"; |
| 19 | import { useAuth } from "@/stores/auth-context"; |
| 20 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 21 | import { cn } from "@/lib/cn"; |
| 22 | |
| 23 | function displayName(user: User) { |
| 24 | return user.display_name?.trim() || [user.name, user.surname].filter(Boolean).join(" ").trim() || user.username; |
| 25 | } |
| 26 | |
| 27 | export function SidebarAccount(props: { |
| 28 | collapsed?: boolean; |
| 29 | onLogout: () => void | Promise<void>; |
| 30 | /** Mobile sheet: open upward so the menu stays on screen. Desktop keeps right-end. */ |
| 31 | menuPlacement?: "right-end" | "top-start"; |
| 32 | /** When set (mobile sheet), host closes the sheet and opens the shared profile dialog. */ |
| 33 | onOpenSettings?: () => void; |
| 34 | /** Mobile sheet: dismiss the sheet before route changes from this menu. */ |
| 35 | onNavigateAway?: () => void; |
| 36 | }) { |
| 37 | const auth = useAuth(); |
| 38 | const prefs = usePreferences(); |
| 39 | const t = useT(); |
| 40 | const navigate = useNavigate(); |
| 41 | const [profileOpen, setProfileOpen] = createSignal(false); |
| 42 | |
| 43 | const leave = (go: () => void) => { |
| 44 | // Defer so the account menu finishes closing before the sheet dismisses |
| 45 | // or a route change remounts focus. |
| 46 | setTimeout(() => { |
| 47 | props.onNavigateAway?.(); |
| 48 | go(); |
| 49 | }, 0); |
| 50 | }; |
| 51 | |
| 52 | const openSettings = () => { |
| 53 | // Defer so the menu finishes closing/restoring focus before another |
| 54 | // overlay mounts — same race TableRowActions guards against. |
| 55 | if (props.onOpenSettings) { |
| 56 | setTimeout(() => props.onOpenSettings!(), 0); |
| 57 | return; |
| 58 | } |
| 59 | setTimeout(() => setProfileOpen(true), 0); |
| 60 | }; |
| 61 | |
| 62 | // The auth user carries no avatar meta, so without this both chips below |
| 63 | // attempt the image and 404 when there is no photo. One profile read tells |
| 64 | // both; it re-reads on every avatar upload/delete via avatarRevision, and |
| 65 | // stays unfetched while logged out. |
| 66 | const [profile] = createResource(() => (auth.user() ? avatarRevision() : null), () => |
| 67 | getMyProfile(), |
| 68 | ); |
| 69 | const hasAvatar = () => { |
| 70 | const p = profile(); |
| 71 | return p ? p.avatar !== null : false; |
| 72 | }; |
| 73 | |
| 74 | return ( |
| 75 | <> |
| 76 | <Show when={auth.user()}> |
| 77 | {(u) => { |
| 78 | const name = () => displayName(u()); |
| 79 | return ( |
| 80 | <div class={cn("shrink-0 border-t border-border/80 p-2 dark:border-white/8", props.collapsed && "px-2 py-2") }> |
| 81 | <DropdownMenu placement={props.menuPlacement ?? "right-end"} gutter={8}> |
| 82 | <DropdownMenuTrigger |
| 83 | class={cn( |
| 84 | "flex w-full items-center text-left outline-hidden transition-colors", |
| 85 | "focus-visible:ring-2 focus-visible:ring-ring", |
| 86 | props.collapsed |
| 87 | ? "h-9 justify-center rounded-md px-0 text-foreground hover:bg-muted/70 data-expanded:bg-muted/70 dark:text-white dark:hover:bg-white/8 dark:data-expanded:bg-white/8" |
| 88 | : "h-10 gap-2 rounded-md px-2 text-foreground hover:bg-muted/70 data-expanded:bg-muted/70 dark:text-white dark:hover:bg-white/8 dark:data-expanded:bg-white/8", |
| 89 | )} |
| 90 | aria-label={t("nav.account")} |
| 91 | > |
| 92 | <UserAvatar |
| 93 | userId={u().id} |
| 94 | name={name()} |
| 95 | hasAvatar={hasAvatar()} |
| 96 | size="sm" |
| 97 | class={props.collapsed ? "h-9 w-9" : undefined} |
| 98 | /> |
| 99 | <Show when={!props.collapsed}> |
| 100 | <span class="min-w-0 flex-1"> |
| 101 | <span class="block truncate text-[13px] font-semibold leading-4 2xl:text-sm">{name()}</span> |
| 102 | <span class="block truncate text-[11px] font-medium text-muted-foreground dark:text-white/55 2xl:text-[11px]"> |
| 103 | {t(`role.${u().role}` as MessageKey)} |
| 104 | </span> |
| 105 | </span> |
| 106 | <IconChevronRight class="h-3.5 w-3.5 shrink-0 text-muted-foreground dark:text-white/50" /> |
| 107 | </Show> |
| 108 | </DropdownMenuTrigger> |
| 109 | |
| 110 | <DropdownMenuContent class="w-72 rounded-xl border-border bg-popover p-0 text-popover-foreground shadow-xl shadow-black/10"> |
| 111 | <DropdownMenuItem |
| 112 | class="m-1.5 gap-2.5 rounded-xl bg-muted/70 p-2.5 focus:bg-muted data-highlighted:bg-muted dark:bg-white/8 dark:focus:bg-white/10 dark:data-highlighted:bg-white/10" |
| 113 | onSelect={() => leave(() => void navigate({ to: "/profile/me" }))} |
| 114 | > |
| 115 | <UserAvatar userId={u().id} name={name()} hasAvatar={hasAvatar()} size="md" class="ring-0" /> |
| 116 | <span class="min-w-0 flex-1"> |
| 117 | <span class="block truncate text-sm font-semibold">{name()}</span> |
| 118 | <span class="block truncate text-xs text-muted-foreground dark:text-white/60">@{u().username}</span> |
| 119 | </span> |
| 120 | <IconChevronRight class="h-3.5 w-3.5 shrink-0 text-muted-foreground dark:text-white/50" /> |
| 121 | </DropdownMenuItem> |
| 122 | <div class="px-1.5 pb-1.5"> |
| 123 | <DropdownMenuItem class="rounded-lg gap-3 font-medium" onSelect={() => leave(() => void navigate({ to: "/profile/me" }))}> |
| 124 | <IconUserCircle class="h-4 w-4 shrink-0 text-muted-foreground dark:text-white/60" /> |
| 125 | <span>{t("profile.myProfile")}</span> |
| 126 | </DropdownMenuItem> |
| 127 | <DropdownMenuItem class="rounded-lg gap-3" onSelect={openSettings}> |
| 128 | <IconSettings class="h-4 w-4 shrink-0 text-muted-foreground dark:text-white/60" /> |
| 129 | <span>{t("nav.settings")}</span> |
| 130 | </DropdownMenuItem> |
| 131 | <DropdownMenuItem class="rounded-lg gap-3" onSelect={() => leave(() => void navigate({ to: "/guide" }))}> |
| 132 | <IconGuide class="h-4 w-4 shrink-0 text-muted-foreground dark:text-white/60" /> |
| 133 | <span>{t("nav.guide")}</span> |
| 134 | </DropdownMenuItem> |
| 135 | <DropdownMenuItem class="rounded-lg gap-3" onSelect={() => prefs.setLocale(prefs.locale() === "tr" ? "en" : "tr")}> |
| 136 | <IconGlobe class="h-4 w-4 shrink-0 text-muted-foreground dark:text-white/60" /> |
| 137 | <span class="min-w-0 flex-1">{t("lang.label")}</span> |
| 138 | <span class="text-xs text-muted-foreground dark:text-white/55">{prefs.locale() === "tr" ? "TR" : "EN"}</span> |
| 139 | </DropdownMenuItem> |
| 140 | </div> |
| 141 | <DropdownMenuSeparator class="my-0 dark:bg-white/8" /> |
| 142 | <div class="p-1.5"> |
| 143 | <DropdownMenuItem destructive class="rounded-lg gap-3" onSelect={() => leave(() => void props.onLogout())}> |
| 144 | <IconLogout class="h-4 w-4 shrink-0" /> |
| 145 | <span>{t("nav.logout")}</span> |
| 146 | </DropdownMenuItem> |
| 147 | </div> |
| 148 | <DropdownMenuSeparator class="my-0 dark:bg-white/8" /> |
| 149 | <div class="flex items-center justify-between gap-3 p-3"> |
| 150 | <span class="font-semibold">{t("app.name")}</span> |
| 151 | <ThemeModeControl variant="compact" /> |
| 152 | </div> |
| 153 | </DropdownMenuContent> |
| 154 | </DropdownMenu> |
| 155 | </div> |
| 156 | ); |
| 157 | }} |
| 158 | </Show> |
| 159 | <AccountProfileDialog open={profileOpen()} onOpenChange={setProfileOpen} /> |
| 160 | </> |
| 161 | ); |
| 162 | } |