import { IconBook, IconDeviceDesktop, IconHome, IconLock, IconSettings, IconShield, IconUser, } from "@tabler/icons-react"; import cn from "classnames"; import React from "react"; import { HasPermission, NavLink } from "src/components"; import { intl } from "src/locale"; interface MenuItem { label: string; icon?: React.ElementType; to?: string; items?: MenuItem[]; permission?: string; permissionType?: "view" | "manage"; } const menuItems: MenuItem[] = [ { to: "/", icon: IconHome, label: "dashboard.title", }, { icon: IconDeviceDesktop, label: "hosts.title", items: [ { to: "/nginx/proxy", label: "proxy-hosts.title", permission: "proxyHosts", permissionType: "view", }, { to: "/nginx/redirection", label: "redirection-hosts.title", permission: "redirectionHosts", permissionType: "view", }, { to: "/nginx/stream", label: "streams.title", permission: "streams", permissionType: "view", }, { to: "/nginx/404", label: "dead-hosts.title", permission: "deadHosts", permissionType: "view", }, ], }, { to: "/access", icon: IconLock, label: "access.title", permission: "accessLists", permissionType: "view", }, { to: "/certificates", icon: IconShield, label: "certificates.title", permission: "certificates", permissionType: "view", }, { to: "/users", icon: IconUser, label: "users.title", permission: "admin", }, { to: "/audit-log", icon: IconBook, label: "auditlog.title", permission: "admin", }, { to: "/settings", icon: IconSettings, label: "settings.title", permission: "admin", }, ]; const getMenuItem = (item: MenuItem, onClick?: () => void) => { if (item.items && item.items.length > 0) { return getMenuDropown(item, onClick); } return (
  • {item.icon && React.createElement(item.icon, { height: 24, width: 24 })} {intl.formatMessage({ id: item.label })}
  • ); }; const getMenuDropown = (item: MenuItem, onClick?: () => void) => { const cns = cn("nav-item", "dropdown"); return (
  • {item.items?.map((subitem, idx) => { return ( {intl.formatMessage({ id: subitem.label })} ); })}
  • ); }; export function SiteMenu() { // This is hacky AF. But that's the price of using a non-react UI kit. const closeMenus = () => { const navMenus = document.querySelectorAll(".nav-item.dropdown"); navMenus.forEach((menu) => { menu.classList.remove("show"); const dropdown = menu.querySelector(".dropdown-menu"); if (dropdown) { dropdown.classList.remove("show"); } }); }; return (
      {menuItems.length > 0 && menuItems.map((item) => { return getMenuItem(item, closeMenus); })}
    ); }