-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathFederableApp.tsx
More file actions
140 lines (128 loc) · 4.39 KB
/
FederableApp.tsx
File metadata and controls
140 lines (128 loc) · 4.39 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
import { ErrorPage500, Loader, ToastProvider } from '@scality/core-ui';
import { useCurrentApp } from '@scality/module-federation';
import { PropsWithChildren, ReactNode, useEffect, useMemo } from 'react';
import { Provider, useDispatch } from 'react-redux';
import { applyMiddleware, compose, createStore, Store } from 'redux';
import createSagaMiddleware from 'redux-saga';
import 'regenerator-runtime/runtime';
import App from './containers/App';
import PrometheusAuthProvider from './containers/PrometheusAuthProvider';
import { authErrorAction } from './ducks/app/authError';
import { setApiConfigAction } from './ducks/config';
import { setHistory as setReduxHistory } from './ducks/history';
import reducer from './ducks/reducer';
import sagas from './ducks/sagas';
import { useTypedSelector } from './hooks';
import { AuthError } from './services/errorhandler';
import {
ShellHooksProvider,
useBasenameRelativeNavigate,
useShellHooks,
} from '@scality/module-federation';
import { FederatedAppProps } from '../@mf-types/shell/App';
const composeEnhancers =
// @ts-expect-error - FIXME when you are working on it
typeof window === 'object' && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
? // @ts-expect-error - FIXME when you are working on it
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({})
: compose;
const sagaMiddleware = createSagaMiddleware({
onError: (error) => {
if (error instanceof AuthError) {
store.dispatch(authErrorAction());
}
},
});
const enhancer = composeEnhancers(applyMiddleware(sagaMiddleware));
export const store: Store = createStore(reducer, enhancer);
// @ts-expect-error - FIXME when you are working on it
if (window.Cypress) window.__store__ = store;
sagaMiddleware.run(sagas);
const RouterWithBaseName = ({ children }: { children: ReactNode }) => {
const configStatus = useTypedSelector((state) => state.config.status);
const navigate = useBasenameRelativeNavigate();
const dispatch = useDispatch();
useMemo(() => {
dispatch(setReduxHistory(navigate));
}, [dispatch]);
if (configStatus === 'error') {
return <ErrorPage500 data-cy="sc-error-page500" />;
}
if (configStatus === 'idle' || configStatus === 'loading') {
return <>{children}</>;
}
return <>{children}</>;
};
type Config = {
url: string;
url_salt: string;
url_prometheus: string;
url_grafana: string;
url_doc: string;
url_alertmanager: string;
url_loki: string;
flags?: string[];
ui_base_path?: string;
url_support?: string;
};
export const useConfig = () => {
const { name } = useCurrentApp();
const { useConfig } = useShellHooks();
const runtimeConfiguration = useConfig({
configType: 'run',
name,
});
return runtimeConfiguration.spec.selfConfiguration;
};
function InternalAppConfigProvider({ children }) {
const dispatch = useDispatch();
const { status, api } = useTypedSelector((state) => state.config);
const config = useConfig();
useEffect(() => {
if (status === 'idle') {
dispatch(setApiConfigAction(config));
} // eslint-disable-next-line
}, [status]);
if (api && status === 'success') {
return <>{children}</>;
} else if (status === 'loading' || status === 'idle') {
return <Loader size="massive" centered={true} aria-label="loading" />; // TODO display the previous module while lazy loading the new one
} else if (status === 'error') {
return <ErrorPage500 data-cy="sc-error-page500" />;
}
}
export function AppConfigProviderWithoutRedux({ children }) {
return <>{children}</>;
}
export const AppConfigProvider = ({
children,
componentWithInjectedImports,
}: PropsWithChildren<{ componentWithInjectedImports?: ReactNode }>) => {
if (!componentWithInjectedImports) {
return <InternalAppConfigProvider>{children}</InternalAppConfigProvider>;
}
return (
//@ts-expect-error
<componentWithInjectedImports>{children}</componentWithInjectedImports>
);
};
export default function FederableApp(props: FederatedAppProps) {
return (
<ShellHooksProvider
shellHooks={props.shellHooks}
shellAlerts={props.shellAlerts}
>
<Provider store={store}>
<AppConfigProvider>
<PrometheusAuthProvider>
<ToastProvider>
<RouterWithBaseName>
<App />
</RouterWithBaseName>
</ToastProvider>
</PrometheusAuthProvider>
</AppConfigProvider>
</Provider>
</ShellHooksProvider>
);
}