on ec91572 help sign in
bridge/src/Bridge.js
98 lines 2.8 kB history raw
1import React from 'react';
2import { useEffect } from 'react';
3import { hot } from 'react-hot-loader/root';
4import { Just, Nothing } from 'folktale/maybe';
5import { IndigoApp } from 'indigo-react';
7import Router from 'components/Router';
9import Provider from 'store/Provider';
11import { ROUTE_NAMES } from 'lib/routeNames';
12import { ROUTES } from 'lib/router';
13import { NETWORK_TYPES } from 'lib/network';
14import { walletFromMnemonic } from 'lib/wallet';
15import { isDevelopment } from 'lib/flags';
16import useImpliedTicket from 'lib/useImpliedTicket';
17import useHasDisclaimed from 'lib/useHasDisclaimed';
19import 'style/index.scss';
20import WithErrorBoundary from 'components/WithErrorBoundary';
21import GlobalErrorBoundary from 'components/GlobalErrorBoundary';
23const INITIAL_NETWORK_TYPE = isDevelopment
24 ? NETWORK_TYPES.LOCAL
25 : NETWORK_TYPES.MAINNET;
27// NB(shrugs): modify these variables to change the default local state.
28const SHOULD_STUB_LOCAL = process.env.REACT_APP_STUB_LOCAL === 'true';
29const IS_STUBBED = isDevelopment && SHOULD_STUB_LOCAL;
31const INITIAL_WALLET = IS_STUBBED
32 ? walletFromMnemonic(
33 process.env.REACT_APP_DEV_MNEMONIC,
34 process.env.REACT_APP_HD_PATH
35 )
36 : undefined;
37const INITIAL_MNEMONIC = IS_STUBBED
38 ? Just(process.env.REACT_APP_DEV_MNEMONIC)
39 : Nothing();
40const INITIAL_POINT_CURSOR = IS_STUBBED ? Just(65792) : Nothing();
42function useInitialRoutes() {
43 const [hasDisclaimed] = useHasDisclaimed();
44 const hasImpliedTicket = !!useImpliedTicket();
46 const isActivateUrl = window.location.pathname === '/activate';
48 if (IS_STUBBED) {
49 return [
50 { key: ROUTE_NAMES.LOGIN },
51 { key: ROUTE_NAMES.POINTS },
52 { key: ROUTE_NAMES.POINT },
53 ];
54 }
56 if (hasImpliedTicket || isActivateUrl) {
57 return [{ key: ROUTE_NAMES.ACTIVATE }];
58 }
60 if (!hasDisclaimed) {
61 return [{ key: ROUTE_NAMES.DISCLAIMER, data: { next: ROUTE_NAMES.LOGIN } }];
62 }
64 return [{ key: ROUTE_NAMES.LOGIN }];
67function Bridge() {
68 const initialRoutes = useInitialRoutes();
70 // full reload if the user changes their selected network in Metamask
71 //
72 useEffect(() => {
73 if (window.ethereum) {
74 window.ethereum.on('chainChanged', () => {
75 document.location.reload();
76 });
77 }
78 }, []);
80 return (
81 <WithErrorBoundary render={error => <GlobalErrorBoundary error={error} />}>
82 <Provider
83 views={ROUTES}
84 names={ROUTE_NAMES}
85 initialRoutes={initialRoutes}
86 initialNetworkType={INITIAL_NETWORK_TYPE}
87 initialWallet={INITIAL_WALLET}
88 initialMnemonic={INITIAL_MNEMONIC}
89 initialPointCursor={INITIAL_POINT_CURSOR}>
90 <IndigoApp>
91 <Router />
92 </IndigoApp>
93 </Provider>
94 </WithErrorBoundary>
95 );
98export default process.env.NODE_ENV === 'development' ? hot(Bridge) : Bridge;