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