"use client"; import { useState, useEffect } from "react"; import { LayoutDashboard, Package, Users, Wallet, ShoppingCart, FileText, Settings, Languages, AlertTriangle, LogOut, ShieldCheck, Shield, Bot, Target, FolderTree, } from "lucide-react"; import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarHeader, SidebarMenu, SidebarMenuBadge, SidebarMenuButton, SidebarMenuItem, SidebarRail, SidebarSeparator, } from "@/components/ui/sidebar"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { useAuthStore } from "@/stores/auth-store"; const mainNav = [ { title: "Dashboard", href: "/", icon: LayoutDashboard, shortcut: "1" }, { title: "Пользователи", href: "/users", icon: Users, shortcut: "2" }, { title: "Кошельки", href: "/wallets", icon: Wallet, shortcut: "3" }, { title: "Покупки", href: "/purchases", icon: ShoppingCart, badge: true, shortcut: "4" }, { title: "Аудит", href: "/audit", icon: FileText, shortcut: "5" }, ]; const catalogNav = [ { title: "Каталог товаров", href: "/catalog", icon: FolderTree, shortcut: "6" }, ]; const automationNav = [ { title: "AI Chatbot", href: "/chatbot", icon: Bot }, { title: "Лиды", href: "/leads", icon: Target }, ]; const systemNav = [ { title: "Настройки", href: "/settings", icon: Settings, shortcut: "9" }, { title: "Локали", href: "/locales", icon: Languages }, ]; function usePendingCount(isAuthenticated: boolean) { const [count, setCount] = useState(0); useEffect(() => { if (!isAuthenticated) return; let cancelled = false; const load = () => { fetch("/api/purchases/bulk?status=pending&limit=1") .then((res) => (res.ok ? res.json() : null)) .then((data) => { if (!cancelled && data) setCount(data.total ?? 0); }) .catch(() => {}); }; load(); window.addEventListener("focus", load); return () => { cancelled = true; window.removeEventListener("focus", load); }; }, [isAuthenticated]); return count; } function useConnectionStatus() { const { checkSession } = useAuthStore(); const [connected, setConnected] = useState(false); useEffect(() => { let cancelled = false; const check = () => { checkSession().then((valid) => { if (!cancelled) setConnected(valid); }); }; check(); window.addEventListener("focus", check); return () => { cancelled = true; window.removeEventListener("focus", check); }; }, [checkSession]); return connected; } function useHashPath() { const [hash, setHash] = useState(""); useEffect(() => { const update = () => setHash(window.location.hash.slice(1) || "/"); update(); window.addEventListener("hashchange", update); return () => window.removeEventListener("hashchange", update); }, []); return hash; } export function AdminSidebar() { const pathname = useHashPath(); const { role, logout, isAuthenticated } = useAuthStore(); const pendingCount = usePendingCount(isAuthenticated); const connected = useConnectionStatus(); return ( Основное {mainNav.map((item) => ( {item.title} {item.shortcut && ( {item.shortcut} )} {item.badge && pendingCount > 0 && ( {pendingCount} )} {item.badge && pendingCount > 0 && ( )} ))} Каталог товаров {catalogNav.map((item) => ( {item.title} {item.shortcut && ( {item.shortcut} )} ))} Автоматизация {automationNav.map((item) => ( {item.title} ))} Система {systemNav.map((item) => ( {item.title} {item.shortcut && ( {item.shortcut} )} ))} {role === "super_admin" && ( Danger Zone )}
{role === "super_admin" ? ( ) : ( )}
{role === "super_admin" ? "Super Admin" : "Admin"} {role}
{connected ? "Connected" : "Disconnected"}
); }