Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Lubega/TRAH: 5324/Change redirection for residence" #18015

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions packages/api-v2/src/hooks/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import useAuthorize from './useAuthorize';
import useClientCountry from './useClientCountry';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';
import useSettings from './useSettings';

type THubEnabledCountryList = {
hub_enabled_country_list: string[];
Expand All @@ -11,17 +11,24 @@ const useIsHubRedirectionEnabled = () => {
featureFlag: 'hub_enabled_country_list',
});
const { data: clientCountry } = useClientCountry();
const { data: authorize } = useAuthorize();
const country = authorize?.country ? authorize.country : clientCountry;
const { data: accountSettings } = useSettings();
const { citizen } = accountSettings;

const isHubRedirectionEnabled =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(country);
citizen &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(citizen);

return { isHubRedirectionEnabled };
const isChangingToHubAppId =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
clientCountry &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clientCountry);

return { isHubRedirectionEnabled, isChangingToHubAppId };
};

export default useIsHubRedirectionEnabled;
6 changes: 3 additions & 3 deletions packages/core/src/App/AppContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const AppContent: React.FC<{ passthrough: unknown }> = observer(({ passthrough }
await logout();
},
});
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const { isChangingToHubAppId } = useIsHubRedirectionEnabled();

const is_app_id_set = localStorage.getItem('config.app_id');
const is_change_login_app_id_set = localStorage.getItem('change_login_app_id');
Expand Down Expand Up @@ -110,13 +110,13 @@ const AppContent: React.FC<{ passthrough: unknown }> = observer(({ passthrough }
useIntercom(token);

React.useEffect(() => {
if (isHubRedirectionEnabled && !is_app_id_set) {
if (isChangingToHubAppId && !is_app_id_set) {
const app_id = process.env.NODE_ENV === 'production' ? 61554 : 53503;
localStorage.setItem('change_login_app_id', app_id.toString());
return;
}
is_change_login_app_id_set && localStorage.removeItem('change_login_app_id');
}, [isHubRedirectionEnabled, is_app_id_set, is_change_login_app_id_set]);
}, [isChangingToHubAppId, is_app_id_set, is_change_login_app_id_set]);

React.useEffect(() => {
switchLanguage(current_language);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { Div100vhContainer, Icon, useOnClickOutside, Text } from '@deriv/components';
import { routes, getActivePlatform, platforms } from '@deriv/shared';
import { BinaryLink } from 'App/Components/Routes';
import 'Sass/app/_common/components/platform-dropdown.scss';
import { Localize } from '@deriv/translations';
import { useHistory } from 'react-router';

import { Div100vhContainer, Icon, Text, useOnClickOutside } from '@deriv/components';
import { useDevice } from '@deriv-com/ui';
import { useIsHubRedirectionEnabled } from '@deriv/hooks';
import { getActivePlatform, platforms, routes } from '@deriv/shared';
import { useStore } from '@deriv/stores';
import { Localize } from '@deriv/translations';
import { useDevice } from '@deriv-com/ui';

import { BinaryLink } from 'App/Components/Routes';

import 'Sass/app/_common/components/platform-dropdown.scss';

const PlatformBox = ({ platform: { icon, description } }) => (
<React.Fragment>
Expand Down Expand Up @@ -57,14 +54,15 @@ const PlatformDropdown = ({ app_routing_history, closeDrawer, platform_config, s
const { isDesktop } = useDevice();
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const { client } = useStore();
const { has_wallet } = client;
const { account_settings, has_wallet } = client;
const { trading_hub } = account_settings;

const TradersHubRedirect = () => {
return (
<div className='platform-dropdown__cta'>
<BinaryLink
onClick={() => {
if (isHubRedirectionEnabled && has_wallet) {
if (isHubRedirectionEnabled && has_wallet && !!trading_hub) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import React from 'react';
import { useHistory } from 'react-router';

import { Button, Icon, MobileDialog, Text } from '@deriv/components';
import { useIsHubRedirectionEnabled, useIsRtl, useStoreWalletAccountsList } from '@deriv/hooks';
import { platforms, routes } from '@deriv/shared';
import { observer } from '@deriv/stores';
import { Localize } from '@deriv/translations';

import { AccountSwitcherWalletList } from './account-switcher-wallet-list';

import { useIsHubRedirectionEnabled, useIsRtl, useStoreWalletAccountsList } from '@deriv/hooks';
import { observer, useStore } from '@deriv/stores';
import './account-switcher-wallet-mobile.scss';

type TAccountSwitcherWalletMobile = {
Expand All @@ -21,6 +18,9 @@ export const AccountSwitcherWalletMobile = observer(({ is_visible, toggle, login
const history = useHistory();
const isRtl = useIsRtl();
const { data: wallet_list } = useStoreWalletAccountsList();
const { client } = useStore();
const { account_settings } = client;
const { trading_hub } = account_settings;

const dtrade_account_wallets = wallet_list?.filter(wallet => wallet.dtrade_loginid);
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
Expand All @@ -30,7 +30,7 @@ export const AccountSwitcherWalletMobile = observer(({ is_visible, toggle, login
}, [toggle]);

const handleTradersHubRedirect = () => {
if (isHubRedirectionEnabled) {
if (isHubRedirectionEnabled && !!trading_hub) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import React from 'react';
import { useHistory } from 'react-router';

import { Icon, Text, ThemedScrollbars, useOnClickOutside } from '@deriv/components';
import { useIsHubRedirectionEnabled, useStoreWalletAccountsList } from '@deriv/hooks';
import { platforms, routes } from '@deriv/shared';
import { observer } from '@deriv/stores';
import { observer, useStore } from '@deriv/stores';
import { Localize } from '@deriv/translations';

import { useIsHubRedirectionEnabled, useStoreWalletAccountsList } from '@deriv/hooks';
import { AccountSwitcherWalletList } from './account-switcher-wallet-list';

import './account-switcher-wallet.scss';

type TAccountSwitcherWalletProps = {
Expand All @@ -21,6 +18,9 @@ export const AccountSwitcherWallet = observer(({ is_visible, toggle }: TAccountS
const dtrade_account_wallets = wallet_list?.filter(wallet => wallet.dtrade_loginid);

const history = useHistory();
const { client } = useStore();
const { account_settings } = client;
const { trading_hub } = account_settings;

const wrapper_ref = React.useRef<HTMLDivElement>(null);
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
Expand All @@ -43,7 +43,7 @@ export const AccountSwitcherWallet = observer(({ is_visible, toggle }: TAccountS
useOnClickOutside(wrapper_ref, closeAccountsDialog, validateClickOutside);

const handleTradersHubRedirect = async () => {
if (isHubRedirectionEnabled) {
if (isHubRedirectionEnabled && !!trading_hub) {
window.location.assign(platforms.tradershub_os.url);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const RootComponent = observer(props => {
setIsWalletsOnboardingTourGuideVisible,
notification_messages_ui,
} = ui;
const { has_wallet, logout, prevent_redirect_to_hub } = client;
const { has_wallet, logout, account_settings, prevent_redirect_to_hub } = client;
const { trading_hub } = account_settings;

const { oAuthLogout } = useOauth2({ handleLogout: logout });

Expand All @@ -38,11 +39,11 @@ const RootComponent = observer(props => {
const STAGING_REDIRECT_URL = 'https://staging-hub.deriv.com/tradershub/options';

useEffect(() => {
if (isHubRedirectionEnabled && has_wallet && !prevent_redirect_to_hub) {
if (isHubRedirectionEnabled && has_wallet && !!trading_hub && !prevent_redirect_to_hub) {
const redirectUrl = process.env.NODE_ENV === 'production' ? PRODUCTION_REDIRECT_URL : STAGING_REDIRECT_URL;
window.location.assign(redirectUrl);
}
}, [isHubRedirectionEnabled, has_wallet, prevent_redirect_to_hub]);
}, [isHubRedirectionEnabled, has_wallet, trading_hub, prevent_redirect_to_hub]);

return has_wallet ? (
<Wallets
Expand All @@ -52,7 +53,7 @@ const RootComponent = observer(props => {
}}
notificationMessagesUi={notification_messages_ui}
onWalletsOnboardingTourGuideCloseHandler={onWalletsOnboardingTourGuideCloseHandler}
isHubRedirectionEnabled={isHubRedirectionEnabled}
isHubRedirectionEnabled={isHubRedirectionEnabled && !!trading_hub}
/>
) : (
<AppStore {...props} />
Expand Down
55 changes: 24 additions & 31 deletions packages/hooks/src/__tests__/useIsHubRedirectionEnabled.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react';

import { mockStore, StoreProvider } from '@deriv/stores';
import { renderHook } from '@testing-library/react-hooks';

import useAuthorize from '../useAuthorize';
import useGrowthbookGetFeatureValue from '../useGrowthbookGetFeatureValue';
import useIsHubRedirectionEnabled from '../useIsHubRedirectionEnabled';
import { mockStore, StoreProvider } from '@deriv/stores';

jest.mock('../useGrowthbookGetFeatureValue', () =>
jest.fn(() => [
Expand All @@ -15,70 +13,65 @@ jest.mock('../useGrowthbookGetFeatureValue', () =>
])
);

jest.mock('../useAuthorize', () =>
jest.fn(() => ({
data: {
country: 'UK',
},
}))
);

describe('useIsHubRedirectionEnabled', () => {
const mock_store = mockStore({
client: {
account_settings: { citizen: 'US' },
clients_country: 'US',
},
});

beforeEach(() => {
mock_store.client.clients_country = 'US';
mock_store.client.account_settings.citizen = 'US';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['US', 'AU', 'UK'],
hub_enabled_country_list: ['AU'],
},
]);
(useAuthorize as jest.Mock).mockReturnValue({
data: {
country: 'UK',
},
});
});

const wrapper = ({ children }: { children: JSX.Element }) => (
<StoreProvider store={mock_store}>{children}</StoreProvider>
);

it('should return initial state correctly', () => {
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['AU'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });

expect(result.current.isHubRedirectionEnabled).toBe(false);
expect(result.current.isChangingToHubAppId).toBe(false);
});

it('should return true if authorize country is in the hub enabled list', () => {
it('should return false if client country is not in the hub enabled list', () => {
mock_store.client.account_settings.citizen = 'UK';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['US', 'AU'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(true);
expect(result.current.isHubRedirectionEnabled).toBe(false);
});

it('should return true if client country is in the hub enabled list and authorize is undefined', () => {
(useAuthorize as jest.Mock).mockReturnValue({
data: undefined,
});
it('should return true if client country is in the hub enabled list', () => {
mock_store.client.clients_country = 'UK';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['US', 'AU', 'UK'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(true);
});

it('should return false if authorize and client country is not in the hub enabled list', () => {
it('should return isChangingToHubAppId true if client country is in the hub enabled list but not in the citizen list', () => {
mock_store.client.clients_country = 'UK';
(useGrowthbookGetFeatureValue as jest.Mock).mockReturnValue([
{
hub_enabled_country_list: ['AU'],
hub_enabled_country_list: ['US', 'AU', 'UK'],
},
]);
const { result } = renderHook(() => useIsHubRedirectionEnabled(), { wrapper });
expect(result.current.isHubRedirectionEnabled).toBe(false);
expect(result.current.isChangingToHubAppId).toBe(true);
});
});
18 changes: 11 additions & 7 deletions packages/hooks/src/useIsHubRedirectionEnabled.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useStore } from '@deriv/stores';

import useAuthorize from './useAuthorize';
import useGrowthbookGetFeatureValue from './useGrowthbookGetFeatureValue';

type THubEnabledCountryList = {
Expand All @@ -11,19 +10,24 @@ const useIsHubRedirectionEnabled = () => {
const [hubEnabledCountryList] = useGrowthbookGetFeatureValue({
featureFlag: 'hub_enabled_country_list',
});
const { data: authorize } = useAuthorize();
const { client } = useStore();
const { clients_country } = client;
const country = authorize?.country ? authorize.country : clients_country;
const { account_settings, clients_country } = client;

const isHubRedirectionEnabled =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(country);
account_settings.citizen &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(account_settings.citizen);

return { isHubRedirectionEnabled };
const isChangingToHubAppId =
typeof hubEnabledCountryList === 'object' &&
hubEnabledCountryList !== null &&
Array.isArray((hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list) &&
clients_country &&
(hubEnabledCountryList as THubEnabledCountryList).hub_enabled_country_list.includes(clients_country);

return { isHubRedirectionEnabled, isChangingToHubAppId };
};

export default useIsHubRedirectionEnabled;
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import React, { createContext, useContext, useEffect, useMemo, useRef, useState
import { createPortal } from 'react-dom';
import { useHistory } from 'react-router-dom';
import { useOnClickOutside } from 'usehooks-ts';
import { useIsHubRedirectionEnabled } from '@deriv/api-v2';
import { useDevice } from '@deriv-com/ui';
import { THooks, TMarketTypes, TPlatforms } from '../../types';
import { useIsHubRedirectionEnabled, useSettings } from '@deriv/api-v2';

type TModalState = {
accountId?: string;
Expand Down Expand Up @@ -48,6 +48,8 @@ const ModalProvider = ({ children }: React.PropsWithChildren<unknown>) => {
const { isDesktop } = useDevice();
const history = useHistory();
const { isHubRedirectionEnabled } = useIsHubRedirectionEnabled();
const { data: accountSettings } = useSettings();
const { trading_hub: tradingHub } = accountSettings;

const rootRef = useRef<HTMLElement>(document.getElementById('wallets_modal_root'));
const rootHeaderRef = useRef<HTMLElement | null>(document.getElementById('wallets_modal_show_header_root'));
Expand Down Expand Up @@ -87,7 +89,7 @@ const ModalProvider = ({ children }: React.PropsWithChildren<unknown>) => {
}));
// We need to add this check here because wallets account is coming from Low-Code tradershub.
// This condition is to reload the page when the modal is closed.
if (isHubRedirectionEnabled) {
if (isHubRedirectionEnabled && !!tradingHub) {
window.location.reload();
}
};
Expand Down
Loading
Loading