@buerokratt-ria/menu 0.1.10 → 0.1.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/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  All changes to this project will be documented in this file.
4
4
 
5
+ ## [0.1.12] - 07-06-2024
6
+
7
+ - Add dynamic hidden menu items
8
+ - Refactor code for better readability
9
+ - Remove unnecessary component arguments
10
+
11
+ ## [0.1.11] - 30-05-2024
12
+
13
+ - Hide some Training Module menu items from production
14
+
5
15
  ## [0.1.10] - 30-05-2024
6
16
 
7
17
  - Hide some menu items from production
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@buerokratt-ria/menu",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "description": "Generic MainNavigation component that would be injected as dependency.",
5
5
  "main": "index.js",
6
6
  "scripts": {},
@@ -0,0 +1,62 @@
1
+ import React, { FC, MouseEvent } from "react";
2
+ import { NavLink } from 'react-router-dom';
3
+ import { MdKeyboardArrowDown } from 'react-icons/md';
4
+ import { MenuItem } from "../../types/menuItem";
5
+ import { useTranslation } from "react-i18next";
6
+ import { menuData } from './menuData';
7
+ import Icon from "../icons/icon/icon";
8
+ import { isSameRoot } from "./isSameRoot";
9
+ import clsx from 'clsx';
10
+ import '../../main-navigation.scss';
11
+
12
+ interface MenuTreeProps {
13
+ menuItems: MenuItem[];
14
+ serviceId: string[];
15
+ handleNavToggle: (event: MouseEvent) => void;
16
+ }
17
+
18
+ const MenuTree: FC<MenuTreeProps> = ({
19
+ menuItems,
20
+ serviceId,
21
+ handleNavToggle,
22
+ }) => {
23
+ const currentlySelectedLanguage = useTranslation().i18n.language;
24
+
25
+ return menuItems.map((menuItem) => (
26
+ <li key={menuItem.label[currentlySelectedLanguage]}>
27
+ {!!menuItem.children ? (
28
+ <>
29
+ <button
30
+ className={clsx('nav__toggle', { 'nav__toggle--icon': !!menuItem.id })}
31
+ aria-expanded={menuItem.path && (isSameRoot(menuItem, serviceId)) ? 'true' : 'false'}
32
+ onClick={handleNavToggle}
33
+ >
34
+ {menuItem.id && (
35
+ <Icon icon={menuData.find(dataItem => dataItem.id === menuItem.id)?.icon} size='large' />
36
+ )}
37
+ <span className='menu-item-title'>{menuItem.label[currentlySelectedLanguage]}</span>
38
+ <Icon icon={<MdKeyboardArrowDown />} className='menu-item-arrow' />
39
+ </button>
40
+ <ul className='nav__submenu'>
41
+ <MenuTree
42
+ menuItems={menuItem.children.map((item) => ({id: menuItem.id, ...item}))}
43
+ serviceId={serviceId}
44
+ handleNavToggle={handleNavToggle}
45
+ />
46
+ </ul>
47
+ </>
48
+ ) : (
49
+ serviceId.includes(menuItem.id!)
50
+ ? <NavLink to={menuItem.path || '#'}>
51
+ {menuItem.label[currentlySelectedLanguage]}
52
+ </NavLink>
53
+ : <a href={(menuData.find(dataItem => dataItem.id === menuItem.id)?.url ?? '') + menuItem.path}>
54
+ {menuItem.label[currentlySelectedLanguage]}
55
+ </a>
56
+ )}
57
+ </li>
58
+ ),
59
+ );
60
+ }
61
+
62
+ export default MenuTree;
@@ -0,0 +1,10 @@
1
+ import { MenuItem } from "../../types/menuItem";
2
+
3
+ export const isSameRoot = (menuItem, serviceId) => {
4
+ const base = window.location.pathname.split("/")[1];
5
+ const currentService = base === 'chat' ? serviceId : [base];
6
+ if(currentService.includes(menuItem.id)) {
7
+ return menuItem.children.some((item: MenuItem) => item.path?.includes("/" + window.location.pathname.split("/")[2]));
8
+ }
9
+ return false;
10
+ }
@@ -0,0 +1,42 @@
1
+ import React from 'react';
2
+ import {
3
+ MdOutlineForum,
4
+ MdOutlineAdb,
5
+ MdOutlineEqualizer,
6
+ MdMiscellaneousServices,
7
+ MdSettings,
8
+ MdOutlineMonitorWeight,
9
+ } from 'react-icons/md';
10
+
11
+ export const menuData = [
12
+ {
13
+ id: 'conversations',
14
+ icon: <MdOutlineForum className='menu-item-icon' />,
15
+ url: import.meta.env.REACT_APP_CONVERSATIONS_BASE_URL,
16
+ },
17
+ {
18
+ id: 'training',
19
+ icon: <MdOutlineAdb className='menu-item-icon' />,
20
+ url: import.meta.env.REACT_APP_TRAINING_BASE_URL,
21
+ },
22
+ {
23
+ id: 'analytics',
24
+ icon: <MdOutlineEqualizer className='menu-item-icon' />,
25
+ url: import.meta.env.REACT_APP_ANALYTICS_BASE_URL,
26
+ },
27
+ {
28
+ id: "services",
29
+ icon: <MdMiscellaneousServices className='menu-item-icon' />,
30
+ url: import.meta.env.REACT_APP_SERVICES_BASE_URL,
31
+ },
32
+ {
33
+ id: 'settings',
34
+ icon: <MdSettings className='menu-item-icon' />,
35
+ url: import.meta.env.REACT_APP_SETTINGS_BASE_URL,
36
+ },
37
+ {
38
+ id: 'monitoring',
39
+ icon: <MdOutlineMonitorWeight className='menu-item-icon' />,
40
+ url: import.meta.env.REACT_APP_MONITORING_BASE_URL,
41
+ },
42
+ ];
@@ -60,6 +60,7 @@
60
60
  "path": "/training/intents"
61
61
  },
62
62
  {
63
+ "hidden": true,
63
64
  "label": {
64
65
  "et": "Avalikud teemad",
65
66
  "en": "Public themes"
@@ -88,6 +89,7 @@
88
89
  "path": "/training/stories"
89
90
  },
90
91
  {
92
+ "hidden": true,
91
93
  "label": {
92
94
  "et": "Konfiguratsioon",
93
95
  "en": "Configuration"
@@ -109,6 +111,7 @@
109
111
  "path": "/training/slots"
110
112
  },
111
113
  {
114
+ "hidden": true,
112
115
  "label": {
113
116
  "et": "Automatic Teenused",
114
117
  "en": "Automatic Services"
@@ -132,6 +135,7 @@
132
135
  "path": "/history/history"
133
136
  },
134
137
  {
138
+ "hidden": true,
135
139
  "label": {
136
140
  "et": "Pöördumised",
137
141
  "en": "Appeals"
@@ -162,6 +166,7 @@
162
166
  "path": "/analytics/models"
163
167
  },
164
168
  {
169
+ "hidden": true,
165
170
  "label": {
166
171
  "et": "Testlood",
167
172
  "en": "testTracks"
@@ -226,7 +231,7 @@
226
231
  },
227
232
  {
228
233
  "id": "services",
229
- "hiddenMode": "production",
234
+ "hidden": true,
230
235
  "label": {
231
236
  "et": "Teenused",
232
237
  "en": "Services"
@@ -333,7 +338,7 @@
333
338
  },
334
339
  {
335
340
  "id": "monitoring",
336
- "hiddenMode": "production",
341
+ "hidden": true,
337
342
  "label": {
338
343
  "et": "Seire",
339
344
  "en": "Monitoring"
@@ -348,14 +353,5 @@
348
353
  "path": "/uptime"
349
354
  }
350
355
  ]
351
- },
352
- {
353
- "id":"hidden-item",
354
- "label": {
355
- "et": "Peidetud",
356
- "en": "Hidden"
357
- },
358
- "path": "/hidden",
359
- "hidden": true
360
356
  }
361
357
  ]
@@ -0,0 +1,33 @@
1
+ import { useState } from "react";
2
+ import { useQuery } from "@tanstack/react-query";
3
+ import { MenuItem } from "../types/menuItem";
4
+ import useMenuItems from "./useMenuItems";
5
+
6
+ const useFilteredMenuItems = () => {
7
+ const items = useMenuItems();
8
+ const [menuItems, setMenuItems] = useState<MenuItem[]>([]);
9
+
10
+ useQuery({
11
+ queryKey: ['accounts/user-role', 'prod'],
12
+ onSuccess: (res: any) => {
13
+ const filteredItems = items.filter((item) => {
14
+ const role = res.response;
15
+ if (role.includes('ROLE_ADMINISTRATOR'))
16
+ return item.id;
17
+ if (role.includes('ROLE_SERVICE_MANAGER'))
18
+ return item.id != "settings" && item.id != "training";
19
+ if (role.includes('ROLE_CUSTOMER_SUPPORT_AGENT'))
20
+ return item.id != "settings" && item.id != "analytics";
21
+ if (role.includes('ROLE_CHATBOT_TRAINER'))
22
+ return item.id != "settings" && item.id != "conversations";
23
+ if (role.includes('ROLE_ANALYST'))
24
+ return item.id == "analytics" || item.id == "monitoring";
25
+ });
26
+ setMenuItems(filteredItems ?? []);
27
+ },
28
+ });
29
+
30
+ return menuItems;
31
+ }
32
+
33
+ export default useFilteredMenuItems;
@@ -0,0 +1,49 @@
1
+ import { useMemo, useState } from "react";
2
+ import { useQuery } from "@tanstack/react-query";
3
+ import menuStructure from '../data/menu-structure.json';
4
+ import { MenuItem } from "../types/menuItem";
5
+
6
+ const useMenuItems = () => {
7
+ const externalMenuItems = import.meta.env.REACT_APP_MENU_JSON;
8
+
9
+ const [mainMenuItems, setMainMenuItems] = useState<MenuItem[] | null>([]);
10
+
11
+ useQuery({
12
+ enabled: !externalMenuItems,
13
+ queryKey: [import.meta.env.REACT_APP_MENU_URL + import.meta.env.REACT_APP_MENU_PATH],
14
+ onSuccess: (res: any) => {
15
+ try {
16
+ setMainMenuItems(res);
17
+ } catch (e) {
18
+ setMainMenuItems(null);
19
+ console.error(e);
20
+ }
21
+ setCache(res);
22
+ },
23
+ onError: () => {
24
+ const cached = getCache();
25
+ if(cached.length > 0)
26
+ setMainMenuItems(cached);
27
+ else
28
+ setMainMenuItems(null);
29
+ },
30
+ });
31
+
32
+ const items = useMemo(() => {
33
+ const allItems = externalMenuItems ?? mainMenuItems ?? menuStructure ?? [];
34
+ return allItems.filter(x => !x.hidden);
35
+ }, [externalMenuItems, mainMenuItems, menuStructure]);
36
+
37
+ return items;
38
+ }
39
+
40
+ function getCache(): any {
41
+ const cache = localStorage.getItem('mainmenu-cache') ?? '[]';
42
+ return JSON.parse(cache);
43
+ }
44
+
45
+ function setCache(res: any) {
46
+ localStorage.setItem('mainmenu-cache', JSON.stringify(res));
47
+ }
48
+
49
+ export default useMenuItems;
@@ -1,103 +1,19 @@
1
- import React, { FC, MouseEvent, useState } from 'react';
2
- import { NavLink } from 'react-router-dom';
1
+ import React, { FC, MouseEvent, useState, useMemo } from 'react';
3
2
  import clsx from 'clsx';
4
3
  import {
5
- MdOutlineForum,
6
- MdOutlineAdb,
7
- MdOutlineEqualizer,
8
- MdKeyboardArrowDown,
9
- MdMiscellaneousServices,
10
- MdSettings,
11
- MdOutlineMonitorWeight,
12
4
  MdKeyboardArrowRight,
13
5
  MdKeyboardArrowLeft,
14
6
  } from 'react-icons/md';
15
7
  import Icon from './components/icons/icon/icon';
16
- import './main-navigation.scss';
17
- import { useQuery } from '@tanstack/react-query';
18
8
  import { useTranslation } from "react-i18next";
19
- import menuStructure from './data/menu-structure.json';
20
- import { isHiddenFeaturesEnabled } from './constants';
21
-
22
- interface MenuItem {
23
- id?: string;
24
- label: TranslatedLabel;
25
- path?: string;
26
- target?: '_blank' | '_self';
27
- children?: MenuItem[];
28
- hidden?: boolean;
29
- hiddenMode?: 'production' | 'development' | 'staging' | 'testing';
30
- }
31
-
32
- interface TranslatedLabel {
33
- [lang: string] : string;
34
- }
35
-
36
- const MainNavigation: FC<{items: MenuItem[], serviceId: string[]}> = ( {items, serviceId}) => {
37
- if(!items.isArray || items.length === 0) {
38
- items = menuStructure;
39
- }
40
-
41
- const { t, i18n } = useTranslation();
42
- const currentlySelectedLanguage = i18n.language;
43
- const [menuItems, setMenuItems] = useState<MenuItem[]>([]);
44
-
45
- const menuData = [
46
- {
47
- id: 'conversations',
48
- icon: <MdOutlineForum className='menu-item-icon' />,
49
- url: import.meta.env.REACT_APP_CONVERSATIONS_BASE_URL,
50
- },
51
- {
52
- id: 'training',
53
- icon: <MdOutlineAdb className='menu-item-icon' />,
54
- url: import.meta.env.REACT_APP_TRAINING_BASE_URL,
55
- },
56
- {
57
- id: 'analytics',
58
- icon: <MdOutlineEqualizer className='menu-item-icon' />,
59
- url: import.meta.env.REACT_APP_ANALYTICS_BASE_URL,
60
- },
61
- {
62
- id: "services",
63
- icon: <MdMiscellaneousServices className='menu-item-icon' />,
64
- url: import.meta.env.REACT_APP_SERVICES_BASE_URL,
65
- },
66
- {
67
- id: 'settings',
68
- icon: <MdSettings className='menu-item-icon' />,
69
- url: import.meta.env.REACT_APP_SETTINGS_BASE_URL,
70
- },
71
- {
72
- id: 'monitoring',
73
- icon: <MdOutlineMonitorWeight className='menu-item-icon' />,
74
- url: import.meta.env.REACT_APP_MONITORING_BASE_URL,
75
- },
76
- ];
9
+ import MenuTree from './components/menuTree';
10
+ import useFilteredMenuItems from './hooks/useFilteredMenuItems';
11
+ import './main-navigation.scss';
77
12
 
78
- useQuery({
79
- queryKey: ['accounts/user-role', 'prod'],
80
- onSuccess: (res: any) => {
81
- const filteredItems =
82
- items.filter((item) => {
83
- const role = res.response;
84
- if (role.includes('ROLE_ADMINISTRATOR')) {
85
- return item.id;
86
- } else if (role.includes('ROLE_SERVICE_MANAGER')) {
87
- return item.id != "settings" && item.id != "training";
88
- } else if (role.includes('ROLE_CUSTOMER_SUPPORT_AGENT')) {
89
- return item.id != "settings" && item.id != "analytics";
90
- } else if (role.includes('ROLE_CHATBOT_TRAINER')) {
91
- return item.id != "settings" && item.id != "conversations";
92
- } else if (role.includes('ROLE_ANALYST')) {
93
- return item.id == "analytics" || item.id == "monitoring";
94
- } else {
95
- return;
96
- }
97
- }) ?? [];
98
- setMenuItems(filteredItems);
99
- },
100
- });
13
+ const MainNavigation: FC = () => {
14
+ const { t } = useTranslation();
15
+ const menuItems = useFilteredMenuItems();
16
+ const serviceId = useMemo(() => import.meta.env.REACT_APP_SERVICE_ID.split(','), []);
101
17
 
102
18
  const [navCollapsed, setNavCollapsed] = useState(false);
103
19
 
@@ -107,52 +23,6 @@ const MainNavigation: FC<{items: MenuItem[], serviceId: string[]}> = ( {items, s
107
23
  event.currentTarget.setAttribute('aria-expanded', isExpanded ? 'false' : 'true');
108
24
  };
109
25
 
110
- const renderMenuTree = (menuItems: MenuItem[]) => {
111
- return menuItems
112
- .filter(x => !x.hidden)
113
- .filter(x => isHiddenFeaturesEnabled || x.hiddenMode !== "production")
114
- .map((menuItem) => (
115
- <li key={menuItem.label[currentlySelectedLanguage]}>
116
- {!!menuItem.children ? (
117
- <>
118
- <button
119
- className={clsx('nav__toggle', { 'nav__toggle--icon': !!menuItem.id })}
120
- aria-expanded={menuItem.path && (isSameRoot(menuItem)) ? 'true' : 'false'}
121
- onClick={handleNavToggle}
122
- >
123
- {menuItem.id && (
124
- <Icon icon={menuData.find(dataItem => dataItem.id === menuItem.id)?.icon} size='large' />
125
- )}
126
- <span className='menu-item-title'>{menuItem.label[currentlySelectedLanguage]}</span>
127
- <Icon icon={<MdKeyboardArrowDown />} className='menu-item-arrow' />
128
- </button>
129
- <ul className='nav__submenu'>
130
- {renderMenuTree(menuItem.children.map((item) => ({id: menuItem.id, ...item})))}
131
- </ul>
132
- </>
133
- ) : (
134
- serviceId.includes(menuItem.id)
135
- ? <NavLink to={menuItem.path || '#'}>
136
- {menuItem.label[currentlySelectedLanguage]}
137
- </NavLink>
138
- : <a href={menuData.find(dataItem => dataItem.id === menuItem.id)?.url + menuItem.path}>
139
- {menuItem.label[currentlySelectedLanguage]}
140
- </a>
141
- )}
142
- </li>
143
- ),
144
- );
145
- };
146
-
147
- const base = window.location.pathname.split("/")[1];
148
- const currentService = base === 'chat' ? serviceId : [base];
149
- const isSameRoot = (menuItem) => {
150
- if(currentService.includes(menuItem.id)) {
151
- return menuItem.children.some((item: MenuItem) => item.path?.includes("/" + window.location.pathname.split("/")[2]));
152
- }
153
- return false;
154
- }
155
-
156
26
  if (!menuItems) return null;
157
27
 
158
28
  return (
@@ -166,7 +36,11 @@ const MainNavigation: FC<{items: MenuItem[], serviceId: string[]}> = ( {items, s
166
36
  <span className='menu-item-title'>{t('mainMenu.closeMenu')}</span>
167
37
  </button>
168
38
  <ul className='nav__menu'>
169
- {renderMenuTree(menuItems)}
39
+ <MenuTree
40
+ menuItems={menuItems}
41
+ serviceId={serviceId}
42
+ handleNavToggle={handleNavToggle}
43
+ />
170
44
  </ul>
171
45
  </nav>
172
46
  );
@@ -0,0 +1,13 @@
1
+
2
+ export interface TranslatedLabel {
3
+ [lang: string] : string;
4
+ }
5
+
6
+ export interface MenuItem {
7
+ id?: string;
8
+ label: TranslatedLabel;
9
+ path?: string;
10
+ target?: '_blank' | '_self';
11
+ children?: MenuItem[];
12
+ hidden?: boolean;
13
+ }
Binary file
@@ -1,3 +0,0 @@
1
- export const isHiddenFeaturesEnabled =
2
- import.meta.env.REACT_APP_ENABLE_HIDDEN_FEATURES?.toLowerCase().trim() == 'true' ||
3
- import.meta.env.REACT_APP_ENABLE_HIDDEN_FEATURES?.toLowerCase().trim() == '1';