"use client"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { BarChart3, LayoutGrid, LogOut, Settings, Wrench } from "lucide-react"; import { useI18n } from "@/lib/i18n/useI18n"; const THEME_COOKIE = "mis_theme"; const SunIcon = ({ className }: { className?: string }) => ( ); const MoonIcon = ({ className }: { className?: string }) => ( ); const items = [ { href: "/overview", labelKey: "nav.overview", icon: LayoutGrid }, { href: "/machines", labelKey: "nav.machines", icon: Wrench }, { href: "/reports", labelKey: "nav.reports", icon: BarChart3 }, { href: "/settings", labelKey: "nav.settings", icon: Settings }, ] as const; export function Sidebar() { const pathname = usePathname(); const router = useRouter(); const { locale, setLocale, t } = useI18n(); const [theme, setTheme] = useState<"dark" | "light">("dark"); 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; }; }, []); useEffect(() => { const current = document.documentElement.getAttribute("data-theme"); if (current === "light" || current === "dark") { setTheme(current); } }, []); function applyTheme(next: "light" | "dark") { document.documentElement.setAttribute("data-theme", next); document.cookie = `${THEME_COOKIE}=${next}; Path=/; Max-Age=31536000; SameSite=Lax`; setTheme(next); } function toggleTheme() { applyTheme(theme === "light" ? "dark" : "light"); } async function onLogout() { await fetch("/api/logout", { method: "POST" }); router.push("/login"); router.refresh(); } const roleKey = (me?.membership?.role || "MEMBER").toLowerCase(); return ( ); }