1
1
import type { NavbarProps as MantineNavbarProps } from '@mantine/core' ;
2
- import {
3
- Navbar as MantineNavbar ,
4
- NavLink ,
5
- Select ,
6
- Box ,
7
- Modal ,
8
- Text ,
9
- Button ,
10
- TextInput ,
11
- } from '@mantine/core' ;
2
+ import { Navbar as MantineNavbar , NavLink , Select , Box , Modal , Text , Button , TextInput } from '@mantine/core' ;
12
3
import {
13
4
IconZoomCode ,
14
5
IconReportAnalytics ,
@@ -21,6 +12,7 @@ import {
21
12
IconSettings ,
22
13
IconTrash ,
23
14
IconInfoCircle ,
15
+ IconUserCog ,
24
16
} from '@tabler/icons-react' ;
25
17
import { FC , useEffect } from 'react' ;
26
18
import { useNavbarStyles } from './styles' ;
@@ -32,9 +24,10 @@ import { DEFAULT_FIXED_DURATIONS, useHeaderContext } from '@/layouts/MainLayout/
32
24
import useMountedState from '@/hooks/useMountedState' ;
33
25
import dayjs from 'dayjs' ;
34
26
import { useDisclosure , useLocalStorage } from '@mantine/hooks' ;
35
- import { LOGIN_ROUTE } from '@/constants/routes' ;
27
+ import { LOGIN_ROUTE , USERS_MANAGEMENT_ROUTE } from '@/constants/routes' ;
36
28
import { useDeleteLogStream } from '@/hooks/useDeleteLogStream' ;
37
29
import InfoModal from './infoModal' ;
30
+ import { useGetUserRole } from '@/hooks/useGetUserRoles' ;
38
31
39
32
const links = [
40
33
{ icon : IconZoomCode , label : 'Query' , pathname : '/query' } ,
@@ -49,12 +42,11 @@ const Navbar: FC<NavbarProps> = (props) => {
49
42
const navigate = useNavigate ( ) ;
50
43
const { streamName } = useParams ( ) ;
51
44
const location = useLocation ( ) ;
52
-
45
+
53
46
const [ username ] = useLocalStorage ( { key : 'username' , getInitialValueInEffect : false } ) ;
54
47
const [ , , removeCredentials ] = useLocalStorage ( { key : 'credentials' } ) ;
55
48
const [ , , removeUsername ] = useLocalStorage ( { key : 'username' } ) ;
56
-
57
-
49
+
58
50
const {
59
51
state : { subNavbarTogle } ,
60
52
} = useHeaderContext ( ) ;
@@ -63,6 +55,7 @@ const Navbar: FC<NavbarProps> = (props) => {
63
55
const [ searchValue , setSearchValue ] = useMountedState ( '' ) ;
64
56
const [ currentPage , setCurrentPage ] = useMountedState ( '/query' ) ;
65
57
const [ deleteStream , setDeleteStream ] = useMountedState ( '' ) ;
58
+ const [ isAdmin , setIsAdmin ] = useMountedState ( false ) ;
66
59
67
60
const [ disableLink , setDisableLink ] = useMountedState ( false ) ;
68
61
const [ isSubNavbarOpen , setIsSubNavbarOpen ] = useMountedState ( false ) ;
@@ -103,8 +96,7 @@ const Navbar: FC<NavbarProps> = (props) => {
103
96
setSearchValue ( '' ) ;
104
97
setDisableLink ( true ) ;
105
98
navigate ( `/` ) ;
106
- }
107
- else if ( streamName ) {
99
+ } else if ( streamName ) {
108
100
if ( streamName === deleteStream && streams ) {
109
101
setDeleteStream ( '' ) ;
110
102
handleChange ( streams [ 0 ] . name ) ;
@@ -122,14 +114,24 @@ const Navbar: FC<NavbarProps> = (props) => {
122
114
handleChange ( streamName ) ;
123
115
}
124
116
} else if ( streams && Boolean ( streams . length ) ) {
125
- handleChange ( streams [ 0 ] . name ) ;
117
+ if ( location . pathname === USERS_MANAGEMENT_ROUTE ) {
118
+ handleChangeWithoutRiderection ( streams [ 0 ] . name , location . pathname ) ;
119
+ } else {
120
+ handleChange ( streams [ 0 ] . name ) ;
121
+ }
126
122
}
127
123
} , [ streams ] ) ;
128
124
129
125
const handleChange = ( value : string ) => {
126
+ handleChangeWithoutRiderection ( value ) ;
127
+ navigate ( `/${ value } ${ currentPage } ` ) ;
128
+ } ;
129
+ const handleChangeWithoutRiderection = ( value : string , page : string = currentPage ) => {
130
130
setActiveStream ( value ) ;
131
131
setSearchValue ( value ) ;
132
+ setCurrentPage ( page ) ;
132
133
const now = dayjs ( ) ;
134
+
133
135
subLogQuery . set ( ( state ) => {
134
136
state . streamName = value || '' ;
135
137
state . startTime = now . subtract ( DEFAULT_FIXED_DURATIONS . milliseconds , 'milliseconds' ) . toDate ( ) ;
@@ -138,16 +140,14 @@ const Navbar: FC<NavbarProps> = (props) => {
138
140
subLogSelectedTimeRange . set ( ( state ) => {
139
141
state . state = 'fixed' ;
140
142
state . value = DEFAULT_FIXED_DURATIONS . name ;
141
- } ) ;
143
+ } ) ;
142
144
subLogSearch . set ( ( state ) => {
143
145
state . search = '' ;
144
146
state . filters = { } ;
145
147
} ) ;
146
148
subRefreshInterval . set ( null ) ;
147
149
setDisableLink ( false ) ;
148
- navigate ( `/${ value } ${ currentPage } ` ) ;
149
150
} ;
150
-
151
151
const handleCloseDelete = ( ) => {
152
152
closeDelete ( ) ;
153
153
setDeleteStream ( '' ) ;
@@ -166,6 +166,22 @@ const Navbar: FC<NavbarProps> = (props) => {
166
166
}
167
167
} , [ deleteData ] ) ;
168
168
169
+ //isAdmin
170
+ const { data : adminData , getRoles, resetData } = useGetUserRole ( ) ;
171
+ useEffect ( ( ) => {
172
+ if ( username ) {
173
+ getRoles ( username ) ;
174
+ }
175
+ return ( ) => {
176
+ resetData ( ) ;
177
+ } ;
178
+ } , [ username ] ) ;
179
+
180
+ useEffect ( ( ) => {
181
+ if ( adminData ) {
182
+ setIsAdmin ( true ) ;
183
+ }
184
+ } , [ adminData ] ) ;
169
185
170
186
const { classes } = useNavbarStyles ( ) ;
171
187
const {
@@ -177,10 +193,13 @@ const Navbar: FC<NavbarProps> = (props) => {
177
193
lowerContainer,
178
194
actionBtn,
179
195
userBtn,
196
+ userManagementBtn,
197
+ userManagementBtnActive,
180
198
} = classes ;
181
199
return (
182
200
< MantineNavbar { ...props } withBorder zIndex = { 1 } hiddenBreakpoint = { window . outerWidth + 20 } hidden = { isSubNavbarOpen } >
183
201
< MantineNavbar . Section grow className = { container } >
202
+
184
203
< NavLink label = "Log Streams" icon = { < IconBinaryTree2 size = "1.5rem" stroke = { 1.3 } /> } className = { streamsBtn } />
185
204
< Select
186
205
placeholder = "Pick one"
@@ -208,8 +227,7 @@ const Navbar: FC<NavbarProps> = (props) => {
208
227
setCurrentPage ( link . pathname ) ;
209
228
} }
210
229
key = { link . label }
211
-
212
- className = { ( currentPage === link . pathname && linkBtnActive ) || linkBtn }
230
+ className = { ( currentPage === link . pathname && linkBtnActive ) || linkBtn }
213
231
/>
214
232
) ;
215
233
} ) }
@@ -231,6 +249,18 @@ const Navbar: FC<NavbarProps> = (props) => {
231
249
sx = { { paddingLeft : 0 } }
232
250
/>
233
251
) }
252
+ { isAdmin && (
253
+ < NavLink
254
+ pt = { 24 }
255
+ className = { ( currentPage === USERS_MANAGEMENT_ROUTE && userManagementBtnActive ) || userManagementBtn }
256
+ label = "Users"
257
+ icon = { < IconUserCog size = "1.5rem" stroke = { 1.3 } /> }
258
+ onClick = { ( ) => {
259
+ navigate ( `/users` ) ;
260
+ setCurrentPage ( USERS_MANAGEMENT_ROUTE ) ;
261
+ } }
262
+ />
263
+ ) }
234
264
</ MantineNavbar . Section >
235
265
< MantineNavbar . Section className = { lowerContainer } >
236
266
< NavLink label = { username } icon = { < IconUser size = "1.3rem" stroke = { 1.3 } /> } className = { userBtn } component = "a" />
@@ -256,15 +286,15 @@ const Navbar: FC<NavbarProps> = (props) => {
256
286
onChange = { ( e ) => {
257
287
setDeleteStream ( e . target . value ) ;
258
288
} }
259
- placeholder = { `Type the name of the stream to confirm. i.e: ${ streamName } ` }
289
+ placeholder = { `Type the name of the stream to confirm. i.e: ${ activeStream } ` }
260
290
/>
261
291
262
292
< Box mt = { 10 } display = "flex" sx = { { justifyContent : 'end' } } >
263
293
< Button
264
294
variant = "filled"
265
295
color = "red"
266
296
sx = { { margin : '12px' } }
267
- disabled = { deleteStream === streamName ? false : true }
297
+ disabled = { deleteStream === activeStream ? false : true }
268
298
onClick = { handleDelete } >
269
299
Delete
270
300
</ Button >
@@ -273,11 +303,9 @@ const Navbar: FC<NavbarProps> = (props) => {
273
303
</ Button >
274
304
</ Box >
275
305
</ Modal >
276
- < InfoModal opened = { opened } close = { close } />
306
+ < InfoModal opened = { opened } close = { close } />
277
307
</ MantineNavbar >
278
308
) ;
279
309
} ;
280
310
281
-
282
-
283
311
export default Navbar ;
0 commit comments