-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathindex.tsx
More file actions
160 lines (138 loc) · 6.65 KB
/
Copy pathindex.tsx
File metadata and controls
160 lines (138 loc) · 6.65 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { useEffect, useState } from 'react';
import clsx from 'clsx';
import { observer } from 'mobx-react-lite';
import { Outlet } from 'react-router-dom';
import { api_base } from '@/external/bot-skeleton';
import { useStore } from '@/hooks/useStore';
import { useDevice } from '@deriv-com/ui';
import { crypto_currencies_display_order, fiat_currencies_display_order } from '../shared';
import Footer from './footer';
import AppHeader from './header';
import Body from './main-body';
import './layout.scss';
const Layout = observer(() => {
const { isDesktop } = useDevice();
const store = useStore();
const is_quick_strategy_active = store?.quick_strategy?.is_open;
const isCallbackPage = window.location.pathname === '/callback';
const checkClientAccount = JSON.parse(localStorage.getItem('clientAccounts') ?? '{}');
const getQueryParams = new URLSearchParams(window.location.search);
const currency = getQueryParams.get('account') ?? '';
const accountsList = JSON.parse(localStorage.getItem('accountsList') ?? '{}');
const isClientAccountsPopulated = Object.keys(accountsList).length > 0;
const ifClientAccountHasCurrency =
Object.values(checkClientAccount).some((account: any) => account.currency === currency) ||
currency === 'demo' ||
currency === '';
const [clientHasCurrency, setClientHasCurrency] = useState(ifClientAccountHasCurrency);
const [isAuthenticating, setIsAuthenticating] = useState(true); // Start with true to prevent flashing
// Expose setClientHasCurrency to window for global access
useEffect(() => {
(window as any).setClientHasCurrency = setClientHasCurrency;
return () => {
delete (window as any).setClientHasCurrency;
};
}, []);
const validCurrencies = [...fiat_currencies_display_order, ...crypto_currencies_display_order];
const query_currency = (getQueryParams.get('account') ?? '')?.toUpperCase();
const isCurrencyValid = validCurrencies.includes(query_currency);
const api_accounts: any[][] = [];
let subscription: { unsubscribe: () => void };
const validateApiAccounts = ({ data }: any) => {
//TO do work on this with account switcher
if (data.msg_type === 'authorize') {
const account_list = data?.authorize?.account_list || [];
const account_list_filter = account_list.filter((acc: any) => acc.is_disabled === 0);
api_accounts.push(account_list_filter || []);
const allCurrencies = new Set(Object.values(checkClientAccount).map((acc: any) => acc.currency));
// Skip disabled accounts when checking for missing currency
const accounts = api_accounts.flat();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let detected_currency = '';
const hasMissingCurrency = accounts.some(data => {
if (!allCurrencies.has(data.currency)) {
sessionStorage.setItem('query_param_currency', data.currency);
return true;
}
detected_currency = data.currency;
return false;
});
let hasMissingToken = false;
let missingTokenCurrency = '';
for (const acc of account_list_filter) {
if (acc.loginid && !accountsList[acc.loginid]) {
hasMissingToken = true;
missingTokenCurrency = acc.currency || '';
// Store the missing token's currency in session storage
if (missingTokenCurrency) {
sessionStorage.setItem('query_param_currency', missingTokenCurrency);
}
break;
}
}
if (hasMissingCurrency || hasMissingToken) {
setClientHasCurrency(false);
} else {
const account_list_ =
account_list_filter?.find((acc: { currency: string }) => acc.currency === currency) ||
account_list_filter?.[0];
let session_storage_currency =
sessionStorage.getItem('query_param_currency') || account_list_?.currency || 'USD';
session_storage_currency = `account=${session_storage_currency}`;
setClientHasCurrency(true);
if (!new URLSearchParams(window.location.search).has('account')) {
window.history.pushState({}, '', `${window.location.pathname}?${session_storage_currency}`);
}
setClientHasCurrency(true);
}
if (subscription) {
subscription?.unsubscribe();
}
}
};
useEffect(() => {
if (isCurrencyValid && api_base.api) {
// Subscribe to the onMessage event
const is_valid_currency = currency && validCurrencies.includes(currency.toUpperCase());
if (!is_valid_currency) return;
subscription = api_base.api.onMessage().subscribe(validateApiAccounts);
}
}, []);
useEffect(() => {
// Always set the currency in session storage, even if the user is not logged in
// This ensures the currency is available on the callback page
setIsAuthenticating(true);
if (currency) {
sessionStorage.setItem('query_param_currency', currency);
}
// Authentication is now handled by the OAuth flow
setIsAuthenticating(false);
}, [isClientAccountsPopulated, isCallbackPage, clientHasCurrency, currency]);
// Add a state to track if initial authentication check is complete
const [isInitialAuthCheckComplete, setIsInitialAuthCheckComplete] = useState(false);
// Effect to mark initial auth check as complete after a short delay
useEffect(() => {
if (!isAuthenticating && !isInitialAuthCheckComplete) {
// Wait a bit to ensure all state updates have propagated
const timer = setTimeout(() => {
setIsInitialAuthCheckComplete(true);
}, 500); // Give it enough time to stabilize
return () => clearTimeout(timer);
}
}, [isAuthenticating, isInitialAuthCheckComplete]);
return (
<div
className={clsx('layout', {
responsive: isDesktop,
'quick-strategy-active': is_quick_strategy_active && !isDesktop,
})}
>
{!isCallbackPage && <AppHeader isAuthenticating={isAuthenticating || !isInitialAuthCheckComplete} />}
<Body>
<Outlet />
</Body>
{!isCallbackPage && isDesktop && <Footer />}
</div>
);
});
export default Layout;