2023-12-07 10:13:09 -08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2021-03-11 13:38:35 -08:00
|
|
|
import Cookies from 'js-cookie';
|
2021-09-02 19:05:13 -07:00
|
|
|
import { message } from 'antd';
|
2021-01-17 12:54:49 -08:00
|
|
|
import { BrowserRouter as Router } from 'react-router-dom';
|
2021-03-11 13:38:35 -08:00
|
|
|
import { ApolloClient, ApolloProvider, createHttpLink, InMemoryCache, ServerError } from '@apollo/client';
|
|
|
|
import { onError } from '@apollo/client/link/error';
|
2021-03-09 23:14:52 -08:00
|
|
|
import { ThemeProvider } from 'styled-components';
|
2023-05-09 15:16:11 -07:00
|
|
|
import { Helmet, HelmetProvider } from 'react-helmet-async';
|
2021-03-09 23:14:52 -08:00
|
|
|
import './App.less';
|
2021-02-12 13:57:38 -08:00
|
|
|
import { Routes } from './app/Routes';
|
2021-03-09 23:14:52 -08:00
|
|
|
import { Theme } from './conf/theme/types';
|
|
|
|
import defaultThemeConfig from './conf/theme/theme_light.config.json';
|
2021-03-11 13:38:35 -08:00
|
|
|
import { PageRoutes } from './conf/Global';
|
|
|
|
import { isLoggedInVar } from './app/auth/checkAuthStatus';
|
|
|
|
import { GlobalCfg } from './conf';
|
2023-11-06 21:34:17 -05:00
|
|
|
import possibleTypesResult from './possibleTypes.generated';
|
2021-01-17 12:54:49 -08:00
|
|
|
|
|
|
|
/*
|
2021-07-17 04:56:50 +10:00
|
|
|
Construct Apollo Client
|
2021-01-17 12:54:49 -08:00
|
|
|
*/
|
2021-03-11 13:38:35 -08:00
|
|
|
const httpLink = createHttpLink({ uri: '/api/v2/graphql' });
|
|
|
|
|
2022-08-28 20:08:25 -07:00
|
|
|
const errorLink = onError((error) => {
|
|
|
|
const { networkError, graphQLErrors } = error;
|
2021-03-11 13:38:35 -08:00
|
|
|
if (networkError) {
|
|
|
|
const serverError = networkError as ServerError;
|
|
|
|
if (serverError.statusCode === 401) {
|
|
|
|
isLoggedInVar(false);
|
|
|
|
Cookies.remove(GlobalCfg.CLIENT_AUTH_COOKIE);
|
2023-07-31 09:57:49 +05:30
|
|
|
const currentPath = window.location.pathname + window.location.search;
|
|
|
|
window.location.replace(`${PageRoutes.AUTHENTICATE}?redirect_uri=${encodeURIComponent(currentPath)}`);
|
2021-03-11 13:38:35 -08:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 19:05:13 -07:00
|
|
|
if (graphQLErrors && graphQLErrors.length) {
|
|
|
|
const firstError = graphQLErrors[0];
|
|
|
|
const { extensions } = firstError;
|
|
|
|
const errorCode = extensions && (extensions.code as number);
|
|
|
|
// Fallback in case the calling component does not handle.
|
|
|
|
message.error(`${firstError.message} (code ${errorCode})`, 3);
|
|
|
|
}
|
2021-03-11 13:38:35 -08:00
|
|
|
});
|
|
|
|
|
2021-01-17 12:54:49 -08:00
|
|
|
const client = new ApolloClient({
|
2021-07-30 17:41:03 -07:00
|
|
|
connectToDevTools: true,
|
2021-03-11 13:38:35 -08:00
|
|
|
link: errorLink.concat(httpLink),
|
2023-03-21 18:36:21 -07:00
|
|
|
cache: new InMemoryCache({
|
|
|
|
typePolicies: {
|
|
|
|
Query: {
|
|
|
|
fields: {
|
|
|
|
dataset: {
|
|
|
|
merge: (oldObj, newObj) => {
|
|
|
|
return { ...oldObj, ...newObj };
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-06 21:34:17 -05:00
|
|
|
// need to define possibleTypes to allow us to use Apollo cache with union types
|
|
|
|
possibleTypes: possibleTypesResult.possibleTypes,
|
2023-03-21 18:36:21 -07:00
|
|
|
}),
|
2021-01-17 12:54:49 -08:00
|
|
|
credentials: 'include',
|
2021-07-08 03:54:16 +08:00
|
|
|
defaultOptions: {
|
|
|
|
watchQuery: {
|
2021-08-02 17:49:57 -07:00
|
|
|
fetchPolicy: 'no-cache',
|
2021-07-08 03:54:16 +08:00
|
|
|
},
|
|
|
|
query: {
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
},
|
|
|
|
},
|
2021-01-17 12:54:49 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
const App: React.VFC = () => {
|
2021-03-11 10:06:48 -08:00
|
|
|
const [dynamicThemeConfig, setDynamicThemeConfig] = useState<Theme>(defaultThemeConfig);
|
2021-03-09 23:14:52 -08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
import(`./conf/theme/${process.env.REACT_APP_THEME_CONFIG}`).then((theme) => {
|
|
|
|
setDynamicThemeConfig(theme);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2021-01-17 12:54:49 -08:00
|
|
|
return (
|
2023-05-09 15:16:11 -07:00
|
|
|
<HelmetProvider>
|
|
|
|
<ThemeProvider theme={dynamicThemeConfig}>
|
|
|
|
<Router>
|
|
|
|
<Helmet>
|
|
|
|
<title>{dynamicThemeConfig.content.title}</title>
|
|
|
|
</Helmet>
|
2023-12-07 10:13:09 -08:00
|
|
|
<ApolloProvider client={client}>
|
|
|
|
<Routes />
|
|
|
|
</ApolloProvider>
|
2023-05-09 15:16:11 -07:00
|
|
|
</Router>
|
|
|
|
</ThemeProvider>
|
|
|
|
</HelmetProvider>
|
2021-01-17 12:54:49 -08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|