hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import type { Component } from "solid-js"; |
| 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 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 { IconProps } from "@/components/ui/icons"; |
| 8 | import { IconGlobe, IconGuide, IconLogout, IconSettings, IconUserCircle } from "@/components/ui/icons"; |
| 9 | import { useAuth } from "@/stores/auth-context"; |
| 10 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 11 | |
| 12 | export type AccountAction = { |
| 13 | id: "profile" | "settings" | "guide" | "language" | "logout"; |
| 14 | label: string; |
| 15 | Icon: Component<IconProps>; |
| 16 | onSelect: () => void; |
| 17 | /** Short value shown at the row's end (the current language). */ |
| 18 | trailing?: string; |
| 19 | destructive?: boolean; |
| 20 | /** Leaves the menu open: the row changes a setting in place. */ |
| 21 | stays?: boolean; |
| 22 | }; |
| 23 | |
| 24 | export function accountDisplayName(user: User) { |
| 25 | return user.display_name?.trim() || [user.name, user.surname].filter(Boolean).join(" ").trim() || user.username; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * What the account menu offers, shared by the desktop dropdown and the phone |
| 30 | * sheet's account view so the two never list different things. |
| 31 | */ |
| 32 | export function createAccountMenu(options: { |
| 33 | onLogout: () => void | Promise<void>; |
| 34 | onOpenSettings: () => void; |
| 35 | /** Runs before a route change or logout — the phone sheet closes itself. */ |
| 36 | onLeave?: () => void; |
| 37 | }) { |
| 38 | const auth = useAuth(); |
| 39 | const prefs = usePreferences(); |
| 40 | const t = useT(); |
| 41 | const navigate = useNavigate(); |
| 42 | |
| 43 | // The auth user carries no avatar meta, so without this the chips attempt |
| 44 | // the image and 404 when there is no photo. One profile read tells them; it |
| 45 | // re-reads on every avatar upload/delete via avatarRevision, and stays |
| 46 | // unfetched while logged out. |
| 47 | const [profile] = createResource(() => (auth.user() ? avatarRevision() : null), () => getMyProfile()); |
| 48 | const hasAvatar = () => { |
| 49 | const p = profile(); |
| 50 | return p ? p.avatar !== null : false; |
| 51 | }; |
| 52 | |
| 53 | // Deferred so a closing menu finishes restoring focus before the route |
| 54 | // changes or another overlay mounts — the race TableRowActions guards too. |
| 55 | const leave = (go: () => void) => { |
| 56 | setTimeout(() => { |
| 57 | options.onLeave?.(); |
| 58 | go(); |
| 59 | }, 0); |
| 60 | }; |
| 61 | |
| 62 | const actions = (): AccountAction[] => [ |
| 63 | { id: "profile", label: t("profile.myProfile"), Icon: IconUserCircle, onSelect: () => leave(() => void navigate({ to: "/profile/me" })) }, |
| 64 | { id: "settings", label: t("nav.settings"), Icon: IconSettings, onSelect: () => setTimeout(options.onOpenSettings, 0) }, |
| 65 | { id: "guide", label: t("nav.guide"), Icon: IconGuide, onSelect: () => leave(() => void navigate({ to: "/guide" })) }, |
| 66 | { |
| 67 | id: "language", |
| 68 | label: t("lang.label"), |
| 69 | Icon: IconGlobe, |
| 70 | trailing: prefs.locale() === "tr" ? "TR" : "EN", |
| 71 | stays: true, |
| 72 | onSelect: () => prefs.setLocale(prefs.locale() === "tr" ? "en" : "tr"), |
| 73 | }, |
| 74 | { id: "logout", label: t("nav.logout"), Icon: IconLogout, destructive: true, onSelect: () => leave(() => void options.onLogout()) }, |
| 75 | ]; |
| 76 | |
| 77 | return { |
| 78 | user: auth.user, |
| 79 | hasAvatar, |
| 80 | actions, |
| 81 | openProfile: () => leave(() => void navigate({ to: "/profile/me" })), |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | export type AccountMenu = ReturnType<typeof createAccountMenu>; |