hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { For, Show, createEffect, createSignal, on } from "solid-js"; |
| 2 | import { Link, useNavigate, useRouterState } from "@tanstack/solid-router"; |
| 3 | import { HOME_ITEM, routeNavItem, visibleNavGroups, type NavGroup, type NavItem } from "@/components/layout/nav-items"; |
| 4 | import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; |
| 5 | import { IconChevronRight } from "@/components/ui/icons"; |
| 6 | import { cn } from "@/lib/cn"; |
| 7 | import { useAuth } from "@/stores/auth-context"; |
| 8 | import { useModules } from "@/stores/modules-context"; |
| 9 | import { useShellFeed } from "@/stores/shell-feed-context"; |
| 10 | import { useT } from "@/stores/preferences-context"; |
| 11 | |
| 12 | const FOLDED_KEY = "hezarfen.navFolded"; |
| 13 | |
| 14 | function readFolded(): Record<string, boolean> { |
| 15 | try { |
| 16 | const parsed: unknown = JSON.parse(localStorage.getItem(FOLDED_KEY) ?? "{}"); |
| 17 | return parsed && typeof parsed === "object" ? (parsed as Record<string, boolean>) : {}; |
| 18 | } catch { |
| 19 | return {}; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | export function SideNav(props: { |
| 24 | onNavigate?: () => void; |
| 25 | collapsed?: boolean; |
| 26 | onOpenCelebi?: () => void; |
| 27 | onOpenProfile?: () => void; |
| 28 | }) { |
| 29 | const auth = useAuth(); |
| 30 | const t = useT(); |
| 31 | const feed = useShellFeed(); |
| 32 | const modules = useModules(); |
| 33 | const navigate = useNavigate(); |
| 34 | const pathname = useRouterState({ select: (state) => state.location.pathname }); |
| 35 | const search = useRouterState({ select: (state) => state.location.search as Record<string, unknown> }); |
| 36 | |
| 37 | const home = () => (auth.user() ? [HOME_ITEM] : []); |
| 38 | const groups = () => visibleNavGroups(auth.user()?.role, modules.enabled()); |
| 39 | const current = () => routeNavItem(pathname(), auth.user()?.role, search()); |
| 40 | const unread = () => feed.unreadMessages().total; |
| 41 | const badgeFor = (item: NavItem) => (item.id === "messages" ? unread() : 0); |
| 42 | |
| 43 | const [folded, setFolded] = createSignal<Record<string, boolean>>(readFolded()); |
| 44 | const isFolded = (group: NavGroup) => folded()[group.id] ?? group.defaultFolded ?? false; |
| 45 | const toggleGroup = (group: NavGroup, section?: HTMLElement) => { |
| 46 | const opening = isFolded(group); |
| 47 | // Opening a section near the bottom would leave its rows below the fold; |
| 48 | // bring the section's last row into view once it has rendered. |
| 49 | if (opening && section) { |
| 50 | requestAnimationFrame(() => section.scrollIntoView({ block: "end", behavior: "smooth" })); |
| 51 | } |
| 52 | const next = { ...folded(), [group.id]: !isFolded(group) }; |
| 53 | setFolded(next); |
| 54 | try { |
| 55 | localStorage.setItem(FOLDED_KEY, JSON.stringify(next)); |
| 56 | } catch { |
| 57 | // Private mode / quota: folding still works for this session. |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | // Landing on a page inside a folded section unfolds it once, so the active |
| 62 | // row is visible; the viewer can still fold it again afterwards. |
| 63 | createEffect( |
| 64 | on(current, (item) => { |
| 65 | if (!item) return; |
| 66 | const group = groups().find((candidate) => candidate.items.some((entry) => entry.id === item.id)); |
| 67 | if (group && isFolded(group)) setFolded({ ...folded(), [group.id]: false }); |
| 68 | }), |
| 69 | ); |
| 70 | |
| 71 | const runAction = (item: NavItem) => { |
| 72 | props.onNavigate?.(); |
| 73 | if (item.action === "celebi") props.onOpenCelebi?.(); |
| 74 | if (item.action === "profile") props.onOpenProfile?.(); |
| 75 | }; |
| 76 | |
| 77 | return ( |
| 78 | <nav class="flex h-full flex-col gap-1" aria-label={t("nav.menu")}> |
| 79 | <div class="grid gap-1"> |
| 80 | <For each={home()}> |
| 81 | {(item) => { |
| 82 | const active = () => current()?.id === item.id; |
| 83 | return ( |
| 84 | <Link |
| 85 | to={item.to} |
| 86 | onClick={() => props.onNavigate?.()} |
| 87 | title={t(item.labelKey)} |
| 88 | aria-current={active() ? "page" : undefined} |
| 89 | class={cn( |
| 90 | "relative flex h-[34px] items-center rounded-lg text-[13px] font-medium outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring", |
| 91 | props.collapsed ? "mx-auto h-10 w-12 justify-center px-0" : "gap-3 px-3", |
| 92 | active() |
| 93 | ? "bg-primary/10 text-primary-text" |
| 94 | : "text-muted-foreground hover:bg-muted/70 hover:text-foreground", |
| 95 | )} |
| 96 | > |
| 97 | <item.Icon class="h-4 w-4 shrink-0" /> |
| 98 | <span class={props.collapsed ? "sr-only" : "truncate"}>{t(item.labelKey)}</span> |
| 99 | </Link> |
| 100 | ); |
| 101 | }} |
| 102 | </For> |
| 103 | </div> |
| 104 | <For each={groups()}> |
| 105 | {(group) => { |
| 106 | const active = () => group.items.some((item) => current()?.id === item.id); |
| 107 | const open = () => !isFolded(group); |
| 108 | let sectionEl: HTMLElement | undefined; |
| 109 | const renderLinks = () => ( |
| 110 | <div class="grid gap-px"> |
| 111 | <For each={group.items}> |
| 112 | {(item: NavItem) => { |
| 113 | const itemActive = () => current()?.id === item.id; |
| 114 | // A function, not a value: Solid runs this callback once, so a plain |
| 115 | // cn(...) would freeze the active style at first render. |
| 116 | const linkClass = () => cn( |
| 117 | "relative flex h-[30px] items-center gap-3 rounded-lg px-3 text-[13px] font-medium outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring", |
| 118 | itemActive() |
| 119 | ? "bg-primary/10 text-primary-text" |
| 120 | : "text-muted-foreground hover:bg-muted/70 hover:text-foreground", |
| 121 | ); |
| 122 | const content = ( |
| 123 | <> |
| 124 | <item.Icon class="h-4 w-4 shrink-0" /> |
| 125 | <span class="truncate">{t(item.labelKey)}</span> |
| 126 | <Show when={item.soon}> |
| 127 | <span class="ml-auto shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"> |
| 128 | {t("nav.soon")} |
| 129 | </span> |
| 130 | </Show> |
| 131 | <Show when={badgeFor(item) > 0}> |
| 132 | <span class="ml-auto flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[11px] font-bold text-white"> |
| 133 | {unread() > 9 ? "9+" : unread()} |
| 134 | </span> |
| 135 | </Show> |
| 136 | </> |
| 137 | ); |
| 138 | return ( |
| 139 | <Show |
| 140 | when={!item.action} |
| 141 | fallback={ |
| 142 | <button type="button" onClick={() => runAction(item)} title={t(item.labelKey)} class={cn(linkClass(), "w-full text-left")}> |
| 143 | {content} |
| 144 | </button> |
| 145 | } |
| 146 | > |
| 147 | <Link to={item.to} search={item.search as never} onClick={() => props.onNavigate?.()} title={t(item.labelKey)} aria-current={itemActive() ? "page" : undefined} class={linkClass()}> |
| 148 | {content} |
| 149 | </Link> |
| 150 | </Show> |
| 151 | ); |
| 152 | }} |
| 153 | </For> |
| 154 | </div> |
| 155 | ); |
| 156 | return ( |
| 157 | <Show |
| 158 | when={props.collapsed} |
| 159 | fallback={ |
| 160 | <section class="mt-2 scroll-mb-2 first:mt-0" ref={sectionEl}> |
| 161 | <button |
| 162 | type="button" |
| 163 | onClick={() => toggleGroup(group, sectionEl)} |
| 164 | aria-expanded={open()} |
| 165 | class="group/section flex h-7 w-full items-center gap-1.5 rounded-md px-3 text-left text-[11px] font-semibold tracking-wide text-muted-foreground/80 outline-hidden transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring" |
| 166 | > |
| 167 | <span class="truncate">{t(group.labelKey)}</span> |
| 168 | <IconChevronRight |
| 169 | class={cn( |
| 170 | "ml-auto h-3 w-3 shrink-0 transition-[transform,opacity] duration-200", |
| 171 | open() ? "rotate-90 opacity-0 group-hover/section:opacity-60 group-focus-visible/section:opacity-60" : "opacity-60", |
| 172 | )} |
| 173 | /> |
| 174 | </button> |
| 175 | <Show when={open()}>{renderLinks()}</Show> |
| 176 | </section> |
| 177 | } |
| 178 | > |
| 179 | <DropdownMenu placement="right-start" gutter={8}> |
| 180 | <DropdownMenuTrigger |
| 181 | class={cn( |
| 182 | "relative mx-auto flex h-10 w-12 items-center justify-center rounded-md outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring", |
| 183 | active() |
| 184 | ? "bg-primary/10 text-primary-text" |
| 185 | : "text-muted-foreground hover:bg-muted/70 hover:text-foreground", |
| 186 | )} |
| 187 | aria-label={t(group.labelKey)} |
| 188 | title={t(group.labelKey)} |
| 189 | > |
| 190 | <group.Icon class="h-4 w-4 shrink-0" /> |
| 191 | <IconChevronRight class={cn("absolute right-1 h-3 w-3", active() ? "opacity-100" : "opacity-60")} /> |
| 192 | </DropdownMenuTrigger> |
| 193 | <DropdownMenuContent class="w-52 p-1"> |
| 194 | <div class="px-2.5 py-1.5 text-xs font-semibold text-muted-foreground"> |
| 195 | {t(group.labelKey)} |
| 196 | </div> |
| 197 | <DropdownMenuSeparator class="my-1" /> |
| 198 | <For each={group.items}> |
| 199 | {(item: NavItem) => ( |
| 200 | <DropdownMenuItem |
| 201 | class="min-h-8 gap-2.5 rounded-md px-2.5 py-1.5 text-[13px]" |
| 202 | onSelect={() => { |
| 203 | if (item.action) { |
| 204 | runAction(item); |
| 205 | return; |
| 206 | } |
| 207 | props.onNavigate?.(); |
| 208 | void navigate({ to: item.to, search: item.search as never }); |
| 209 | }} |
| 210 | > |
| 211 | <item.Icon class="h-4 w-4 shrink-0" /> |
| 212 | <span>{t(item.labelKey)}</span> |
| 213 | <Show when={item.soon}> |
| 214 | <span class="ml-auto shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground"> |
| 215 | {t("nav.soon")} |
| 216 | </span> |
| 217 | </Show> |
| 218 | <Show when={badgeFor(item) > 0}> |
| 219 | <span class="ml-auto flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[11px] font-bold text-white"> |
| 220 | {unread() > 9 ? "9+" : unread()} |
| 221 | </span> |
| 222 | </Show> |
| 223 | </DropdownMenuItem> |
| 224 | )} |
| 225 | </For> |
| 226 | </DropdownMenuContent> |
| 227 | </DropdownMenu> |
| 228 | </Show> |
| 229 | ); |
| 230 | }} |
| 231 | </For> |
| 232 | </nav> |
| 233 | ); |
| 234 | } |