import React, { useEffect, useState } from "react"; import { Box, Collapse, Divider, IconButton, List, ListItemButton, ListItemIcon, ListItemText, ListSubheader, Menu, MenuItem, Stack, Tooltip, Typography, } from "@mui/material"; import { useLocation, useNavigate } from "react-router"; import { useTheme } from "@emotion/react"; import { useDispatch, useSelector } from "react-redux"; import { clearAuthState } from "../../Features/Auth/authSlice"; import { setMode, toggleSidebar } from "../../Features/UI/uiSlice"; import { clearUptimeMonitorState } from "../../Features/UptimeMonitors/uptimeMonitorsSlice"; import Avatar from "../Avatar"; import LockSvg from "../../assets/icons/lock.svg?react"; import UserSvg from "../../assets/icons/user.svg?react"; import TeamSvg from "../../assets/icons/user-two.svg?react"; import LogoutSvg from "../../assets/icons/logout.svg?react"; import Support from "../../assets/icons/support.svg?react"; import Dashboard from "../../assets/icons/dashboard.svg?react"; import Account from "../../assets/icons/user-edit.svg?react"; import StatusPages from "../../assets/icons/status-pages.svg?react"; import Maintenance from "../../assets/icons/maintenance.svg?react"; import Monitors from "../../assets/icons/monitors.svg?react"; import Incidents from "../../assets/icons/incidents.svg?react"; import Integrations from "../../assets/icons/integrations.svg?react"; import PageSpeed from "../../assets/icons/page-speed.svg?react"; import Settings from "../../assets/icons/settings.svg?react"; import ArrowDown from "../../assets/icons/down-arrow.svg?react"; import ArrowUp from "../../assets/icons/up-arrow.svg?react"; import ArrowRight from "../../assets/icons/right-arrow.svg?react"; import ArrowLeft from "../../assets/icons/left-arrow.svg?react"; import DotsVertical from "../../assets/icons/dots-vertical.svg?react"; import ChangeLog from "../../assets/icons/changeLog.svg?react"; import Docs from "../../assets/icons/docs.svg?react"; import Folder from "../../assets/icons/folder.svg?react"; import "./index.css"; const menu = [ { name: "Dashboard", icon: , nested: [ { name: "Monitors", path: "monitors", icon: }, { name: "Pagespeed", path: "pagespeed", icon: }, ], }, { name: "Incidents", path: "incidents", icon: }, // { name: "Status pages", path: "status", icon: }, { name: "Maintenance", path: "maintenance", icon: }, // { name: "Integrations", path: "integrations", icon: }, { name: "Account", icon: , nested: [ { name: "Profile", path: "account/profile", icon: }, { name: "Password", path: "account/password", icon: }, { name: "Team", path: "account/team", icon: }, ], }, { name: "Other", icon: , nested: [ { name: "Settings", path: "settings", icon: }, { name: "Support", path: "support", icon: }, { name: "Docs", path: "docs", icon: }, { name: "Changelog", path: "changelog", icon: }, ], }, ]; const URL_MAP = { support: "https://github.com/bluewave-labs/bluewave-uptime/issues", docs: "https://bluewavelabs.gitbook.io/uptime-manager", changelog: "https://github.com/bluewave-labs/bluewave-uptime/releases", }; const PATH_MAP = { monitors: "Dashboard", pagespeed: "Dashboard", account: "Account", settings: "Other", }; /** * @component * Sidebar component serves as a sidebar containing a menu. * * @returns {JSX.Element} The JSX element representing the Sidebar component. */ function Sidebar() { const theme = useTheme(); const navigate = useNavigate(); const location = useLocation(); const dispatch = useDispatch(); const authState = useSelector((state) => state.auth); const collapsed = useSelector((state) => state.ui.sidebar.collapsed); const [open, setOpen] = useState({ Dashboard: false, Account: false, Other: false }); const [anchorEl, setAnchorEl] = useState(null); const [popup, setPopup] = useState(); const { user } = useSelector((state) => state.auth); // Remove demo password if demo const accountMenuItem = menu.find((item) => item.name === "Account"); if (user.role?.includes("demo") && accountMenuItem) { accountMenuItem.nested = accountMenuItem.nested.filter((item) => { return item.name !== "Password"; }); } const openPopup = (event, id) => { setAnchorEl(event.currentTarget); setPopup(id); }; const closePopup = () => { setAnchorEl(null); }; /** * Handles logging out the user * */ const logout = async () => { // Clear auth state dispatch(clearAuthState()); dispatch(clearUptimeMonitorState()); navigate("/login"); }; useEffect(() => { const matchedKey = Object.keys(PATH_MAP).find((key) => location.pathname.includes(key) ); if (matchedKey) { setOpen((prev) => ({ ...prev, [PATH_MAP[matchedKey]]: true })); } }, []); return ( BU BlueWave Uptime { setOpen((prev) => Object.fromEntries(Object.keys(prev).map((key) => [key, false])) ); dispatch(toggleSidebar()); }} > {collapsed ? : } {/* menu */} Menu } sx={{ px: theme.spacing(6) }} > {menu.map((item) => item.path ? ( navigate(`/${item.path}`)} sx={{ height: "37px", gap: theme.spacing(4), borderRadius: theme.shape.borderRadius, px: theme.spacing(4), }} > {item.icon} {item.name} ) : collapsed ? ( openPopup(event, item.name)} sx={{ position: "relative", gap: theme.spacing(4), borderRadius: theme.shape.borderRadius, px: theme.spacing(4), }} > {item.icon} {item.name} {item.nested.map((child) => { if ( child.name === "Team" && authState.user?.role && !authState.user.role.includes("superadmin") ) { return null; } return ( { const url = URL_MAP[child.path]; if (url) { window.open(url, "_blank", "noreferrer"); } else { navigate(`/${child.path}`); } closePopup(); }} sx={{ gap: theme.spacing(4), opacity: 0.9, "& svg": { "& path": { stroke: theme.palette.other.icon, strokeWidth: 1.1, }, }, }} > {child.icon} {child.name} ); })} ) : ( setOpen((prev) => ({ ...Object.fromEntries(Object.keys(prev).map((key) => [key, false])), [item.name]: !prev[item.name], })) } sx={{ gap: theme.spacing(4), borderRadius: theme.shape.borderRadius, px: theme.spacing(4), }} > {item.icon} {item.name} {open[`${item.name}`] ? : } {item.nested.map((child) => { if ( child.name === "Team" && authState.user?.role && !authState.user.role.includes("superadmin") ) { return null; } return ( { const url = URL_MAP[child.path]; if (url) { window.open(url, "_blank", "noreferrer"); } else { navigate(`/${child.path}`); } }} sx={{ gap: theme.spacing(4), borderRadius: theme.shape.borderRadius, pl: theme.spacing(4), "&::before": { content: `""`, position: "absolute", top: 0, left: "-7px", height: "100%", borderLeft: 1, borderLeftColor: theme.palette.other.line, }, "&:last-child::before": { height: "50%", }, "&::after": { content: `""`, position: "absolute", top: "45%", left: "-8px", height: "3px", width: "3px", borderRadius: "50%", backgroundColor: theme.palette.other.line, }, "&.selected-path::after": { backgroundColor: theme.palette.text.tertiary, transform: "scale(1.2)", }, }} > {child.icon} {child.name} ); })} ) )} {collapsed ? ( <> openPopup(event, "logout")} sx={{ p: 0, "&:focus": { outline: "none" } }} > ) : ( <> {authState.user?.firstName} {authState.user?.lastName} {authState.user?.role} openPopup(event, "logout")} > )} {collapsed && ( {authState.user?.firstName} {authState.user?.lastName} {authState.user?.role} )} {collapsed && } {/* { dispatch(setMode("light")); closePopup(); }} > Light { dispatch(setMode("dark")); closePopup(); }} > Dark */} Log out ); } export default Sidebar;