2024-01-25 16:51:40 -08:00
|
|
|
import React from 'react';
|
2021-03-11 13:38:35 -08:00
|
|
|
import Cookies from 'js-cookie';
|
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';
|
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-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';
|
2024-01-19 13:38:48 -05:00
|
|
|
import { ErrorCodes } from './app/shared/constants';
|
2024-01-25 16:51:40 -08:00
|
|
|
import CustomThemeProvider from './CustomThemeProvider';
|
|
|
|
import { useCustomTheme } from './customThemeContext';
|
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) => {
|
2024-08-05 09:00:19 -07:00
|
|
|
const { networkError } = error;
|
2021-03-11 13:38:35 -08:00
|
|
|
if (networkError) {
|
|
|
|
const serverError = networkError as ServerError;
|
2024-01-19 13:38:48 -05:00
|
|
|
if (serverError.statusCode === ErrorCodes.Unauthorized) {
|
2021-03-11 13:38:35 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2024-08-05 09:00:19 -07:00
|
|
|
// Disabled behavior for now -> Components are expected to handle their errors.
|
|
|
|
// 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); // TODO: Decide if we want this back.
|
|
|
|
// }
|
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 };
|
2024-02-14 09:06:21 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
entity: {
|
|
|
|
merge: (oldObj, newObj) => {
|
|
|
|
return { ...oldObj, ...newObj };
|
2023-03-21 18:36:21 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
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
|
|
|
});
|
|
|
|
|
2024-01-03 17:16:16 -05:00
|
|
|
export const InnerApp: React.VFC = () => {
|
2021-01-17 12:54:49 -08:00
|
|
|
return (
|
2023-05-09 15:16:11 -07:00
|
|
|
<HelmetProvider>
|
2024-01-25 16:51:40 -08:00
|
|
|
<CustomThemeProvider>
|
|
|
|
<Helmet>
|
2024-12-12 20:49:25 +05:30
|
|
|
<title>{useCustomTheme().theme?.content?.title}</title>
|
2024-01-25 16:51:40 -08:00
|
|
|
</Helmet>
|
2023-05-09 15:16:11 -07:00
|
|
|
<Router>
|
2024-01-03 17:16:16 -05:00
|
|
|
<Routes />
|
2023-05-09 15:16:11 -07:00
|
|
|
</Router>
|
2024-01-25 16:51:40 -08:00
|
|
|
</CustomThemeProvider>
|
2023-05-09 15:16:11 -07:00
|
|
|
</HelmetProvider>
|
2021-01-17 12:54:49 -08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-01-03 17:16:16 -05:00
|
|
|
export const App: React.VFC = () => {
|
|
|
|
return (
|
|
|
|
<ApolloProvider client={client}>
|
|
|
|
<InnerApp />
|
|
|
|
</ApolloProvider>
|
|
|
|
);
|
|
|
|
};
|