import React, { useState, useEffect } from 'react';
import { Link, useLocation } from 'react-router-dom';
import { Menu, Typography, Image } from 'antd';
import { getSessionData } from '../components/Global/Formatter';
import {
HomeOutlined,
DatabaseOutlined,
SettingOutlined,
UserOutlined,
AntDesignOutlined,
ShoppingCartOutlined,
ShoppingOutlined,
HistoryOutlined,
DollarOutlined,
RollbackOutlined,
ProductOutlined,
TagOutlined,
AppstoreOutlined,
MobileOutlined,
WarningOutlined,
LineChartOutlined,
FileTextOutlined,
BellOutlined,
AlertOutlined,
SafetyOutlined,
TeamOutlined,
ClockCircleOutlined,
CalendarOutlined,
DesktopOutlined,
NodeExpandOutlined,
GroupOutlined,
SlidersOutlined,
SnippetsOutlined,
} from '@ant-design/icons';
const { Text } = Typography;
const allItems = [
{
key: 'home',
icon: ,
label: (
Home
),
},
{
key: 'dashboard-svg',
icon: ,
label: 'Dashboard',
children: [
{
key: 'dashboard-svg-overview',
icon: ,
label: Overview,
},
{
key: 'dashboard-svg-compressor-a',
icon: ,
label: Compressor A,
},
{
key: 'dashboard-svg-compressor-b',
icon: ,
label: Compressor B,
},
{
key: 'dashboard-svg-compressor-c',
icon: ,
label: Compressor C,
},
{
key: 'dashboard-svg-airdryer-a',
icon: ,
label: Air Dryer A,
},
{
key: 'dashboard-svg-airdryer-b',
icon: ,
label: Air Dryer B,
},
{
key: 'dashboard-svg-airdryer-c',
icon: ,
label: Air Dryer C,
},
],
},
{
key: 'master',
icon: ,
label: 'Master',
children: [
{
key: 'master-plant-sub-section',
icon: ,
label: Plant Sub Section,
},
{
key: 'master-brand-device',
icon: ,
label: Brand Device,
},
{
key: 'master-device',
icon: ,
label: Device,
},
{
key: 'master-unit',
icon: ,
label: Unit,
},
{
key: 'master-tag',
icon: ,
label: Tag,
},
{
key: 'master-status',
icon: ,
label: Status,
},
{
key: 'master-shift',
icon: ,
label: Shift,
},
],
},
{
key: 'report',
icon: ,
label: 'Report',
children: [
{
key: 'report-trending',
icon: ,
label: Trending,
},
{
key: 'report-report',
icon: ,
label: Report,
},
],
},
{
key: 'history',
icon: ,
label: 'History',
children: [
{
key: 'history-alarm',
icon: ,
label: Alarm,
},
{
key: 'history-event',
icon: ,
label: Event,
},
],
},
{
key: 'notification',
icon: ,
label: (
Notification
),
},
{
key: 'role',
icon: ,
label: (
Role
),
},
{
key: 'user',
icon: ,
label: (
User
),
},
{
key: 'jadwal-shift',
icon: ,
label: (
Jadwal Shift
),
},
];
const LayoutMenu = () => {
const location = useLocation();
const [stateOpenKeys, setStateOpenKeys] = useState(['home']);
const [selectedKeys, setSelectedKeys] = useState(['home']);
// Function to get menu key from current path
const getMenuKeyFromPath = (pathname) => {
// Remove leading slash and split path
const pathParts = pathname.replace(/^\//, '').split('/');
// Handle different route patterns
if (pathname === '/dashboard/home') return 'home';
if (pathname === '/user') return 'user';
if (pathname === '/role') return 'role';
if (pathname === '/notification') return 'notification';
if (pathname === '/jadwal-shift') return 'jadwal-shift';
// Handle master routes
if (pathname.startsWith('/master/')) {
const subPath = pathParts[1];
return `master-${subPath}`;
}
// Handle dashboard svg routes
if (pathname.startsWith('/dashboard-svg/')) {
const subPath = pathParts[1];
return `dashboard-svg-${subPath}`;
}
// Handle report routes
if (pathname.startsWith('/report/')) {
const subPath = pathParts[1];
return `report-${subPath}`;
}
// Handle history routes
if (pathname.startsWith('/history/')) {
const subPath = pathParts[1];
return `history-${subPath}`;
}
return 'home'; // default
};
// Function to get parent key from menu key
const getParentKey = (key) => {
if (key.startsWith('master-')) return 'master';
if (key.startsWith('dashboard-svg-')) return 'dashboard-svg';
if (key.startsWith('report-')) return 'report';
if (key.startsWith('history-')) return 'history';
return null;
};
// Update selected and open keys when route changes
useEffect(() => {
const currentKey = getMenuKeyFromPath(location.pathname);
setSelectedKeys([currentKey]);
const parentKey = getParentKey(currentKey);
// If current menu has parent, open it. Otherwise, close all dropdowns
if (parentKey) {
setStateOpenKeys([parentKey]);
} else {
setStateOpenKeys([]);
}
}, [location.pathname]);
const getLevelKeys = (items1) => {
const key = {};
const func = (items2, level = 1) => {
items2.forEach((item) => {
if (item.key) {
key[item.key] = level;
}
if (item.children) {
func(item.children, level + 1);
}
});
};
func(items1);
return key;
};
const levelKeys = getLevelKeys(allItems);
const onOpenChange = (openKeys) => {
const currentOpenKey = openKeys.find((key) => stateOpenKeys.indexOf(key) === -1);
if (currentOpenKey !== undefined) {
const repeatIndex = openKeys
.filter((key) => key !== currentOpenKey)
.findIndex((key) => levelKeys[key] === levelKeys[currentOpenKey]);
setStateOpenKeys(
openKeys
.filter((_, index) => index !== repeatIndex)
.filter((key) => levelKeys[key] <= levelKeys[currentOpenKey])
);
} else {
setStateOpenKeys(openKeys);
}
};
const session = getSessionData();
const isAdmin = session?.user?.user_id;
const karyawan = () => {
return allItems
.filter(
(item) => item.key !== 'setting'
// tambahkan menu jika terdapat menu yang di sembunyikan dari user karyawan
// && item.key !== 'master'
// && item.key !== 'master'
)
.map((item) => {
if (item.key === 'master') {
return {
...item,
// buka command dibawah jika terdapat sub menu yang di sembunyikan
// children: item.children.filter(
// child => child.key !== 'master-product'
// tambahkan menu jika terdapat menu yang di sembunyikan dari user karyawan
// && child.key !== 'master-service'
// )
};
}
return item;
});
};
const items = isAdmin === 1 ? allItems : karyawan();
return (
);
};
export default LayoutMenu;