import { For, Show, createEffect, createSignal, on } from "solid-js"; import { Link, useNavigate, useRouterState } from "@tanstack/solid-router"; import { HOME_ITEM, routeNavItem, visibleNavGroups, type NavGroup, type NavItem } from "@/components/layout/nav-items"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"; import { IconChevronRight } from "@/components/ui/icons"; import { TooltipTrigger } from "@/components/ui/tooltip"; import { RailTip } from "@/components/layout/rail-tip"; import { cn } from "@/lib/cn"; import { useAuth } from "@/stores/auth-context"; import { useModules } from "@/stores/modules-context"; import { useShellFeed } from "@/stores/shell-feed-context"; import { useT } from "@/stores/preferences-context"; const FOLDED_KEY = "hezarfen.navFolded"; // The collapsed rail: one square hit area per control, icon dead centre, and // the active control marked twice — the tinted square plus a bar on the rail's // outer edge — because an icon alone carries no label to read the state from. const RAIL_ITEM = "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"; const RAIL_ACTIVE = "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"; const RAIL_IDLE = "text-muted-foreground hover:bg-muted/70 hover:text-foreground"; function readFolded(): Record { try { const parsed: unknown = JSON.parse(localStorage.getItem(FOLDED_KEY) ?? "{}"); return parsed && typeof parsed === "object" ? (parsed as Record) : {}; } catch { return {}; } } export function SideNav(props: { onNavigate?: () => void; collapsed?: boolean; onOpenCelebi?: () => void; onOpenProfile?: () => void; }) { const auth = useAuth(); const t = useT(); const feed = useShellFeed(); const modules = useModules(); const navigate = useNavigate(); const pathname = useRouterState({ select: (state) => state.location.pathname }); const search = useRouterState({ select: (state) => state.location.search as Record }); const home = () => (auth.user() ? [HOME_ITEM] : []); const groups = () => visibleNavGroups(auth.user()?.role, modules.enabled()); const current = () => routeNavItem(pathname(), auth.user()?.role, search()); const unread = () => feed.unreadMessages().total; const badgeFor = (item: NavItem) => (item.id === "messages" ? unread() : 0); const [folded, setFolded] = createSignal>(readFolded()); const isFolded = (group: NavGroup) => folded()[group.id] ?? group.defaultFolded ?? false; const toggleGroup = (group: NavGroup, section?: HTMLElement) => { const opening = isFolded(group); // Opening a section near the bottom would leave its rows below the fold; // bring the section's last row into view once it has rendered. if (opening && section) { requestAnimationFrame(() => section.scrollIntoView({ block: "end", behavior: "smooth" })); } const next = { ...folded(), [group.id]: !isFolded(group) }; setFolded(next); try { localStorage.setItem(FOLDED_KEY, JSON.stringify(next)); } catch { // Private mode / quota: folding still works for this session. } }; // Landing on a page inside a folded section unfolds it once, so the active // row is visible; the viewer can still fold it again afterwards. createEffect( on(current, (item) => { if (!item) return; const group = groups().find((candidate) => candidate.items.some((entry) => entry.id === item.id)); if (group && isFolded(group)) setFolded({ ...folded(), [group.id]: false }); }), ); const runAction = (item: NavItem) => { props.onNavigate?.(); if (item.action === "celebi") props.onOpenCelebi?.(); if (item.action === "profile") props.onOpenProfile?.(); }; return ( ); }