on 09c909e help sign in
bridge/src/Bridge.js
121 lines 3.6 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, isMainnet, isRopsten } from 'lib/flags';
15import useImpliedTicket from 'lib/useImpliedTicket';
16import useHasDisclaimed from 'lib/useHasDisclaimed';
18import 'style/index.scss';
19import { ThemeProvider } from 'styled-components';
20import light from '@tlon/indigo-light';
22import WithErrorBoundary from 'components/WithErrorBoundary';
23import GlobalErrorBoundary from 'components/GlobalErrorBoundary';
24import Modal from 'components/L2/Modal';
25import { useRollerStore } from 'store/rollerStore';
26import { Box } from '@tlon/indigo-react';
27import LoadingOverlay from 'components/L2/LoadingOverlay';
28import { useRollerPoller } from 'lib/useRollerPoller';
30const INITIAL_NETWORK_TYPE = isRopsten
31 ? NETWORK_TYPES.ROPSTEN
32 : isDevelopment && !isMainnet
33 ? NETWORK_TYPES.LOCAL
34 : NETWORK_TYPES.MAINNET;
36// NB(shrugs): modify these variables to change the default local state.
37const SHOULD_STUB_LOCAL = process.env.REACT_APP_STUB_LOCAL === 'true';
38const IS_STUBBED = isDevelopment && SHOULD_STUB_LOCAL;
40const INITIAL_WALLET = IS_STUBBED
41 ? walletFromMnemonic(
42 process.env.REACT_APP_DEV_MNEMONIC,
43 process.env.REACT_APP_HD_PATH
44 )
45 : undefined;
46const INITIAL_MNEMONIC = IS_STUBBED
47 ? Just(process.env.REACT_APP_DEV_MNEMONIC)
48 : Nothing();
49const INITIAL_POINT_CURSOR = IS_STUBBED ? Just(65792) : Nothing();
51function useInitialRoutes() {
52 const [hasDisclaimed] = useHasDisclaimed();
53 const { impliedTicket } = useImpliedTicket();
55 const isActivateUrl = window.location.pathname === '/activate';
57 if (IS_STUBBED) {
58 return [
59 { key: ROUTE_NAMES.LOGIN },
60 // { key: ROUTE_NAMES.POINTS },
61 // { key: ROUTE_NAMES.POINT },
62 ];
63 }
65 if (impliedTicket || isActivateUrl) {
66 return [{ key: ROUTE_NAMES.ACTIVATE }];
67 }
69 if (!hasDisclaimed) {
70 return [{ key: ROUTE_NAMES.DISCLAIMER, data: { next: ROUTE_NAMES.LOGIN } }];
71 }
73 return [{ key: ROUTE_NAMES.LOGIN }];
76function Poller() {
77 useRollerPoller();
79 return null;
82function Bridge() {
83 const initialRoutes = useInitialRoutes();
84 const { loading, modalText, setModalText } = useRollerStore();
86 // full reload if the user changes their selected network in Metamask
87 //
88 useEffect(() => {
89 if (window.ethereum) {
90 window.ethereum.on('chainChanged', () => {
91 document.location.reload();
92 });
93 }
94 }, []);
96 return (
97 <WithErrorBoundary render={error => <GlobalErrorBoundary error={error} />}>
98 <Provider
99 views={ROUTES}
100 names={ROUTE_NAMES}
101 initialRoutes={initialRoutes}
102 initialNetworkType={INITIAL_NETWORK_TYPE}
103 initialWallet={INITIAL_WALLET}
104 initialMnemonic={INITIAL_MNEMONIC}
105 initialPointCursor={INITIAL_POINT_CURSOR}>
106 <IndigoApp>
107 <ThemeProvider theme={light}>
108 <Router />
109 </ThemeProvider>
110 </IndigoApp>
111 <Modal show={modalText !== undefined} hide={() => setModalText()}>
112 <Box width="280px">{modalText}</Box>
113 </Modal>
114 <LoadingOverlay loading={loading} />
115 <Poller />
116 </Provider>
117 </WithErrorBoundary>
118 );
121export default Bridge;