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(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 ? ( ) : 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 ( {item.name} ); } const topItems = items.filter((f) => !f.bottom && !f.hidden); const bottomItems = items.filter((f) => f.bottom && !f.hidden); return (
{/* Desktop: Inner-Sidebar in Davids Optik, echt einklappbar (Width-Transition) */}
{topItems.map(renderItem)} {bottomItems.length > 0 && (
{bottomItems.map(renderItem)}
)}
{/* Eigener sichtbarer Toggle: Davids interner Button wird vom overflow-hidden der Bar verdeckt. */}
: } onClick={toggleCollapsed} aria-label={t("components.sidebar.toggle")} />
{/* Mobile: Tabs-Leiste (unverändert) */}
!f.hidden) .map((i) => { return { name: i.name, routePath: i.href }; })} className="grow" />
{/* Hauptbereich */}
{children}
); }