-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathapp-root.tsx
More file actions
80 lines (68 loc) · 2.58 KB
/
Copy pathapp-root.tsx
File metadata and controls
80 lines (68 loc) · 2.58 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
import { lazy, Suspense, useEffect, useRef, useState } from 'react';
import { observer } from 'mobx-react-lite';
import ErrorBoundary from '@/components/error-component/error-boundary';
import ErrorComponent from '@/components/error-component/error-component';
import ChunkLoader from '@/components/loader/chunk-loader';
import { api_base } from '@/external/bot-skeleton';
import { useStore } from '@/hooks/useStore';
import { localize } from '@deriv-com/translations';
import './app-root.scss';
const AppContent = lazy(() => import('./app-content'));
const AppRootLoader = () => {
return <ChunkLoader message={localize('Loading...')} />;
};
const ErrorComponentWrapper = observer(() => {
const { common } = useStore();
if (!common.error) return null;
return (
<ErrorComponent
header={common.error?.header}
message={common.error?.message}
redirect_label={common.error?.redirect_label}
redirectOnClick={common.error?.redirectOnClick}
should_clear_error_on_click={common.error?.should_clear_error_on_click}
setError={common.setError}
redirect_to={common.error?.redirect_to}
should_redirect={common.error?.should_redirect}
/>
);
});
const AppRoot = () => {
const store = useStore();
const api_base_initialized = useRef(false);
const [is_api_initialized, setIsApiInitialized] = useState(false);
// Initialize API
useEffect(() => {
const timeoutId = setTimeout(() => {
if (!is_api_initialized) {
setIsApiInitialized(true);
}
}, 5000);
const initializeApi = async () => {
if (!api_base_initialized.current) {
try {
await api_base.init();
api_base_initialized.current = true;
} catch (error) {
console.error('API initialization failed:', error);
api_base_initialized.current = false;
} finally {
setIsApiInitialized(true);
clearTimeout(timeoutId); // Clear timeout if API init completes
}
}
};
initializeApi();
return () => clearTimeout(timeoutId);
}, []);
if (!store || !is_api_initialized) return <AppRootLoader />;
return (
<Suspense fallback={<AppRootLoader />}>
<ErrorBoundary root_store={store}>
<ErrorComponentWrapper />
<AppContent />
</ErrorBoundary>
</Suspense>
);
};
export default AppRoot;