155 lines
5.6 KiB
TypeScript
155 lines
5.6 KiB
TypeScript
import { Link, useLocation } from "react-router";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherSidebarClose, FeatherSidebarOpen } from "@subframe/core";
|
|
import UrlUtils from "~/utils/app/UrlUtils";
|
|
import Tabs from "../tabs/Tabs";
|
|
import EntityIcon from "~/components/layouts/icons/EntityIcon";
|
|
import { IconButton, 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 { t } = useTranslation();
|
|
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
|
|
);
|
|
// Eingeklappt: Davids zentrierte icon-only-Variant (Icon mittig, Text aus, Selected-Bg als sauberes
|
|
// Quadrat). Der Link zentriert das w-auto-Item in der schmalen Rail (justify-center), sonst klebt das
|
|
// Icon links. Aufgeklappt: icon-and-text, w-full -> linksbündig wie gehabt. Die Width bleibt animiert.
|
|
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 in Davids Optik, echt einklappbar (Width-Transition) */}
|
|
<div className="relative hidden sm:flex">
|
|
<SidebarInside
|
|
visible={true}
|
|
variant="open"
|
|
className={`min-h-[calc(100vh-70px)] min-w-0 overflow-hidden ${animate ? "transition-[width] duration-200 ease-in-out" : ""} ${
|
|
collapsed ? "w-12" : "w-[208px]"
|
|
}`}
|
|
>
|
|
{topItems.map(renderItem)}
|
|
{bottomItems.length > 0 && (
|
|
<div className="mt-auto flex w-full flex-col">
|
|
<SidebarInside.NavDivider />
|
|
{bottomItems.map(renderItem)}
|
|
</div>
|
|
)}
|
|
</SidebarInside>
|
|
{/* Eigener sichtbarer Toggle: Davids interner Button wird vom overflow-hidden der Bar verdeckt. */}
|
|
<div className="absolute -right-[17px] top-[26px] z-20 flex h-8 w-8 items-center justify-center">
|
|
<IconButton
|
|
variant="tertiary"
|
|
size="extra-small"
|
|
square={true}
|
|
icon={collapsed ? <FeatherSidebarOpen /> : <FeatherSidebarClose />}
|
|
onClick={toggleCollapsed}
|
|
aria-label={t("components.sidebar.toggle")}
|
|
/>
|
|
</div>
|
|
</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>
|
|
);
|
|
}
|