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 } from "./systemSlice" ;
28
+ import { SessionCallStates } from "./screens/Console/consoleSlice" ;
36
29
37
30
interface ProtectedRouteProps {
38
31
Component : any ;
@@ -41,8 +34,14 @@ 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
+ // sessionLoading is used to control rendering this page, if we only use console.sessionLoadingState,
39
+ // since the state chages it triggers the reload of this component multiple times, causing duplicate queries.
40
+ const [ sessionLoading , setSessionLoading ] = useState < boolean > ( true ) ;
41
+ const sessionLoadingState = useSelector (
42
+ ( state : AppState ) => state . console . sessionLoadingState
43
+ ) ;
44
+
46
45
const anonymousMode = useSelector (
47
46
( state : AppState ) => state . system . anonymousMode
48
47
) ;
@@ -53,56 +52,16 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
53
52
return < Navigate to = { { pathname : `login` } } /> ;
54
53
} ;
55
54
56
- const pathnameParts = pathname . split ( "/" ) ;
57
- const screen = pathnameParts . length > 2 ? pathnameParts [ 1 ] : "" ;
55
+ // update location path and store it
56
+ useEffect ( ( ) => {
57
+ dispatch ( fetchSession ( pathname ) ) ;
58
+ } , [ dispatch , pathname ] ) ;
58
59
59
60
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 ] ) ;
61
+ if ( sessionLoadingState === SessionCallStates . Done ) {
62
+ setSessionLoading ( false ) ;
63
+ }
64
+ } , [ dispatch , sessionLoadingState ] ) ;
106
65
107
66
const [ , invokeSRInfoApi ] = useApi (
108
67
( res : any ) => {
@@ -142,6 +101,7 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
142
101
if ( sessionLoading ) {
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