import {
AppstoreOutlined,
BookOutlined,
FileTextOutlined,
HistoryOutlined,
LogoutOutlined,
ScanOutlined,
SettingOutlined,
SnippetsOutlined,
UsergroupAddOutlined,
} from "@ant-design/icons";
import { Badge, Divider, Menu } from "antd";
import Sider from "antd/es/layout/Sider";
import { useEffect, useState } from "react";
import { useLocation, useNavigate } from "react-router-dom";
import PropTypes from "prop-types";
import { Constants, hasOnePermission, hasPermission } from "../../utils";
import { useTranslation } from "react-i18next";
import { MyUserAvatar } from "../MyAvatar";
import { useSideBarContext } from "../../Contexts/SideBarContext";
import { useAppContext } from "../../Contexts/AppContext";
export default function SideMenu({
userSession,
setUserSession,
isSideMenuCollapsed,
setIsSideMenuCollapsed,
}) {
const appContext = useAppContext();
const sideBarContext = useSideBarContext();
const location = useLocation();
const [selectedKeys, setSelectedKeys] = useState("/");
const { t } = useTranslation();
useEffect(() => setSelectedKeys(location.pathname), [location.pathname]);
const navigate = useNavigate();
const getCurrentUsedScannerName = () => {
/*const scannerName = webSocketContext.Scanners.find(
(scanner) => scanner.UsedByUserId === getUserId()
)?.Name;
return scannerName === undefined
? t("sideMenu.adminArea.noScannerSelected")
: scannerName; */
// TODO: handle scanner name
return Constants.LOADING;
};
const getFirstMenuItems = () => {
// dashboard
let items = [
{
label: t("sideMenu.dashboard"),
icon: ,
key: "/",
},
];
// equipment documentation
if (
hasPermission(
appContext.userPermissions,
Constants.PERMISSIONS.EQUIPMENT_DOCUMENTATION.VIEW
)
) {
items.push({
label: t("sideMenu.equipmentDocumentation"),
icon: ,
key: "/equipment-documentation",
});
}
// group tasks
let groupTasksGroup = {
label: t("sideMenu.groupTasks.menuCategory"),
type: "group",
icon: ,
children: [],
};
let groupTasks = {
label: t("sideMenu.groupTasks.overview"),
icon: ,
key: "/group-tasks",
children: [],
};
for (let i = 0; i < sideBarContext.availableCategoryGroups.length; i++) {
groupTasks.children.push({
label: sideBarContext.availableCategoryGroups[i],
icon: ,
key: `/group-tasks/${sideBarContext.availableCategoryGroups[i]}`,
});
}
groupTasksGroup.children.push(groupTasks);
if (
hasPermission(
appContext.userPermissions,
Constants.PERMISSIONS.GROUP_TASKS.HISTORY
)
) {
groupTasksGroup.children.push({
label: t("sideMenu.groupTasks.history"),
icon: ,
key: Constants.ROUTE_PATHS.GROUP_TASKS_HISTORY,
});
}
items.push(groupTasksGroup);
// admin area
if (
hasOnePermission(
appContext.userPermissions,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.CREATE_NEW_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.UPDATE_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.DELETE_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.MOVE_ROLE_UP_DOWN,
Constants.PERMISSIONS.ADMIN_AREA.LOGS
)
) {
items.push({
type: "divider",
});
let adminArea = {
label: t("sideMenu.adminArea.menuCategory"),
icon: ,
key: "/admin-area",
children: [],
};
if (
hasOnePermission(
appContext.userPermissions,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.CREATE_NEW_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.UPDATE_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.DELETE_ROLE,
Constants.PERMISSIONS.ADMIN_AREA.ROLES.MOVE_ROLE_UP_DOWN
)
) {
adminArea.children.push({
label: t("sideMenu.adminArea.roles"),
icon: ,
key: "/admin-area/roles",
});
}
if (
hasPermission(
appContext.userPermissions,
Constants.PERMISSIONS.ADMIN_AREA.LOGS
)
) {
adminArea.children.push({
label: t("sideMenu.adminArea.logs"),
icon: ,
key: "/admin-area/logs",
});
}
items.push(adminArea);
}
return items;
};
const getSecondMenuItems = () => {
let items = [];
// scanner
if (
hasPermission(
appContext.userPermissions,
Constants.PERMISSIONS.SCANNER.USE_SCANNERS
)
) {
items.push({
icon: ,
label: getCurrentUsedScannerName(),
key: "/scanners",
});
}
// connection status, userprofile, logout
items.push(
{
icon: (
),
key: "/users",
},
{
label: ` ${sideBarContext.username}`,
icon: ,
key: "/user-profile",
},
{
label: t("sideMenu.logout"),
icon: ,
onClick: () => {
setUserSession();
window.location.href = "/";
fetch(`${Constants.API_ADDRESS}/user/auth/logout`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-Authorization": userSession,
},
}).catch(console.error);
},
key: "/",
}
);
return items;
};
return (
setIsSideMenuCollapsed(collapsed)}
>
C
O
M
P
A
N
Y
Admin Dashboard
);
}
/*
J
A
N
N
E
X
*/
SideMenu.propTypes = {
setUserSession: PropTypes.func.isRequired,
};