Skip to content

Commit 3f15e54

Browse files
Merge branch 'master' of github.com:deriv-com/deriv-app
2 parents 8820e8e + 18cf193 commit 3f15e54

File tree

14 files changed

+110
-15
lines changed

14 files changed

+110
-15
lines changed

packages/api-v2/src/AuthProvider.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ const AuthProvider = ({ loginIDKey, children, cookieTimeout, selectDefaultAccoun
154154
if (!activeAccount) return;
155155

156156
localStorage.setItem(loginIDKey ?? 'active_loginid', activeLoginID);
157+
sessionStorage.setItem(loginIDKey ?? 'active_loginid', activeLoginID);
157158
const isDemo = activeAccount.is_virtual;
158159
const shouldCreateNewWSConnection =
159160
(isDemo && wsClient?.endpoint === AppIDConstants.environments.real) ||

packages/api/src/APIProvider.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ type TAPIProviderProps = {
147147
const APIProvider = ({ children, standalone = false }: PropsWithChildren<TAPIProviderProps>) => {
148148
const WS = useWS();
149149
const [reconnect, setReconnect] = useState(false);
150-
const activeLoginid = window.localStorage.getItem('active_loginid');
150+
const activeLoginid =
151+
window.sessionStorage.getItem('active_loginid') || window.localStorage.getItem('active_loginid');
151152
const [environment, setEnvironment] = useState(getEnvironment(activeLoginid));
152153
const standaloneDerivAPI = useRef(standalone ? initializeDerivAPI(() => setReconnect(true)) : null);
153154
const subscriptions = useRef<Record<string, DerivAPIBasic['subscribe']>>();

packages/api/src/hooks/useAuthorize.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const useAuthorize = () => {
3232
(loginid: string) => {
3333
const active_loginid = getActiveLoginIDFromLocalStorage();
3434
if (active_loginid !== loginid) {
35+
sessionStorage.setItem('active_loginid', loginid);
3536
localStorage.setItem('active_loginid', loginid);
3637
switchEnvironment(active_loginid);
3738
// whenever we change the loginid, we need to invalidate all queries

packages/core/src/App/Components/Layout/Header/dtrader-v2/account-switcher-dtrader-v2.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ const AccountSwitcherDTraderV2 = observer(({ history }: TAccountSwitcherDTraderV
123123
}
124124
};
125125

126-
const getAccountItem = (item: typeof account_list[0], is_demo?: boolean) => (
126+
const getAccountItem = (item: (typeof account_list)[0], is_demo?: boolean) => (
127127
<AccountListDTraderV2
128128
key={item.loginid}
129129
balance={accounts[item?.loginid ?? '']?.balance}

packages/core/src/App/Components/Layout/Header/wallets/account-info-wallets.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ const AccountInfoWallets = observer(({ is_dialog_on, toggleDialog }: TAccountInf
116116
const { isDesktop } = useDevice();
117117

118118
const active_account = accounts?.[loginid ?? ''];
119-
const wallet_loginid = localStorage.getItem('active_wallet_loginid');
119+
const wallet_loginid =
120+
sessionStorage.getItem('active_wallet_loginid') || localStorage.getItem('active_wallet_loginid');
120121
const active_wallet =
121122
wallet_list?.find(wallet => wallet.loginid === wallet_loginid) ??
122123
wallet_list?.find(wallet => wallet.loginid === loginid);
@@ -126,7 +127,9 @@ const AccountInfoWallets = observer(({ is_dialog_on, toggleDialog }: TAccountInf
126127
if (active_wallet) {
127128
// get 'dtrade' loginid account linked to the current wallet
128129
linked_dtrade_trading_account_loginid =
129-
active_wallet.dtrade_loginid || linked_wallets_accounts.dtrade?.[0]?.loginid;
130+
sessionStorage.getItem('active_loginid') ||
131+
active_wallet.dtrade_loginid ||
132+
linked_wallets_accounts.dtrade?.[0]?.loginid;
130133

131134
// switch to dtrade account
132135
if (linked_dtrade_trading_account_loginid && linked_dtrade_trading_account_loginid !== loginid) {

packages/core/src/App/Containers/AccountSwitcherWallet/account-switcher-wallet-item.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const AccountSwitcherWalletItem = observer(
4242
if (is_dtrade_active) return;
4343
await switchAccount(dtrade_loginid);
4444
localStorage.setItem('active_wallet_loginid', loginid);
45+
sessionStorage.setItem('active_wallet_loginid', loginid);
4546
};
4647

4748
return (

packages/core/src/App/Containers/Redirect/redirect.jsx

+22
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,28 @@ const Redirect = observer(() => {
323323
}
324324
useEffect(() => {
325325
if (!redirected_to_route && history.location.pathname !== routes.traders_hub) {
326+
const account_currency = url_params.get('account');
327+
const client_account_lists = JSON.parse(localStorage.getItem('client.accounts'));
328+
329+
if (account_currency) {
330+
let matching_loginid;
331+
332+
const converted_account_currency = account_currency.toUpperCase();
333+
334+
if (converted_account_currency === 'DEMO') {
335+
matching_loginid = Object.keys(client_account_lists).find(loginid => /^VR/.test(loginid));
336+
} else {
337+
matching_loginid = Object.keys(client_account_lists).find(
338+
loginid =>
339+
client_account_lists[loginid].currency?.toUpperCase() === converted_account_currency &&
340+
client_account_lists[loginid].account_category === 'trading'
341+
);
342+
}
343+
344+
if (matching_loginid) {
345+
sessionStorage.setItem('active_loginid', matching_loginid);
346+
}
347+
}
326348
const route_mappings = [
327349
{ pattern: /accumulator/i, route: routes.trade, type: 'accumulator' },
328350
{ pattern: /turbos/i, route: routes.trade, type: 'turboslong' },

packages/core/src/App/app.jsx

+36
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,42 @@ const AppWithoutTranslation = ({ root_store }) => {
5151
const is_dark_mode = is_dark_mode_on || JSON.parse(localStorage.getItem('ui_store'))?.is_dark_mode_on;
5252
const language = preferred_language ?? getInitialLanguage();
5353

54+
const url_query_string = window.location.search;
55+
const url_params = new URLSearchParams(url_query_string);
56+
const account_currency = url_params.get('account');
57+
const client_account_lists = JSON.parse(localStorage.getItem('client.accounts'));
58+
59+
if (account_currency) {
60+
let matching_loginid, matching_wallet_loginid;
61+
62+
const converted_account_currency = account_currency.toUpperCase();
63+
64+
if (converted_account_currency === 'DEMO') {
65+
matching_loginid = Object.keys(client_account_lists).find(loginid => /^VRTC/.test(loginid));
66+
matching_wallet_loginid = Object.keys(client_account_lists).find(loginid => /^VRW/.test(loginid));
67+
} else {
68+
matching_loginid = Object.keys(client_account_lists).find(
69+
loginid =>
70+
client_account_lists[loginid].currency?.toUpperCase() === converted_account_currency &&
71+
client_account_lists[loginid].account_category === 'trading' &&
72+
client_account_lists[loginid]?.landing_company !== 'virtual'
73+
);
74+
matching_wallet_loginid = Object.keys(client_account_lists).find(
75+
loginid =>
76+
client_account_lists[loginid].currency?.toUpperCase() === converted_account_currency &&
77+
client_account_lists[loginid].account_category === 'wallet' &&
78+
client_account_lists[loginid]?.landing_company !== 'virtual'
79+
);
80+
}
81+
82+
if (matching_loginid) {
83+
sessionStorage.setItem('active_loginid', matching_loginid);
84+
}
85+
if (matching_wallet_loginid) {
86+
sessionStorage.setItem('active_wallet_loginid', matching_wallet_loginid);
87+
}
88+
}
89+
5490
React.useEffect(() => {
5591
const dir = i18n.dir(i18n.language.toLowerCase());
5692
document.documentElement.dir = dir;

packages/core/src/Stores/client-store.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ export default class ClientStore extends BaseStore {
359359
setLoginId: action.bound,
360360
setAccounts: action.bound,
361361
setSwitched: action.bound,
362+
setUrlParams: action.bound,
362363
setIsAuthorize: action.bound,
363364
setIsLoggingIn: action.bound,
364365
setPreSwitchAccount: action.bound,
@@ -1065,11 +1066,22 @@ export default class ClientStore extends BaseStore {
10651066
resetLocalStorageValues(loginid) {
10661067
this.accounts[loginid].accepted_bch = 0;
10671068
LocalStore.setObject(storage_key, this.accounts);
1068-
LocalStore.set('active_loginid', loginid);
1069+
sessionStorage.setItem('active_loginid', loginid);
1070+
this.setUrlParams();
10691071
this.syncWithLegacyPlatforms(loginid, toJS(this.accounts));
10701072
this.loginid = loginid;
10711073
}
10721074

1075+
setUrlParams() {
1076+
const url = new URL(window.location.href);
1077+
const loginid = sessionStorage.getItem('active_wallet_loginid') || sessionStorage.getItem('active_loginid');
1078+
const account_param = /^VR/.test(loginid) ? 'demo' : this.accounts[loginid]?.currency;
1079+
if (account_param) {
1080+
url.searchParams.set('account', account_param);
1081+
window.history.replaceState({}, '', url.toString());
1082+
}
1083+
}
1084+
10731085
setIsAuthorize(value) {
10741086
this.is_authorize = value;
10751087
}
@@ -1576,7 +1588,7 @@ export default class ClientStore extends BaseStore {
15761588

15771589
if (['crypto_transactions_withdraw', 'payment_withdraw'].includes(action_param) && loginid_param)
15781590
this.setLoginId(loginid_param);
1579-
else this.setLoginId(LocalStore.get('active_loginid'));
1591+
else this.setLoginId(window.sessionStorage.getItem('active_loginid') || LocalStore.get('active_loginid'));
15801592
this.user_id = LocalStore.get('active_user_id');
15811593
this.setAccounts(LocalStore.getObject(storage_key));
15821594
this.setSwitched('');
@@ -2005,11 +2017,15 @@ export default class ClientStore extends BaseStore {
20052017

20062018
//temporary workaround to sync this.loginid with selected wallet loginid
20072019
if (window.location.pathname.includes(routes.wallets)) {
2008-
this.resetLocalStorageValues(localStorage.getItem('active_loginid') ?? this.loginid);
2020+
this.resetLocalStorageValues(
2021+
window.sessionStorage.getItem('active_loginid') ??
2022+
localStorage.getItem('active_loginid') ??
2023+
this.loginid
2024+
);
20092025
return;
20102026
}
20112027

2012-
this.resetLocalStorageValues(this.loginid);
2028+
this.resetLocalStorageValues(window.sessionStorage.getItem('active_loginid') ?? this.loginid);
20132029
}
20142030
}
20152031

@@ -2232,11 +2248,12 @@ export default class ClientStore extends BaseStore {
22322248
if (active_loginid && Object.keys(client_object).length) {
22332249
if (selected_account && is_wallets_selected) {
22342250
localStorage.setItem('active_wallet_loginid', active_wallet_loginid);
2251+
sessionStorage.setItem('active_wallet_loginid', active_wallet_loginid);
22352252
if (verification_code) {
22362253
localStorage.setItem('verification_code.payment_withdraw', verification_code);
22372254
}
22382255
}
2239-
2256+
sessionStorage.setItem('active_loginid', active_loginid);
22402257
localStorage.setItem('active_loginid', active_loginid);
22412258
localStorage.setItem('client.accounts', JSON.stringify(client_object));
22422259
this.syncWithLegacyPlatforms(active_loginid, this.accounts);

packages/shared/src/utils/config/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export const getSocketURL = (is_wallets = false) => {
8888
active_loginid_from_url = params.get('acct1');
8989
}
9090
const local_storage_loginid = is_wallets
91-
? window.localStorage.getItem('active_wallet_loginid')
92-
: window.localStorage.getItem('active_loginid');
91+
? window.sessionStorage.getItem('active_wallet_loginid') || window.localStorage.getItem('active_wallet_loginid')
92+
: window.sessionStorage.getItem('active_loginid') || window.localStorage.getItem('active_loginid');
9393
const loginid = local_storage_loginid || active_loginid_from_url;
9494
const is_real = loginid && !/^(VRT|VRW)/.test(loginid);
9595

packages/utils/src/getActiveLoginIDFromLocalStorage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @deprecated Please use 'WebSocketUtils.getActiveLoginid' from '@deriv-com/utils' instead of this.
66
*/
77
const getActiveLoginIDFromLocalStorage = (loginid_key = 'active_loginid') => {
8-
const active_custom_loginid = localStorage.getItem(loginid_key);
8+
const active_custom_loginid = sessionStorage.getItem(loginid_key) || localStorage.getItem(loginid_key);
99
return active_custom_loginid ?? undefined;
1010
};
1111

packages/wallets/src/hooks/__tests__/useWalletAccountSwitcher.spec.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ describe('useWalletAccountSwitcher', () => {
106106
const switchWalletAccount = result.current;
107107

108108
await switchWalletAccount('CRW34569');
109-
expect(global.localStorage.getItem('active_loginid')).toBe('CR34567');
109+
expect(global.sessionStorage.getItem('active_loginid')).toBe('CR34567');
110110
});
111111
});

packages/wallets/src/hooks/useSyncLocalStorageClientAccounts.ts

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const useSyncLocalStorageClientAccounts = () => {
122122
platform: 'dtrade',
123123
},
124124
];
125+
sessionStorage.setItem('active_loginid', newAccount.client_id);
125126
}
126127

127128
setLocalStorageClientAccounts(localStorageData);

packages/wallets/src/hooks/useWalletAccountSwitcher.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const useWalletAccountSwitcher = () => {
77

88
const switchWalletAccount = useCallback(
99
async (loginid: string) => {
10+
const url = new URL(window.location.href);
1011
const dtradeAccount = walletAccounts
1112
?.find(account => account.loginid === loginid)
1213
?.linked_to?.find(linkedAccount => linkedAccount.platform === 'dtrade');
@@ -16,8 +17,19 @@ const useWalletAccountSwitcher = () => {
1617
account => account.loginid === dtradeAccount?.loginid
1718
);
1819

19-
if (dtradeAccount?.loginid && !linkedAccountDetails?.is_disabled)
20-
localStorage.setItem('active_loginid', dtradeAccount.loginid);
20+
const accountLoginId =
21+
sessionStorage.getItem('active_wallet_loginid') || sessionStorage.getItem('active_loginid') || '';
22+
const accountParam = /^VR/.test(accountLoginId)
23+
? 'demo'
24+
: authorizeData.account_list?.find(account => account.loginid === accountLoginId)?.currency;
25+
if (accountParam) {
26+
url.searchParams.set('account', accountParam);
27+
window.history.replaceState({}, '', url.toString());
28+
}
29+
30+
if (dtradeAccount?.loginid && !linkedAccountDetails?.is_disabled) {
31+
sessionStorage.setItem('active_loginid', dtradeAccount.loginid);
32+
}
2133
},
2234
[_switchAccount, authorizeData.account_list, walletAccounts]
2335
);

0 commit comments

Comments
 (0)