forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot-component.jsx
70 lines (60 loc) · 2.88 KB
/
root-component.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React, { useEffect } from 'react';
import { useIsHubRedirectionEnabled, useOauth2 } from '@deriv/hooks';
import { moduleLoader } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
const AppStore = React.lazy(() =>
moduleLoader(() => {
// eslint-disable-next-line import/no-unresolved
return import(/* webpackChunkName: "appstore" */ '@deriv/appstore');
})
);
const Wallets = React.lazy(() =>
moduleLoader(() => {
// eslint-disable-next-line import/no-unresolved
return import(/* webpackChunkName: "wallets" */ '@deriv/wallets');
})
);
const RootComponent = observer(props => {
const { client, ui } = useStore();
const {
is_wallets_onboarding_tour_guide_visible,
setIsWalletsOnboardingTourGuideVisible,
notification_messages_ui,
} = ui;
const { has_wallet, logout, prevent_redirect_to_hub, is_client_store_initialized } = client;
const { oAuthLogout } = useOauth2({ handleLogout: logout });
const onWalletsOnboardingTourGuideCloseHandler = () => {
setIsWalletsOnboardingTourGuideVisible(false);
};
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const PRODUCTION_REDIRECT_URL = 'https://hub.deriv.com/tradershub/home';
const STAGING_REDIRECT_URL = 'https://staging-hub.deriv.com/tradershub/home';
useEffect(() => {
if (isHubRedirectionEnabled && has_wallet && !prevent_redirect_to_hub && is_client_store_initialized) {
const redirectUrl = process.env.NODE_ENV === 'production' ? PRODUCTION_REDIRECT_URL : STAGING_REDIRECT_URL;
// NOTE: Clear OIDC related local storage, this is to prevent OIDC to re-apply client.accounts again from the callback page
localStorage.removeItem('config.account1');
localStorage.removeItem('config.tokens');
// NOTE: Clear local storage to prevent user from being logged in at Deriv.app since they should be logged in at low-code Traders Hub only
localStorage.removeItem('active_loginid');
localStorage.removeItem('active_user_id');
localStorage.removeItem('client.accounts');
localStorage.removeItem('active_wallet_loginid');
window.location.assign(redirectUrl);
}
}, [isHubRedirectionEnabled, has_wallet, prevent_redirect_to_hub, is_client_store_initialized]);
return has_wallet ? (
<Wallets
isWalletsOnboardingTourGuideVisible={is_wallets_onboarding_tour_guide_visible}
logout={async () => {
await oAuthLogout();
}}
notificationMessagesUi={notification_messages_ui}
onWalletsOnboardingTourGuideCloseHandler={onWalletsOnboardingTourGuideCloseHandler}
isHubRedirectionEnabled={isHubRedirectionEnabled}
/>
) : (
<AppStore {...props} />
);
});
export default RootComponent;