-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathapp-content.jsx
More file actions
184 lines (162 loc) · 6.95 KB
/
Copy pathapp-content.jsx
File metadata and controls
184 lines (162 loc) · 6.95 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import React, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { ToastContainer } from 'react-toastify';
import AuthLoadingWrapper from '@/components/auth-loading-wrapper';
import useLiveChat from '@/components/chat/useLiveChat';
import ChunkLoader from '@/components/loader/chunk-loader';
import { getUrlBase } from '@/components/shared';
import TransactionDetailsModal from '@/components/transaction-details';
import { api_base, ApiHelpers, ServerTime } from '@/external/bot-skeleton';
import { CONNECTION_STATUS } from '@/external/bot-skeleton/services/api/observables/connection-status-stream';
import { useApiBase } from '@/hooks/useApiBase';
import useDevMode from '@/hooks/useDevMode';
import { useStore } from '@/hooks/useStore';
import useThemeSwitcher from '@/hooks/useThemeSwitcher';
import { ThemeProvider } from '@deriv-com/quill-ui';
import { setSmartChartsPublicPath } from '@deriv-com/smartcharts-champion';
import { localize } from '@deriv-com/translations';
import Audio from '../components/audio';
import BlocklyLoading from '../components/blockly-loading';
import BotStopped from '../components/bot-stopped';
import BotBuilder from '../pages/bot-builder';
import Main from '../pages/main';
import './app.scss';
import 'react-toastify/dist/ReactToastify.css';
import '../components/bot-notification/bot-notification.scss';
const AppContent = observer(() => {
const [is_api_initialized, setIsApiInitialized] = React.useState(false);
const [is_loading, setIsLoading] = React.useState(true);
const store = useStore();
const { app, transactions, common, client } = store;
const { is_dark_mode_on } = useThemeSwitcher();
const { recovered_transactions, recoverPendingContracts } = transactions;
const is_subscribed_to_msg_listener = React.useRef(false);
const msg_listener = React.useRef(null);
const { connectionStatus } = useApiBase();
// Initialize dev mode keyboard shortcuts
useDevMode();
const livechat_client_information = {
is_client_store_initialized: client?.is_logged_in ? true : !!client,
is_logged_in: client?.is_logged_in,
loginid: client?.loginid,
currency: client?.currency,
residence: client?.residence,
email: '',
first_name: '',
last_name: '',
};
useLiveChat(livechat_client_information);
// NOTE: Disabled Intercom until further notice
// const token = V2GetActiveToken() ?? null;
// useIntercom(token);
useEffect(() => {
if (connectionStatus === CONNECTION_STATUS.OPENED) {
setIsApiInitialized(true);
common.setSocketOpened(true);
} else if (connectionStatus !== CONNECTION_STATUS.OPENED) {
common.setSocketOpened(false);
}
}, [common, connectionStatus]);
const { current_language } = common;
const html = document.documentElement;
React.useEffect(() => {
html?.setAttribute('lang', current_language.toLowerCase());
html?.setAttribute('dir', current_language.toLowerCase() === 'ar' ? 'rtl' : 'ltr');
}, [current_language, html]);
const handleMessage = React.useCallback(
({ data }) => {
if (data?.msg_type === 'proposal_open_contract' && !data?.error) {
const { proposal_open_contract } = data;
if (
proposal_open_contract?.status !== 'open' &&
!recovered_transactions?.includes(proposal_open_contract?.contract_id)
) {
recoverPendingContracts(proposal_open_contract);
}
}
},
[recovered_transactions, recoverPendingContracts]
);
React.useEffect(() => {
setSmartChartsPublicPath(getUrlBase('/js/smartcharts/'));
}, []);
React.useEffect(() => {
// Check if api is initialized and then subscribe to the api messages
// Also we should only subscribe to the messages once user is logged in
// And is not already subscribed to the messages
if (!is_subscribed_to_msg_listener.current && client.is_logged_in && is_api_initialized && api_base?.api) {
is_subscribed_to_msg_listener.current = true;
msg_listener.current = api_base.api.onMessage()?.subscribe(handleMessage);
}
return () => {
if (is_subscribed_to_msg_listener.current && msg_listener.current) {
is_subscribed_to_msg_listener.current = false;
msg_listener.current.unsubscribe?.();
}
};
}, [is_api_initialized, client.is_logged_in, client.loginid, handleMessage, connectionStatus]);
const init = () => {
ServerTime.init(common);
app.setDBotEngineStores();
ApiHelpers.setInstance(app.api_helpers_store);
import('@/utils/gtm').then(({ default: GTM }) => {
GTM.init(store);
});
};
const changeActiveSymbolLoadingState = () => {
init();
const retrieveActiveSymbols = () => {
const { active_symbols } = ApiHelpers.instance;
active_symbols.retrieveActiveSymbols(true).then(() => {
setIsLoading(false);
});
};
if (ApiHelpers?.instance?.active_symbols) {
retrieveActiveSymbols();
} else {
// This is a workaround to fix the issue where the active symbols are not loaded immediately
// when the API is initialized. Should be replaced with RxJS pubsub
const intervalId = setInterval(() => {
if (ApiHelpers?.instance?.active_symbols) {
clearInterval(intervalId);
retrieveActiveSymbols();
}
}, 1000);
}
};
React.useEffect(() => {
if (is_api_initialized) {
init();
setIsLoading(true);
if (!client.is_logged_in) {
changeActiveSymbolLoadingState();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [is_api_initialized]);
React.useEffect(() => {
if (client.is_logged_in && is_api_initialized) {
changeActiveSymbolLoadingState();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [is_api_initialized, client.loginid]);
if (common?.error) return null;
return is_loading ? (
<ChunkLoader message={localize('Initializing Deriv Bot account...')} />
) : (
<AuthLoadingWrapper>
<ThemeProvider theme={is_dark_mode_on ? 'dark' : 'light'}>
<BlocklyLoading />
<div className='bot-dashboard bot' data-testid='dt_bot_dashboard'>
<Audio />
<Main />
<BotBuilder />
<BotStopped />
<TransactionDetailsModal />
<ToastContainer limit={3} draggable={false} />
</div>
</ThemeProvider>
</AuthLoadingWrapper>
);
});
export default AppContent;