on 88ef6e9 help sign in
bridge/src/Bridge.js
108 lines 3.3 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 WithErrorBoundary from 'components/WithErrorBoundary';
20import GlobalErrorBoundary from 'components/GlobalErrorBoundary';
21import Modal from 'components/L2/Modal';
22import { useRollerStore } from 'store/rollerStore';
23import { Box } from '@tlon/indigo-react';
24import LoadingOverlay from 'components/L2/LoadingOverlay';
26const INITIAL_NETWORK_TYPE = isRopsten
27 ? NETWORK_TYPES.ROPSTEN
28 : isDevelopment && !isMainnet
29 ? NETWORK_TYPES.LOCAL
30 : NETWORK_TYPES.MAINNET;
32// NB(shrugs): modify these variables to change the default local state.
33const SHOULD_STUB_LOCAL = process.env.REACT_APP_STUB_LOCAL === 'true';
34const IS_STUBBED = isDevelopment && SHOULD_STUB_LOCAL;
36const INITIAL_WALLET = IS_STUBBED
37 ? walletFromMnemonic(
38 process.env.REACT_APP_DEV_MNEMONIC,
39 process.env.REACT_APP_HD_PATH
40 )
41 : undefined;
42const INITIAL_MNEMONIC = IS_STUBBED
43 ? Just(process.env.REACT_APP_DEV_MNEMONIC)
44 : Nothing();
45const INITIAL_POINT_CURSOR = IS_STUBBED ? Just(65792) : Nothing();
47function useInitialRoutes() {
48 const [hasDisclaimed] = useHasDisclaimed();
49 const { impliedTicket } = useImpliedTicket();
51 const isActivateUrl = window.location.pathname === '/activate';
53 if (IS_STUBBED) {
54 return [
55 { key: ROUTE_NAMES.LOGIN },
56 // { key: ROUTE_NAMES.POINTS },
57 // { key: ROUTE_NAMES.POINT },
58 ];
59 }
61 if (impliedTicket || isActivateUrl) {
62 return [{ key: ROUTE_NAMES.ACTIVATE }];
63 }
65 if (!hasDisclaimed) {
66 return [{ key: ROUTE_NAMES.DISCLAIMER, data: { next: ROUTE_NAMES.LOGIN } }];
67 }
69 return [{ key: ROUTE_NAMES.LOGIN }];
72function Bridge() {
73 const initialRoutes = useInitialRoutes();
74 const { loading, modalText, setModalText } = useRollerStore();
76 // full reload if the user changes their selected network in Metamask
77 //
78 useEffect(() => {
79 if (window.ethereum) {
80 window.ethereum.on('chainChanged', () => {
81 document.location.reload();
82 });
83 }
84 }, []);
86 return (
87 <WithErrorBoundary render={error => <GlobalErrorBoundary error={error} />}>
88 <Provider
89 views={ROUTES}
90 names={ROUTE_NAMES}
91 initialRoutes={initialRoutes}
92 initialNetworkType={INITIAL_NETWORK_TYPE}
93 initialWallet={INITIAL_WALLET}
94 initialMnemonic={INITIAL_MNEMONIC}
95 initialPointCursor={INITIAL_POINT_CURSOR}>
96 <IndigoApp>
97 <Router />
98 </IndigoApp>
99 <Modal show={modalText !== undefined} hide={() => setModalText()}>
100 <Box width="280px">{modalText}</Box>
101 </Modal>
102 <LoadingOverlay loading={loading} />
103 </Provider>
104 </WithErrorBoundary>
105 );
108export default Bridge;