11import React from "react" ;
22import { useTranslation } from "react-i18next" ;
3- import { Link , useNavigate } from "react-router" ;
3+ import { NavLink , useLocation , useNavigate } from "react-router" ;
44import {
55 getOrgProperties ,
66 getUserInformation ,
@@ -52,6 +52,127 @@ const MainNav = ({
5252 [ toggleMenu ] ,
5353 ) ;
5454
55+ // Find current view based on pathname of router
56+ const location = useLocation ( ) ;
57+ let pathname = "" ;
58+ let firstPathFragment = "" ;
59+ if ( location ?. pathname . length > 0 ) {
60+ pathname = location . pathname ;
61+ if ( pathname . startsWith ( "/" ) ) {
62+ firstPathFragment = pathname . substring ( 1 , pathname . indexOf ( "/" , 1 ) ) ;
63+ }
64+ }
65+
66+ interface linkMapItem {
67+ links : ( React . ComponentProps < typeof MainNavLink > & { accessRole : string } ) [ ]
68+ }
69+
70+ const linkMap : { [ key : string ] : linkMapItem } = {
71+ "events" : {
72+ links : [
73+ {
74+ path : "/events/events" ,
75+ accessRole : "ROLE_UI_EVENTS_VIEW" ,
76+ tooltipTitle : "NAV.EVENTS.TITLE" ,
77+ className : "events" ,
78+ } ,
79+ {
80+ path : "/events/series" ,
81+ accessRole : "ROLE_UI_SERIES_VIEW" ,
82+ tooltipTitle : "NAV.EVENTS.TITLE" ,
83+ className : "events" ,
84+ } ,
85+ ] ,
86+ } ,
87+ "recordings" : {
88+ links : [
89+ {
90+ path : "/recordings/recordings" ,
91+ accessRole : "ROLE_UI_LOCATIONS_VIEW" ,
92+ tooltipTitle : "NAV.CAPTUREAGENTS.TITLE" ,
93+ className : "recordings" ,
94+ } ,
95+ ] ,
96+ } ,
97+ "systems" : {
98+ links : [
99+ {
100+ path : "/systems/jobs" ,
101+ accessRole : "ROLE_UI_JOBS_VIEW" ,
102+ tooltipTitle : "NAV.SYSTEMS.TITLE" ,
103+ className : "systems" ,
104+ } ,
105+ {
106+ path : "/systems/servers" ,
107+ accessRole : "ROLE_UI_SERVERS_VIEW" ,
108+ tooltipTitle : "NAV.SYSTEMS.TITLE" ,
109+ className : "systems" ,
110+ } ,
111+ {
112+ path : "/systems/services" ,
113+ accessRole : "ROLE_UI_SERVICES_VIEW" ,
114+ tooltipTitle : "NAV.SYSTEMS.TITLE" ,
115+ className : "systems" ,
116+ } ,
117+ ] ,
118+ } ,
119+ "users" : {
120+ links : [
121+ {
122+ path : "/users/users" ,
123+ accessRole : "ROLE_UI_USERS_VIEW" ,
124+ tooltipTitle : "NAV.USERS.TITLE" ,
125+ className : "users" ,
126+ } ,
127+ {
128+ path : "/users/groups" ,
129+ accessRole : "ROLE_UI_GROUPS_VIEW" ,
130+ tooltipTitle : "NAV.USERS.TITLE" ,
131+ className : "users" ,
132+ } ,
133+ {
134+ path : "/users/acls" ,
135+ accessRole : "ROLE_UI_ACLS_VIEW" ,
136+ tooltipTitle : "NAV.USERS.TITLE" ,
137+ className : "users" ,
138+ } ,
139+ ] ,
140+ } ,
141+ "configuration" : {
142+ links : [
143+ {
144+ path : "/configuration/themes" ,
145+ accessRole : "ROLE_UI_THEMES_VIEW" ,
146+ tooltipTitle : "NAV.CONFIGURATION.TITLE" ,
147+ className : "configuration" ,
148+ } ,
149+ ] ,
150+ } ,
151+ "statistics" : {
152+ links : [
153+ {
154+ path : "/statistics/organization" ,
155+ accessRole : "ROLE_UI_STATISTICS_ORGANIZATION_VIEW" ,
156+ tooltipTitle : "NAV.STATISTICS.TITLE" ,
157+ className : "statistics" ,
158+ } ,
159+ ] ,
160+ } ,
161+ } ;
162+
163+ // Link arrays containing more than one link must be sorted so that the
164+ // current view is always the first element. Otherwise, NavLink will not
165+ // recognize the current view as active.
166+ if ( firstPathFragment . length > 0 ) {
167+ const arrToSort = linkMap [ firstPathFragment as keyof typeof linkMap ] . links ;
168+ if ( arrToSort != undefined && arrToSort . length > 1 ) {
169+ arrToSort . forEach ( ( item : any ) => {
170+ if ( item . path === pathname ) { item . tmpIndex = 0 } else { item . tmpIndex = 1 }
171+ } ) ;
172+ arrToSort . sort ( ( a : any , b : any ) => a . tmpIndex - b . tmpIndex ) ;
173+ }
174+ }
175+
55176 return (
56177 < div className = "menu-top" >
57178 < ButtonLikeAnchor onClick = { ( ) => toggleMenu ( ) } >
@@ -65,102 +186,30 @@ const MainNav = ({
65186 { /* todo: more than one href? how? roles? (see MainNav admin-ui-frontend)*/ }
66187 < MainNavButton
67188 accessRole = "ROLE_UI_NAV_RECORDINGS_VIEW"
68- links = { [
69- {
70- path : "/events/events" ,
71- accessRole : "ROLE_UI_EVENTS_VIEW" ,
72- tooltipTitle : "NAV.EVENTS.TITLE" ,
73- className : "events" ,
74- } ,
75- {
76- path : "/events/series" ,
77- accessRole : "ROLE_UI_SERIES_VIEW" ,
78- tooltipTitle : "NAV.EVENTS.TITLE" ,
79- className : "events" ,
80- } ,
81- ] }
189+ links = { linkMap [ "events" ] . links }
82190 />
83191 < MainNavButton
84192 accessRole = "ROLE_UI_NAV_CAPTURE_VIEW"
85- links = { [
86- {
87- path : "/recordings/recordings" ,
88- accessRole : "ROLE_UI_LOCATIONS_VIEW" ,
89- tooltipTitle : "NAV.CAPTUREAGENTS.TITLE" ,
90- className : "recordings" ,
91- } ,
92- ] }
193+ links = { linkMap [ "recordings" ] . links }
93194 />
94195 < MainNavButton
95196 accessRole = "ROLE_UI_NAV_SYSTEMS_VIEW"
96- links = { [
97- {
98- path : "/systems/jobs" ,
99- accessRole : "ROLE_UI_JOBS_VIEW" ,
100- tooltipTitle : "NAV.SYSTEMS.TITLE" ,
101- className : "systems" ,
102- } ,
103- {
104- path : "/systems/servers" ,
105- accessRole : "ROLE_UI_SERVERS_VIEW" ,
106- tooltipTitle : "NAV.SYSTEMS.TITLE" ,
107- className : "systems" ,
108- } ,
109- {
110- path : "/systems/services" ,
111- accessRole : "ROLE_UI_SERVICES_VIEW" ,
112- tooltipTitle : "NAV.SYSTEMS.TITLE" ,
113- className : "systems" ,
114- } ,
115- ] }
197+ links = { linkMap [ "systems" ] . links }
116198 />
117199 < MainNavButton
118200 accessRole = "ROLE_UI_NAV_ORGANIZATION_VIEW"
119- links = { [
120- {
121- path : "/users/users" ,
122- accessRole : "ROLE_UI_USERS_VIEW" ,
123- tooltipTitle : "NAV.USERS.TITLE" ,
124- className : "users" ,
125- } ,
126- {
127- path : "/users/groups" ,
128- accessRole : "ROLE_UI_GROUPS_VIEW" ,
129- tooltipTitle : "NAV.USERS.TITLE" ,
130- className : "users" ,
131- } ,
132- {
133- path : "/users/acls" ,
134- accessRole : "ROLE_UI_ACLS_VIEW" ,
135- tooltipTitle : "NAV.USERS.TITLE" ,
136- className : "users" ,
137- } ,
138- ] }
201+ links = { linkMap [ "users" ] . links }
139202 />
140203 { themesEnabled &&
141204 < MainNavButton
142205 accessRole = "ROLE_UI_NAV_CONFIGURATION_VIEW"
143- links = { [
144- {
145- path : "/configuration/themes" ,
146- accessRole : "ROLE_UI_THEMES_VIEW" ,
147- tooltipTitle : "NAV.CONFIGURATION.TITLE" ,
148- className : "configuration" ,
149- } ,
150- ] }
206+ links = { linkMap [ "configuration" ] . links }
151207 />
152208 }
153209 { statisticsEnabled &&
154210 < MainNavButton
155211 accessRole = "ROLE_UI_NAV_STATISTICS_VIEW"
156- links = { [
157- {
158- path : "/statistics/organization" ,
159- accessRole : "ROLE_UI_STATISTICS_ORGANIZATION_VIEW" ,
160- tooltipTitle : "NAV.STATISTICS.TITLE" ,
161- className : "statistics" ,
162- } ,
163- ] }
212+ links = { linkMap [ "statistics" ] . links }
164213 />
165214 }
166215 </ div >
@@ -203,11 +252,12 @@ const MainNavLink = ({
203252 const { t } = useTranslation ( ) ;
204253
205254 return (
206- < Link to = { path } >
255+ < NavLink to = { path }
256+ className = { ( { isActive } ) => isActive ? "roll-up-menu-active" : "" } >
207257 < Tooltip title = { t ( tooltipTitle ) } placement = { "right" } >
208258 < i className = { className } />
209259 </ Tooltip >
210- </ Link >
260+ </ NavLink >
211261 ) ;
212262} ;
213263
0 commit comments