14
14
// You should have received a copy of the GNU Affero General Public License
15
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
- import React , { useEffect , useState } from "react" ;
17
+ import { useEffect , useState } from "react" ;
18
18
import { Navigate , useLocation } from "react-router-dom" ;
19
19
import useApi from "./screens/Console/Common/Hooks/useApi" ;
20
20
import { ErrorResponseHandler } from "./common/types" ;
21
21
import { ReplicationSite } from "./screens/Console/Configurations/SiteReplication/SiteReplication" ;
22
22
import { useSelector } from "react-redux" ;
23
- import {
24
- globalSetDistributedSetup ,
25
- setAnonymousMode ,
26
- setOverrideStyles ,
27
- setSiteReplicationInfo ,
28
- userLogged ,
29
- } from "./systemSlice" ;
30
23
import { SRInfoStateType } from "./types" ;
31
24
import { AppState , useAppDispatch } from "./store" ;
32
- import { saveSessionResponse } from "./screens/Console/consoleSlice" ;
33
- import { getOverrideColorVariants } from "./utils/stylesUtils" ;
34
25
import LoadingComponent from "./common/LoadingComponent" ;
35
- import { api } from "api" ;
26
+ import { fetchSession } from "./screens/LoginPage/sessionThunk" ;
27
+ import { setSiteReplicationInfo , setLocationPath } from "./systemSlice" ;
28
+ import { SessionCallStates } from "./screens/Console/consoleSlice.types" ;
36
29
37
30
interface ProtectedRouteProps {
38
31
Component : any ;
@@ -41,8 +34,11 @@ interface ProtectedRouteProps {
41
34
const ProtectedRoute = ( { Component } : ProtectedRouteProps ) => {
42
35
const dispatch = useAppDispatch ( ) ;
43
36
44
- const [ sessionLoading , setSessionLoading ] = useState < boolean > ( true ) ;
45
37
const userLoggedIn = useSelector ( ( state : AppState ) => state . system . loggedIn ) ;
38
+ const [ componentLoading , setComponentLoading ] = useState < boolean > ( true ) ;
39
+ const sessionLoadingState = useSelector (
40
+ ( state : AppState ) => state . console . sessionLoadingState
41
+ ) ;
46
42
const anonymousMode = useSelector (
47
43
( state : AppState ) => state . system . anonymousMode
48
44
) ;
@@ -53,56 +49,19 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
53
49
return < Navigate to = { { pathname : `login` } } /> ;
54
50
} ;
55
51
56
- const pathnameParts = pathname . split ( "/" ) ;
57
- const screen = pathnameParts . length > 2 ? pathnameParts [ 1 ] : "" ;
52
+ useEffect ( ( ) => {
53
+ dispatch ( setLocationPath ( pathname ) ) ;
54
+ } , [ dispatch , pathname ] ) ;
58
55
59
56
useEffect ( ( ) => {
60
- api . session
61
- . sessionCheck ( )
62
- . then ( ( res ) => {
63
- dispatch ( saveSessionResponse ( res . data ) ) ;
64
- dispatch ( userLogged ( true ) ) ;
65
- setSessionLoading ( false ) ;
66
- dispatch ( globalSetDistributedSetup ( res . data ?. distributedMode || false ) ) ;
67
-
68
- if ( res . data . customStyles && res . data . customStyles !== "" ) {
69
- const overrideColorVariants = getOverrideColorVariants (
70
- res . data . customStyles
71
- ) ;
72
-
73
- if ( overrideColorVariants !== false ) {
74
- dispatch ( setOverrideStyles ( overrideColorVariants ) ) ;
75
- }
76
- }
77
- } )
78
- . catch ( ( ) => {
79
- // if we are trying to browse, probe access to the requested prefix
80
- if ( screen === "browser" ) {
81
- const bucket = pathnameParts . length >= 3 ? pathnameParts [ 2 ] : "" ;
82
- // no bucket, no business
83
- if ( bucket === "" ) {
84
- setSessionLoading ( false ) ;
85
- return ;
86
- }
87
- // before marking the session as done, let's check if the bucket is publicly accessible
88
- api . buckets
89
- . listObjects (
90
- bucket ,
91
- { limit : 1 } ,
92
- { headers : { "X-Anonymous" : "1" } }
93
- )
94
- . then ( ( ) => {
95
- dispatch ( setAnonymousMode ( ) ) ;
96
- setSessionLoading ( false ) ;
97
- } )
98
- . catch ( ( ) => {
99
- setSessionLoading ( false ) ;
100
- } ) ;
101
- } else {
102
- setSessionLoading ( false ) ;
103
- }
104
- } ) ;
105
- } , [ dispatch , screen , pathnameParts ] ) ;
57
+ dispatch ( fetchSession ( ) ) ;
58
+ } , [ dispatch ] ) ;
59
+
60
+ useEffect ( ( ) => {
61
+ if ( sessionLoadingState === SessionCallStates . Done ) {
62
+ setComponentLoading ( false ) ;
63
+ }
64
+ } , [ dispatch , sessionLoadingState ] ) ;
106
65
107
66
const [ , invokeSRInfoApi ] = useApi (
108
67
( res : any ) => {
@@ -132,16 +91,17 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
132
91
) ;
133
92
134
93
useEffect ( ( ) => {
135
- if ( userLoggedIn && ! sessionLoading && ! anonymousMode ) {
94
+ if ( userLoggedIn && ! componentLoading && ! anonymousMode ) {
136
95
invokeSRInfoApi ( "GET" , `api/v1/admin/site-replication` ) ;
137
96
}
138
97
// eslint-disable-next-line react-hooks/exhaustive-deps
139
- } , [ userLoggedIn , sessionLoading ] ) ;
98
+ } , [ userLoggedIn , componentLoading ] ) ;
140
99
141
100
// if we're still trying to retrieve user session render nothing
142
- if ( sessionLoading ) {
101
+ if ( componentLoading ) {
143
102
return < LoadingComponent /> ;
144
103
}
104
+
145
105
// redirect user to the right page based on session status
146
106
return userLoggedIn ? < Component /> : < StorePathAndRedirect /> ;
147
107
} ;
0 commit comments