1414// You should have received a copy of the GNU Affero General Public License
1515// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17- import React , { useEffect , useState } from "react" ;
17+ import { useEffect , useState } from "react" ;
1818import { Navigate , useLocation } from "react-router-dom" ;
1919import useApi from "./screens/Console/Common/Hooks/useApi" ;
2020import { ErrorResponseHandler } from "./common/types" ;
2121import { ReplicationSite } from "./screens/Console/Configurations/SiteReplication/SiteReplication" ;
2222import { useSelector } from "react-redux" ;
23- import {
24- globalSetDistributedSetup ,
25- setAnonymousMode ,
26- setOverrideStyles ,
27- setSiteReplicationInfo ,
28- userLogged ,
29- } from "./systemSlice" ;
3023import { SRInfoStateType } from "./types" ;
3124import { AppState , useAppDispatch } from "./store" ;
32- import { saveSessionResponse } from "./screens/Console/consoleSlice" ;
33- import { getOverrideColorVariants } from "./utils/stylesUtils" ;
3425import 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" ;
3629
3730interface ProtectedRouteProps {
3831 Component : any ;
@@ -41,8 +34,11 @@ interface ProtectedRouteProps {
4134const ProtectedRoute = ( { Component } : ProtectedRouteProps ) => {
4235 const dispatch = useAppDispatch ( ) ;
4336
44- const [ sessionLoading , setSessionLoading ] = useState < boolean > ( true ) ;
4537 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+ ) ;
4642 const anonymousMode = useSelector (
4743 ( state : AppState ) => state . system . anonymousMode
4844 ) ;
@@ -53,56 +49,19 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
5349 return < Navigate to = { { pathname : `login` } } /> ;
5450 } ;
5551
56- const pathnameParts = pathname . split ( "/" ) ;
57- const screen = pathnameParts . length > 2 ? pathnameParts [ 1 ] : "" ;
52+ useEffect ( ( ) => {
53+ dispatch ( setLocationPath ( pathname ) ) ;
54+ } , [ dispatch , pathname ] ) ;
5855
5956 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 ] ) ;
10665
10766 const [ , invokeSRInfoApi ] = useApi (
10867 ( res : any ) => {
@@ -132,16 +91,17 @@ const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
13291 ) ;
13392
13493 useEffect ( ( ) => {
135- if ( userLoggedIn && ! sessionLoading && ! anonymousMode ) {
94+ if ( userLoggedIn && ! componentLoading && ! anonymousMode ) {
13695 invokeSRInfoApi ( "GET" , `api/v1/admin/site-replication` ) ;
13796 }
13897 // eslint-disable-next-line react-hooks/exhaustive-deps
139- } , [ userLoggedIn , sessionLoading ] ) ;
98+ } , [ userLoggedIn , componentLoading ] ) ;
14099
141100 // if we're still trying to retrieve user session render nothing
142- if ( sessionLoading ) {
101+ if ( componentLoading ) {
143102 return < LoadingComponent /> ;
144103 }
104+
145105 // redirect user to the right page based on session status
146106 return userLoggedIn ? < Component /> : < StorePathAndRedirect /> ;
147107} ;
0 commit comments