forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

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