import { useState, useMemo } from 'react'
import { NavLink, Box, Text, Stack, Badge } from '@mantine/core'
import {
IconDashboard,
IconFileCheck,
IconSettings,
IconChevronRight,
IconList,
IconUsers,
IconBuilding,
IconShield,
IconKey,
IconCategory,
IconBriefcase,
IconUserCheck,
IconGitBranch,
IconUserCog,
IconCheck,
IconBellRinging,
IconBlocks,
IconHistory, IconChartBar,
} from '@tabler/icons-react'
import { useLocation, useNavigate } from 'react-router-dom'
import { useAuth } from '@/hooks/useAuth'
interface MenuItem {
label: string
icon?: any
path?: string
children?: MenuItem[]
adminOnly?: boolean // скрывать от наблюдателей
}
const menuItems: MenuItem[] = [
{ label: 'Обзор', icon: IconDashboard, path: '/' },
{ label: 'Администраторы', icon: IconUserCog, path: '/admins', adminOnly: true },
{ label: 'Уведомления', icon: IconBellRinging, path: '/notifications', adminOnly: true },
{ label: 'Журнал действий', icon: IconHistory, path: '/logs', adminOnly: true },
{
label: 'Заказы на месяц',
icon: IconChartBar,
path: '/monthly-orders-settings',
adminOnly: true, // Скрываем от наблюдателей
// Если нужно скрыть от менеджеров, можно добавить флаг analystOnly: true и фильтровать
},
{
label: 'Система согласований',
icon: IconFileCheck,
children: [
{ label: 'Панель управления', icon: IconDashboard, path: '/approval-dash' },
{ label: 'Заявки', icon: IconList, path: '/approvals' },
{ label: 'Задачи', icon: IconCheck, path: '/approval-tasks' },
{ label: 'Пользователи', icon: IconUsers, path: '/users' },
{ label: 'Отделы', icon: IconBuilding, path: '/departments', adminOnly: true },
{ label: 'Роли', icon: IconShield, path: '/roles', adminOnly: true },
{ label: 'Права', icon: IconKey, path: '/permissions', adminOnly: true },
{
label: 'Темы согласований',
icon: IconBriefcase,
adminOnly: true,
children: [
// { label: 'Категории', icon: IconCategory, path: '/categories' },
{ label: 'Настройки тем', icon: IconBriefcase, path: '/topics' },
{ label: 'Исполнители', icon: IconUserCheck, path: '/executors' },
]
},
{ label: 'Конструктор тем', icon: IconBlocks, path: '/nodes', adminOnly: true },
{ label: 'Иерархия', icon: IconGitBranch, path: '/hierarchies', adminOnly: true },
]
},
{ label: 'Настройки', icon: IconSettings, path: '/settings'},
]
function filterMenuByRole(items: MenuItem[], isObserver: boolean): MenuItem[] {
return items
.filter((item) => {
if (isObserver && item.adminOnly) return false
return true
})
.map((item) => {
if (item.children) {
const filteredChildren = filterMenuByRole(item.children, isObserver)
// Если после фильтрации детей не осталось — скрываем родителя
if (filteredChildren.length === 0) return null
return { ...item, children: filteredChildren }
}
return item
})
.filter((item): item is MenuItem => item !== null)
}
function MenuItemLink({ item, level }: { item: MenuItem; level: number }) {
const location = useLocation()
const navigate = useNavigate()
const [opened, setOpened] = useState(false)
const hasChildren = item.children && item.children.length > 0
const isActive = item.path ? location.pathname === item.path : false
const hasActiveChild = item.children?.some(child =>
child.path ? location.pathname === child.path : false
) || false
const paddingLeft = 16 + level * 20
if (hasChildren) {
return (
: undefined}
rightSection={
}
active={hasActiveChild}
onClick={() => setOpened(!opened)}
styles={{
root: {
paddingLeft,
paddingRight: 12,
borderRadius: '8px',
marginBottom: 2,
'&:hover': {
backgroundColor: 'var(--mantine-color-gray-0)',
},
},
}}
/>
{opened && (
{item.children!.map((child) => (
))}
)}
)
}
return (
: undefined}
active={isActive}
onClick={() => {
if (item.path) navigate(item.path)
}}
styles={{
root: {
paddingLeft,
paddingRight: 12,
borderRadius: '8px',
marginBottom: 2,
'&:hover': {
backgroundColor: 'var(--mantine-color-gray-0)',
},
},
label: {
fontWeight: isActive ? 600 : 400,
},
}}
/>
)
}
export default function Sidebar() {
const { user } = useAuth()
const isObserver = user?.is_observer === true
const isAnalyst = user?.is_analyst === true || user?.is_root === true
// 🆕 Фильтруем меню в зависимости от роли
const filteredMenuItems = useMemo(
() => filterMenuByRole(menuItems, isObserver),
[isObserver]
)
return (
Главное меню
{/* 🆕 Индикатор режима наблюдателя */}
{isObserver && (
Режим наблюдателя
)}
{filteredMenuItems.map((item) => (
))}
)
}