"use client"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { useEffect, useMemo, useState } from "react"; import { BarChart3, Bell, DollarSign, LayoutGrid, LogOut, Settings, Wrench, X } from "lucide-react"; import type { LucideIcon } from "lucide-react"; import { useI18n } from "@/lib/i18n/useI18n"; type NavItem = { href: string; labelKey: string; icon: LucideIcon; ownerOnly?: boolean; }; const items: NavItem[] = [ { href: "/overview", labelKey: "nav.overview", icon: LayoutGrid }, { href: "/machines", labelKey: "nav.machines", icon: Wrench }, { href: "/reports", labelKey: "nav.reports", icon: BarChart3 }, { href: "/alerts", labelKey: "nav.alerts", icon: Bell }, { href: "/financial", labelKey: "nav.financial", icon: DollarSign, ownerOnly: true }, { href: "/settings", labelKey: "nav.settings", icon: Settings }, ]; type SidebarProps = { variant?: "desktop" | "drawer"; onNavigate?: () => void; onClose?: () => void; }; export function Sidebar({ variant = "desktop", onNavigate, onClose }: SidebarProps) { const pathname = usePathname(); const router = useRouter(); const { t } = useI18n(); const [me, setMe] = useState<{ user?: { name?: string | null; email?: string | null }; org?: { name?: string | null }; membership?: { role?: string | null }; } | null>(null); useEffect(() => { let alive = true; async function loadMe() { try { const res = await fetch("/api/me", { cache: "no-store" }); const data = await res.json().catch(() => ({})); if (alive && res.ok && data?.ok) { setMe(data); } } catch { if (alive) setMe(null); } } loadMe(); return () => { alive = false; }; }, []); async function onLogout() { await fetch("/api/logout", { method: "POST" }); router.push("/login"); router.refresh(); onNavigate?.(); } const roleKey = (me?.membership?.role || "MEMBER").toLowerCase(); const isOwner = roleKey === "owner"; const visibleItems = useMemo(() => items.filter((it) => !it.ownerOnly || isOwner), [isOwner]); useEffect(() => { visibleItems.forEach((it) => { router.prefetch(it.href); }); }, [router, visibleItems]); const shellClass = [ "relative z-20 flex flex-col border-r border-white/10 bg-black/40", variant === "desktop" ? "hidden md:flex h-screen w-64" : "flex h-full w-72 max-w-[85vw]", ].join(" "); return ( ); }