@campxdev/shared 0.5.11 → 0.5.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campxdev/shared",
3
- "version": "0.5.11",
3
+ "version": "0.5.12",
4
4
  "main": "./exports.ts",
5
5
  "scripts": {
6
6
  "start": "react-scripts start",
@@ -7,8 +7,8 @@ import {
7
7
  ListItemText,
8
8
  styled,
9
9
  } from '@mui/material'
10
- import {ReactNode, useState} from 'react'
11
- import {Link, useMatch, useResolvedPath} from 'react-router-dom'
10
+ import {ReactNode, useEffect, useState} from 'react'
11
+ import {Link, useLocation, useMatch, useResolvedPath} from 'react-router-dom'
12
12
  import {PermissionsStore} from '../permissions'
13
13
  import {
14
14
  ListItemButton,
@@ -52,7 +52,6 @@ const RenderMenuItem = ({menuItem}) => {
52
52
  const permissions = PermissionsStore.useState((s) => s).permissions
53
53
  const hasAccess = permissionKey ? permissions[permissionKey] : accessIfNoKey
54
54
 
55
- if (!hasAccess && !children?.length) return null
56
55
  if (children?.length)
57
56
  return (
58
57
  <Box
@@ -71,36 +70,40 @@ const RenderMenuItem = ({menuItem}) => {
71
70
  )
72
71
 
73
72
  return (
74
- <StyledLink to={path}>
75
- <ListItemButton
76
- hasChildren={false}
77
- label={title}
78
- isActive={!!match}
79
- icon={icon}
80
- />
81
- </StyledLink>
73
+ <>
74
+ {hasAccess ? (
75
+ <StyledLink to={path}>
76
+ <ListItemButton
77
+ hasChildren={false}
78
+ label={title}
79
+ isActive={!!match}
80
+ icon={icon}
81
+ />
82
+ </StyledLink>
83
+ ) : null}
84
+ </>
82
85
  )
83
86
  }
84
87
 
85
88
  const DropDownMenu = ({path, title, icon, menuItems, permissionKey}) => {
89
+ let resolved = useResolvedPath(path)
90
+ let match = useMatch({path: resolved.pathname, end: false})
91
+
86
92
  const [open, setOpen] = useState(false)
87
93
  const permissions = PermissionsStore.useState((s) => s).permissions
88
94
 
89
95
  const validateDropdownAccess = () => {
90
- if (accessIfNoKey && !permissions) return true
91
- let arr = []
92
-
93
- for (let i = 0; i < menuItems.length; i++) {
94
- if (permissions[menuItems[i].permissionKey]) {
95
- arr.push(true)
96
- } else {
97
- arr.push(accessIfNoKey)
98
- }
99
- }
96
+ if (process.env.NODE_ENV === 'development' && !permissions) return true
100
97
 
101
- return arr.includes(true)
98
+ return menuItems?.every((item) =>
99
+ item?.permissionKey ? permissions[item.permissionKey] : accessIfNoKey
100
+ )
102
101
  }
103
102
 
103
+ useEffect(() => {
104
+ setOpen(!!match)
105
+ }, [resolved])
106
+
104
107
  if (!validateDropdownAccess()) return null
105
108
  return (
106
109
  <>