@@ -19,9 +19,8 @@ import { useNavbarStyles } from './styles';
19
19
import { useLocation , useParams } from 'react-router-dom' ;
20
20
import { notifications } from '@mantine/notifications' ;
21
21
import { useNavigate } from 'react-router-dom' ;
22
- import { DEFAULT_FIXED_DURATIONS , useHeaderContext } from '@/layouts/MainLayout/Context' ;
22
+ import { useHeaderContext } from '@/layouts/MainLayout/Context' ;
23
23
import useMountedState from '@/hooks/useMountedState' ;
24
- import dayjs from 'dayjs' ;
25
24
import { useDisclosure } from '@mantine/hooks' ;
26
25
import { USERS_MANAGEMENT_ROUTE } from '@/constants/routes' ;
27
26
import InfoModal from './infoModal' ;
@@ -36,7 +35,9 @@ const baseURL = import.meta.env.VITE_PARSEABLE_URL ?? '/';
36
35
const isSecureConnection = window . location . protocol === 'https:' ;
37
36
const links = [
38
37
{ icon : IconTableShortcut , label : 'Explore' , pathname : '/logs' , requiredAccess : [ 'Query' , 'GetSchema' ] } ,
39
- ...( ! isSecureConnection ? [ { icon : IconTimelineEvent , label : 'Live tail' , pathname : '/live-tail' , requiredAccess : [ 'GetLiveTail' ] } ] : [ ] ) ,
38
+ ...( ! isSecureConnection
39
+ ? [ { icon : IconTimelineEvent , label : 'Live tail' , pathname : '/live-tail' , requiredAccess : [ 'GetLiveTail' ] } ]
40
+ : [ ] ) ,
40
41
{ icon : IconReportAnalytics , label : 'Stats' , pathname : '/stats' , requiredAccess : [ 'GetStats' ] } ,
41
42
{ icon : IconSettings , label : 'Config' , pathname : '/config' , requiredAccess : [ 'PutAlert' ] } ,
42
43
] ;
@@ -51,12 +52,14 @@ const Navbar: FC<NavbarProps> = (props) => {
51
52
const username = Cookies . get ( 'username' ) ;
52
53
53
54
const {
54
- state : { subNavbarTogle } ,
55
+ state : { subNavbarTogle, subAppContext } ,
56
+ methods : { streamChangeCleanup, setUserRoles, setSelectedStream } ,
55
57
} = useHeaderContext ( ) ;
56
58
57
- const [ activeStream , setActiveStream ] = useMountedState ( '' ) ;
59
+ const selectedStream = subAppContext . get ( ) . selectedStream ;
60
+ // const [selectedStream, setSelectedStream] = useMountedState('');
58
61
const [ searchValue , setSearchValue ] = useMountedState ( '' ) ;
59
- const [ currentPage , setCurrentPage ] = useMountedState ( '/logs ' ) ;
62
+ const [ currentPage , setCurrentPage ] = useMountedState ( '/' ) ;
60
63
const [ deleteStream , setDeleteStream ] = useMountedState ( '' ) ;
61
64
const [ userSepecficStreams , setUserSepecficStreams ] = useMountedState < LogStreamData | null > ( null ) ;
62
65
const [ userSepecficAccess , setUserSepecficAccess ] = useMountedState < string [ ] | null > ( null ) ;
@@ -85,16 +88,16 @@ const Navbar: FC<NavbarProps> = (props) => {
85
88
window . location . href = `${ baseURL } api/v1/o/logout?redirect=${ window . location . origin } /login` ;
86
89
} ;
87
90
88
- const {
89
- state : { subLogQuery, subLogSelectedTimeRange, subLogSearch, subRefreshInterval } ,
90
- } = useHeaderContext ( ) ;
91
-
92
91
useEffect ( ( ) => {
93
92
if ( location . pathname . split ( '/' ) [ 2 ] ) {
94
93
setCurrentPage ( `/${ location . pathname . split ( '/' ) [ 2 ] } ` ) ;
95
94
}
96
- if ( userSepecficStreams && userSepecficStreams . length === 0 ) {
97
- setActiveStream ( '' ) ;
95
+ if ( location . pathname === '/' ) {
96
+ setSelectedStream ( '' ) ;
97
+ setCurrentPage ( '/' ) ;
98
+ setUserSepecficAccess ( getStreamsSepcificAccess ( getUserRolesData ?. data ) ) ;
99
+ } else if ( userSepecficStreams && userSepecficStreams . length === 0 ) {
100
+ setSelectedStream ( '' ) ;
98
101
setSearchValue ( '' ) ;
99
102
setDisableLink ( true ) ;
100
103
navigate ( '/' ) ;
@@ -126,33 +129,19 @@ const Navbar: FC<NavbarProps> = (props) => {
126
129
} , [ userSepecficStreams ] ) ;
127
130
128
131
const handleChange = ( value : string , page : string = currentPage ) => {
129
- handleChangeWithoutRiderection ( value , page ) ;
132
+ const targetPage = page === '/' ? '/logs' : page ;
133
+ handleChangeWithoutRiderection ( value , targetPage ) ;
134
+ setUserSepecficAccess ( getStreamsSepcificAccess ( getUserRolesData ?. data , value ) ) ;
130
135
if ( page !== '/users' ) {
131
- navigate ( `/${ value } ${ page } ` ) ;
136
+ navigate ( `/${ value } ${ targetPage } ` ) ;
132
137
}
133
138
} ;
134
139
135
140
const handleChangeWithoutRiderection = ( value : string , page : string = currentPage ) => {
136
- setActiveStream ( value ) ;
141
+ setSelectedStream ( value ) ;
137
142
setSearchValue ( value ) ;
138
143
setCurrentPage ( page ) ;
139
- const now = dayjs ( ) ;
140
- setUserSepecficAccess ( getStreamsSepcificAccess ( getUserRolesData ?. data , value ) ) ;
141
- subLogQuery . set ( ( state ) => {
142
- state . streamName = value || '' ;
143
- state . startTime = now . subtract ( DEFAULT_FIXED_DURATIONS . milliseconds , 'milliseconds' ) . toDate ( ) ;
144
- state . endTime = now . toDate ( ) ;
145
- state . access = getStreamsSepcificAccess ( getUserRolesData ?. data , value ) ;
146
- } ) ;
147
- subLogSelectedTimeRange . set ( ( state ) => {
148
- state . state = 'fixed' ;
149
- state . value = DEFAULT_FIXED_DURATIONS . name ;
150
- } ) ;
151
- subLogSearch . set ( ( state ) => {
152
- state . search = '' ;
153
- state . filters = { } ;
154
- } ) ;
155
- subRefreshInterval . set ( null ) ;
144
+ streamChangeCleanup ( value ) ;
156
145
setDisableLink ( false ) ;
157
146
} ;
158
147
const handleCloseDelete = ( ) => {
@@ -167,6 +156,7 @@ const Navbar: FC<NavbarProps> = (props) => {
167
156
168
157
useEffect ( ( ) => {
169
158
if ( getLogStreamListData ?. data && getLogStreamListData ?. data . length > 0 && getUserRolesData ?. data ) {
159
+ getUserRolesData ?. data && setUserRoles ( getUserRolesData ?. data ) ; // TODO: move user context main context
170
160
const userStreams = getUserSepcificStreams ( getUserRolesData ?. data , getLogStreamListData ?. data as any ) ;
171
161
setUserSepecficStreams ( userStreams as any ) ;
172
162
} else {
@@ -209,10 +199,10 @@ const Navbar: FC<NavbarProps> = (props) => {
209
199
placeholder = "Pick one"
210
200
onChange = { ( value ) => handleChange ( value || '' ) }
211
201
nothingFound = "No options"
212
- value = { activeStream }
202
+ value = { selectedStream }
213
203
searchValue = { searchValue }
214
204
onSearchChange = { ( value ) => setSearchValue ( value ) }
215
- onDropdownClose = { ( ) => setSearchValue ( activeStream ) }
205
+ onDropdownClose = { ( ) => setSearchValue ( selectedStream ) }
216
206
onDropdownOpen = { ( ) => setSearchValue ( '' ) }
217
207
data = { userSepecficStreams ?. map ( ( stream : any ) => ( { value : stream . name , label : stream . name } ) ) ?? [ ] }
218
208
searchable
@@ -231,8 +221,9 @@ const Navbar: FC<NavbarProps> = (props) => {
231
221
) }
232
222
{ links . map ( ( link ) => {
233
223
if (
234
- link . requiredAccess &&
235
- ! userSepecficAccess ?. some ( ( access : string ) => link . requiredAccess . includes ( access ) )
224
+ ( link . requiredAccess &&
225
+ ! userSepecficAccess ?. some ( ( access : string ) => link . requiredAccess . includes ( access ) ) ) ||
226
+ selectedStream === ''
236
227
) {
237
228
return null ;
238
229
}
@@ -243,14 +234,15 @@ const Navbar: FC<NavbarProps> = (props) => {
243
234
sx = { { paddingLeft : 53 } }
244
235
disabled = { disableLink }
245
236
onClick = { ( ) => {
246
- handleChange ( activeStream , link . pathname ) ;
237
+ handleChange ( selectedStream , link . pathname ) ;
247
238
} }
248
239
key = { link . label }
249
240
className = { ( currentPage === link . pathname && linkBtnActive ) || linkBtn }
250
241
/>
251
242
) ;
252
243
} ) }
253
- { ! userSepecficAccess ?. some ( ( access : string ) => [ 'DeleteStream' ] . includes ( access ) ) ? null : (
244
+ { ! userSepecficAccess ?. some ( ( access : string ) => [ 'DeleteStream' ] . includes ( access ) ) ||
245
+ selectedStream === '' ? null : (
254
246
< NavLink
255
247
label = { 'Delete' }
256
248
icon = { < IconTrash size = "1.3rem" stroke = { 1.2 } /> }
@@ -305,14 +297,14 @@ const Navbar: FC<NavbarProps> = (props) => {
305
297
onChange = { ( e ) => {
306
298
setDeleteStream ( e . target . value ) ;
307
299
} }
308
- placeholder = { `Type the name of the stream to confirm. i.e. ${ activeStream } ` }
300
+ placeholder = { `Type the name of the stream to confirm. i.e. ${ selectedStream } ` }
309
301
required
310
302
/>
311
303
312
304
< Group mt = { 10 } position = "right" >
313
305
< Button
314
306
className = { modalActionBtn }
315
- disabled = { deleteStream === activeStream ? false : true }
307
+ disabled = { deleteStream === selectedStream ? false : true }
316
308
onClick = { handleDelete } >
317
309
Delete
318
310
</ Button >
0 commit comments