forked from thebull-finance/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.js
105 lines (94 loc) · 3.24 KB
/
gatsby-browser.js
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
import React from "react";
import { Provider } from "react-redux";
import { Helmet } from "react-helmet";
import { PersistGate } from "redux-persist/integration/react";
import { store, persistor } from "./src/store/store.js";
import { navigateLogin } from "./src/services/navigation";
import "./src/styles/styles.scss";
import { verifySessionData } from "./src/utils/auth";
import { navigate } from "gatsby";
import "whatwg-fetch";
import tradeApi from "services/tradeApiClient";
// import { createHistory } from "@reach/router";
// const history = typeof window !== "undefined" ? createHistory(window) : null;
// if (window.Cypress || process.env.MIRAGE === "true") {
// import("utils/mirage/server").then((server) => {
// server.setup();
// });
// }
// Handle Mirage server
export const wrapRootElement = ({ element }) => {
// Add notranslate meta to prevent Chrome translation tool mutating React virtual DOM.
// See: https://github.com/facebook/react/issues/11538
return (
<Provider store={store}>
<PersistGate persistor={persistor}>
<Helmet>
<meta content="notranslate" name="google" />
</Helmet>
{element}
</PersistGate>
</Provider>
);
};
/**
* Cleanup process to unregister any SW installed by legacy webapp1.
*
* @returns {Void} None.
*/
const serviceWorkerCleanup = () => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.getRegistrations().then((registrations) => {
registrations.forEach((registration) => {
registration.unregister();
});
if (Array.isArray(registrations) && registrations.length > 0) {
location.reload();
}
});
}
};
export const onClientEntry = () => {
serviceWorkerCleanup();
if (process.env.NODE_ENV === "development") {
// const whyDidYouRender = require("@welldone-software/why-did-you-render");
// whyDidYouRender(React, {
// trackAllPureComponents: true,
// trackExtraHooks: [[require("react-redux/lib"), "useSelector"]],
// });
}
};
export const onInitialClientRender = () => {
const state = store.getState();
// @ts-ignore
const token = state.session.tradeApi.accessToken;
// Init api auth token with persisted value
tradeApi.setToken(token);
// @ts-ignore
const sessionData = state.session.sessionData;
let path = "";
if (typeof window !== "undefined") {
path = window.location.pathname;
}
// Verify session when app loads.
const sessionValid = verifySessionData(token, sessionData);
// Root page handles the correct redirection manually.
if (path !== "/") {
const isLoginArea = path.match(/login|signup/g);
const isRecoverArea = path.match(/recover|disable2fa|changeEmail|deleteAccount/g);
const isPrivateArea = !isLoginArea && !isRecoverArea;
if (sessionValid) {
if (isLoginArea) {
// Redirect to dashboard when navigating login pages with an active session.
navigate("/dashboard");
}
} else if (isPrivateArea) {
// Redirect to login when navigating private pages with expired session.
navigateLogin();
}
}
// TODO: Disable due to weird behaviors caused in login page render and blank page on other pages reload.
// setInterval(() => {
// store.dispatch(showLoader(false));
// }, 500);
};