Projekt

Obecné

Profil

Stáhnout (3.05 KB) Statistiky
| Větev: | Tag: | Revize:
1 456b0111 Vaclav Honzik
import { OverridableComponent } from '@mui/material/OverridableComponent'
2
import HomeIcon from '@mui/icons-material/Home'
3
import { MenuBook } from '@mui/icons-material'
4
import LibraryBooksIcon from '@mui/icons-material/LibraryBooks'
5
import MapIcon from '@mui/icons-material/Map'
6
import PersonIcon from '@mui/icons-material/Person'
7
import LoginIcon from '@mui/icons-material/Login'
8
import { SvgIconTypeMap } from '@mui/material'
9 0d90d67b Vaclav Honzik
import DataSaverOffIcon from '@mui/icons-material/DataSaverOff'
10
import LogoutIcon from '@mui/icons-material/Logout'
11 456b0111 Vaclav Honzik
12
export interface NavigationMenuItem {
13
    name: string
14
    path: string
15
    // All privileges that can access this menu item
16
    accessibleTo: Set<string>
17
    icon: OverridableComponent<SvgIconTypeMap<{}, 'svg'>>
18 6129910f Vaclav Honzik
    position: number,
19
    isDialog?: boolean
20 456b0111 Vaclav Honzik
}
21
22
const visitorRole = 'VISITOR'
23
const visitorRoleOnly = 'VISITOR_ONLY'
24 0d90d67b Vaclav Honzik
const loggedInRole = 'LOGGED_IN'
25 456b0111 Vaclav Honzik
const visitorAccess = new Set([visitorRole])
26 0d90d67b Vaclav Honzik
const adminAccess = new Set(['ADMIN'])
27
const loggedInAccess = new Set([loggedInRole])
28 456b0111 Vaclav Honzik
29
const items: NavigationMenuItem[] = [
30
    {
31
        name: 'Home',
32
        path: '/',
33
        accessibleTo: visitorAccess,
34
        icon: HomeIcon,
35
        position: 0,
36
    },
37
    {
38
        name: 'Catalog',
39
        path: '/catalog',
40
        accessibleTo: visitorAccess,
41
        icon: MenuBook,
42
        position: 1,
43
    },
44
    {
45
        name: 'Map',
46
        path: '/map',
47
        accessibleTo: visitorAccess,
48
        icon: MapIcon,
49
        position: 2,
50
    },
51
    {
52
        name: 'Sources',
53
        path: '/sources',
54
        accessibleTo: visitorAccess,
55
        icon: LibraryBooksIcon,
56
        position: 3,
57
    },
58
    // TODO add statistics
59
    {
60
        name: 'Admin',
61
        path: '/admin',
62 0d90d67b Vaclav Honzik
        accessibleTo: adminAccess,
63 456b0111 Vaclav Honzik
        icon: PersonIcon,
64 0d90d67b Vaclav Honzik
        position: 5,
65 456b0111 Vaclav Honzik
    },
66
    // TODO move this to the top
67
    {
68
        name: 'Login',
69
        path: '/login',
70
        accessibleTo: new Set([visitorRoleOnly]),
71
        icon: LoginIcon,
72
        position: 5,
73 6129910f Vaclav Honzik
        isDialog: true
74 456b0111 Vaclav Honzik
    },
75 0d90d67b Vaclav Honzik
    {
76
        name: 'Statistics',
77
        path: '/stats',
78
        accessibleTo: adminAccess,
79
        icon: DataSaverOffIcon,
80
        position: 4,
81
    },
82
    {
83
        name: 'Logout',
84
        path: '/logout',
85
        accessibleTo: loggedInAccess,
86
        icon: LogoutIcon,
87
        position: 1337,
88
    }
89 456b0111 Vaclav Honzik
]
90
91
const getNavigationItems = (_userRoles: string[]): NavigationMenuItem[] => {
92
    const userRoles: string[] = [..._userRoles]
93
    // Add visitor role if not present and visitor role only if user has no roles
94
    if (!userRoles || userRoles.length === 0) {
95
        userRoles.push( visitorRole, visitorRoleOnly)
96
    } else {
97
        userRoles.push(visitorRole)
98 0d90d67b Vaclav Honzik
        userRoles.push(loggedInRole)
99 456b0111 Vaclav Honzik
    }
100
101
    return items // else return everything the user has privileges to
102
        .filter((item) => {
103
            // If the user has any of the roles that are required to access this item
104
            // then return it
105
            return userRoles.some((role) => item.accessibleTo.has(role))
106
        })
107
        .sort((a, b) => a.position - b.position)
108
}
109
110
export default getNavigationItems