forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

side-nav.tsx177 lines, 8.7 KiBHistoryRawDownload
1import { For, Show } from "solid-js";
2import { Link, useNavigate, useRouterState } from "@tanstack/solid-router";
3import { primaryNavItems, routeNavItem, sidebarNavGroups, 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 { cn } from "@/lib/cn";
7import { useAuth } from "@/stores/auth-context";
8import { useShellFeed } from "@/stores/shell-feed-context";
9import { useT } from "@/stores/preferences-context";
10
11export function SideNav(props: { onNavigate?: () => void; collapsed?: boolean }) {
12 const auth = useAuth();
13 const t = useT();
14 const feed = useShellFeed();
15 const navigate = useNavigate();
16 const pathname = useRouterState({ select: (state) => state.location.pathname });
17
18 const primary = () => primaryNavItems(auth.user()?.role);
19 const groups = () => sidebarNavGroups(auth.user()?.role);
20 const current = () => routeNavItem(pathname(), auth.user()?.role);
21 const unread = () => feed.unreadMessages().total;
22 const badgeFor = (item: NavItem) => (item.id === "messages" ? unread() : 0);
23
24 return (
25 <nav class="flex h-full flex-col gap-1" aria-label={t("nav.menu")}>
26 <div class="grid gap-1">
27 <For each={primary()}>
28 {(item) => {
29 const active = () => current()?.id === item.id;
30 return (
31 <Link
32 to={item.to}
33 onClick={() => props.onNavigate?.()}
34 title={t(item.labelKey)}
35 aria-current={active() ? "page" : undefined}
36 class={cn(
37 "relative flex h-9 items-center rounded-md text-[13px] font-medium outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring",
38 props.collapsed ? "mx-auto h-10 w-12 justify-center px-0" : "gap-3 px-3",
39 active()
40 ? "bg-primary/10 text-primary"
41 : "text-muted-foreground hover:bg-muted/70 hover:text-foreground",
42 )}
43 >
44 <item.Icon class="h-4 w-4 shrink-0" />
45 <span class={props.collapsed ? "sr-only" : "truncate"}>{t(item.labelKey)}</span>
46 <Show when={badgeFor(item) > 0}>
47 <Show
48 when={!props.collapsed}
49 fallback={
50 <span class="absolute -right-0.5 -top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-500 px-1 text-[9px] font-bold text-white">
51 {unread() > 9 ? "9+" : unread()}
52 </span>
53 }
54 >
55 <span class="ml-auto flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[10px] font-bold text-white">
56 {unread() > 9 ? "9+" : unread()}
57 </span>
58 </Show>
59 </Show>
60 </Link>
61 );
62 }}
63 </For>
64 </div>
65 <For each={groups()}>
66 {(group) => {
67 const active = () => group.items.some((item) => current()?.id === item.id);
68 const renderLinks = () => (
69 <div class="grid gap-px">
70 <For each={group.items}>
71 {(item: NavItem) => {
72 const itemActive = () => current()?.id === item.id;
73 return (
74 <Link
75 to={item.to}
76 onClick={() => props.onNavigate?.()}
77 title={t(item.labelKey)}
78 aria-current={itemActive() ? "page" : undefined}
79 class={cn(
80 // Active submenu item: bold foreground text + a bright segment over
81 // the group's left guide line (no filled pill).
82 "relative flex h-8 items-center gap-2.5 rounded-md px-2.5 text-[13px] outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring",
83 itemActive()
84 ? "font-semibold text-foreground before:absolute before:-left-[9px] before:top-1 before:bottom-1 before:w-0.5 before:rounded-full before:bg-foreground"
85 : "font-medium text-muted-foreground hover:bg-muted/70 hover:text-foreground",
86 )}
87 >
88 <item.Icon class="h-4 w-4 shrink-0 opacity-80" />
89 <span class="truncate">{t(item.labelKey)}</span>
90 <Show when={badgeFor(item) > 0}>
91 <Show
92 when={!props.collapsed}
93 fallback={
94 <span class="absolute -right-0.5 -top-0.5 flex h-4 min-w-4 items-center justify-center rounded-full bg-rose-500 px-1 text-[9px] font-bold text-white">
95 {unread() > 9 ? "9+" : unread()}
96 </span>
97 }
98 >
99 <span class="ml-auto flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[10px] font-bold text-white">
100 {unread() > 9 ? "9+" : unread()}
101 </span>
102 </Show>
103 </Show>
104 </Link>
105 );
106 }}
107 </For>
108 </div>
109 );
110 return (
111 <Show
112 when={props.collapsed}
113 fallback={
114 <details class="group/nav" open={active()}>
115 <summary
116 class={cn(
117 "flex h-9 cursor-pointer list-none items-center gap-3 rounded-md px-3 text-[13px] font-medium outline-hidden transition-colors focus-visible:ring-2 focus-visible:ring-ring [&::-webkit-details-marker]:hidden",
118 active()
119 ? "bg-muted/70 text-foreground"
120 : "text-muted-foreground hover:bg-muted/70 hover:text-foreground",
121 )}
122 >
123 <group.Icon class="h-4 w-4 shrink-0" />
124 <span class="truncate">{t(group.labelKey)}</span>
125 <IconChevronRight class="ml-auto h-3.5 w-3.5 shrink-0 opacity-60 transition-transform duration-200 group-open/nav:rotate-90" />
126 </summary>
127 <div class="mt-0.5 ml-[1.15rem] border-l border-border/70 pl-2">{renderLinks()}</div>
128 </details>
129 }
130 >
131 <DropdownMenu placement="right-start" gutter={8}>
132 <DropdownMenuTrigger
133 class={cn(
134 "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",
135 active()
136 ? "bg-primary/10 text-primary"
137 : "text-muted-foreground hover:bg-muted/70 hover:text-foreground",
138 )}
139 aria-label={t(group.labelKey)}
140 title={t(group.labelKey)}
141 >
142 <group.Icon class="h-4 w-4 shrink-0" />
143 <IconChevronRight class={cn("absolute right-1 h-3 w-3", active() ? "opacity-100" : "opacity-60")} />
144 </DropdownMenuTrigger>
145 <DropdownMenuContent class="w-52 p-1">
146 <div class="px-2.5 py-1.5 text-xs font-semibold text-muted-foreground">
147 {t(group.labelKey)}
148 </div>
149 <DropdownMenuSeparator class="my-1" />
150 <For each={group.items}>
151 {(item: NavItem) => (
152 <DropdownMenuItem
153 class="min-h-8 gap-2.5 rounded-md px-2.5 py-1.5 text-[13px]"
154 onSelect={() => {
155 props.onNavigate?.();
156 void navigate({ to: item.to });
157 }}
158 >
159 <item.Icon class="h-4 w-4 shrink-0" />
160 <span>{t(item.labelKey)}</span>
161 <Show when={badgeFor(item) > 0}>
162 <span class="ml-auto flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[10px] font-bold text-white">
163 {unread() > 9 ? "9+" : unread()}
164 </span>
165 </Show>
166 </DropdownMenuItem>
167 )}
168 </For>
169 </DropdownMenuContent>
170 </DropdownMenu>
171 </Show>
172 );
173 }}
174 </For>
175 </nav>
176 );
177}