import { BarChart3, Bell, BookOpen, ChevronLeft, ChevronRight, ClipboardList, CreditCard, LayoutDashboard, LogOut, UserCircle2, Users, UsersRound, Settings, Star, X, } from "lucide-react"; import { type ComponentType, useEffect, useState } from "react"; import { NavLink } from "react-router-dom"; import { cn } from "../../lib/utils"; import { SidebarLogo } from "./SidebarLogo"; import { getUnreadCount } from "../../api/notifications.api"; import { useTeamPermissions } from "../../hooks/useTeamPermissions"; import { hasRatingsPermission } from "../../lib/ratingsPermissions"; import { hasPersonaPermission } from "../../lib/personasPermissions"; import { hasActivityLogPermission } from "../../lib/activityLogPermissions"; import { hasExportPermission } from "../../lib/exportPermissions"; import { SidebarNavGroup } from "./SidebarNavGroup"; import { useAdminAccess } from "../../hooks/useAdminAccess"; import { isNavEntryAllowedForTeamRole } from "../../lib/adminAccess"; type NavLinkItem = { kind: "link"; label: string; to: string; icon: ComponentType<{ className?: string }>; permission?: string; }; type NavGroupItem = { kind: "group"; label: string; basePath: string; activePaths?: string[]; icon: ComponentType<{ className?: string }>; children: { label: string; to: string; end?: boolean }[]; }; type NavSectionItem = { kind: "section"; label: string; }; type NavEntry = NavLinkItem | NavGroupItem | NavSectionItem; const navEntries: NavEntry[] = [ { kind: "section", label: "Overview" }, { kind: "link", label: "Dashboard", to: "/dashboard", icon: LayoutDashboard }, { kind: "link", label: "Analytics", to: "/analytics", icon: BarChart3 }, { kind: "section", label: "People" }, { kind: "group", label: "Users & access", basePath: "/users", activePaths: ["/users", "/team"], icon: Users, children: [ { label: "All users", to: "/users/list" }, { label: "Team members", to: "/team" }, ], }, { kind: "section", label: "Learning content" }, { kind: "group", label: "Content", basePath: "/new-content", activePaths: ["/new-content"], icon: BookOpen, children: [ { label: "Learning content", to: "/new-content", end: true }, { label: "Reorder structure", to: "/new-content/reorder" }, { label: "Question types", to: "/new-content/question-types" }, { label: "Initial assessment", to: "/new-content/initial-assessment" }, ], }, { kind: "link", label: "Personas", to: "/personas", icon: UsersRound, permission: "personas.list", }, { kind: "section", label: "Communications" }, { kind: "group", label: "Notifications", basePath: "/notifications", icon: Bell, children: [ { label: "Inbox", to: "/notifications", end: true }, { label: "All notifications", to: "/notifications/all" }, { label: "Email templates", to: "/notifications/email-templates" }, { label: "Send notification", to: "/notifications/create" }, { label: "Scheduled", to: "/notifications/scheduled" }, ], }, { kind: "section", label: "Operations" }, { kind: "link", label: "Payments", to: "/payments", icon: CreditCard }, { kind: "link", label: "Subscriptions export", to: "/subscriptions/export", icon: CreditCard, permission: "subscriptions.export", }, { kind: "link", label: "Activity log", to: "/user-log", icon: ClipboardList, permission: "activity_logs.list", }, { kind: "link", label: "App reviews", to: "/app-reviews", icon: Star, permission: "ratings.list_by_target", }, { kind: "section", label: "Account" }, { kind: "link", label: "Profile", to: "/profile", icon: UserCircle2 }, { kind: "link", label: "Settings", to: "/settings", icon: Settings }, ]; type SidebarProps = { isOpen: boolean; isCollapsed: boolean; onToggleCollapse: () => void; onClose: () => void; }; export function Sidebar({ isOpen, isCollapsed, onToggleCollapse, onClose, }: SidebarProps) { const { permissions, hasPermission, loading: permissionsLoading, } = useTeamPermissions(); const [unreadCount, setUnreadCount] = useState(0); const { teamRole: sessionTeamRole } = useAdminAccess(); useEffect(() => { const fetchUnread = async () => { try { const res = await getUnreadCount(); setUnreadCount(res.data.unread); } catch { // silently fail } }; fetchUnread(); window.addEventListener("notifications-updated", fetchUnread); return () => window.removeEventListener("notifications-updated", fetchUnread); }, []); const unreadBadge = unreadCount > 0 && ( {unreadCount > 99 ? "99+" : unreadCount} ); const collapsedUnreadDot = unreadCount > 0 && ( ); return ( <>
> ); }