Files
2026-07-08 16:15:37 +02:00

138 lines
4.7 KiB
TypeScript

import { Link, useLocation } from "react-router";
import { useEffect, useRef, useState } from "react";
import UrlUtils from "~/utils/app/UrlUtils";
import Tabs from "../tabs/Tabs";
import EntityIcon from "~/components/layouts/icons/EntityIcon";
import { SidebarInside } from "~/ui";
export type IconDto = {
name: string;
href: string;
prefetch?: "intent" | "render" | "none";
icon?: React.ReactNode;
iconSelected?: React.ReactNode;
bottom?: boolean;
exact?: boolean;
textIcon?: string;
textIconSelected?: string;
hidden?: boolean;
};
// Pro-User gemerkter Collapse-Zustand der Inner-Sidebar (Default: aufgeklappt).
const COLLAPSE_STORAGE_KEY = "hg.innerSidebar.collapsed";
export default function SidebarIconsLayout({
children,
items,
}: {
children: React.ReactNode;
items: IconDto[];
// Beibehalten für API-Kompatibilität der 17 Call-Sites; in Davids Inner-Sidebar nicht mehr nötig.
label?: {
align: "bottom" | "right";
};
scrollRestoration?: boolean;
}) {
const location = useLocation();
const mainElement = useRef<HTMLDivElement>(null);
// SSR-safe: Server rendert immer aufgeklappt, der gemerkte Zustand wird erst nach Hydration geladen.
const [collapsed, setCollapsed] = useState(false);
// Transitions erst NACH dem Restore aktivieren, sonst animiert der gemerkte Zustand bei jedem Seitenaufruf.
const [animate, setAnimate] = useState(false);
useEffect(() => {
try {
setCollapsed(window.localStorage.getItem(COLLAPSE_STORAGE_KEY) === "1");
} catch {
// localStorage nicht verfügbar (Privatmodus o.ä.) -> Default aufgeklappt
}
const id = window.requestAnimationFrame(() => setAnimate(true));
return () => window.cancelAnimationFrame(id);
}, []);
function toggleCollapsed() {
setCollapsed((prev) => {
const next = !prev;
try {
window.localStorage.setItem(COLLAPSE_STORAGE_KEY, next ? "1" : "0");
} catch {
// persistieren best-effort
}
return next;
});
}
function findExactRoute(element: IconDto) {
const hrefWithoutQuery = element.href.split("?")[0];
if (element.exact) {
return UrlUtils.stripTrailingSlash(location.pathname) === UrlUtils.stripTrailingSlash(hrefWithoutQuery);
}
return (location.pathname + location.search).includes(hrefWithoutQuery);
}
const currentTab = items.find((element) => findExactRoute(element));
function renderItem(item: IconDto, idx: number) {
const current = currentTab?.name === item.name;
const iconNode =
item.textIcon !== undefined ? (
<EntityIcon className="h-5 w-5" icon={current ? item.textIconSelected ?? item.textIcon : item.textIcon} />
) : current ? (
item.iconSelected ?? item.icon
) : (
item.icon
);
// Collapsed: center the icon-only item in the narrow rail. Expanded: keep the regular label row.
return (
<Link key={idx} prefetch={item.prefetch} to={item.href} className="flex w-full justify-center" title={item.name}>
<SidebarInside.NavItem icon={iconNode} selected={current} variant={collapsed ? "icon-only" : "icon-and-text"}>
{item.name}
</SidebarInside.NavItem>
</Link>
);
}
const topItems = items.filter((f) => !f.bottom && !f.hidden);
const bottomItems = items.filter((f) => f.bottom && !f.hidden);
return (
<div className="sm:flex sm:flex-row">
{/* Desktop: Inner sidebar uses the same mechanical width-collapse as the main navigation. */}
<div className="relative hidden sm:flex">
<SidebarInside
visible={true}
variant={collapsed ? "closed" : "open"}
onToggleCollapse={toggleCollapsed}
className={`min-h-[calc(100svh-4rem)] ${animate ? "" : "transition-none"}`}
>
{topItems.map(renderItem)}
{bottomItems.length > 0 && (
<div className="mt-auto flex w-full flex-col">
<SidebarInside.NavDivider />
{bottomItems.map(renderItem)}
</div>
)}
</SidebarInside>
</div>
{/* Mobile: Tabs-Leiste (unverändert) */}
<div className="border-border-default bg-surface-neutral-primary w-full border-b py-2 shadow-2xs sm:hidden">
<div className="mx-auto flex max-w-5xl items-center justify-between gap-2 px-4 sm:px-6 lg:px-8 xl:max-w-7xl 2xl:max-w-(--breakpoint-2xl)">
<Tabs
tabs={items
.filter((f) => !f.hidden)
.map((i) => {
return { name: i.name, routePath: i.href };
})}
className="grow"
/>
</div>
</div>
{/* Hauptbereich */}
<div ref={mainElement} className="w-full overflow-x-hidden">
{children}
</div>
</div>
);
}