on 4bc52fa help sign in
bridge/src/Bridge.tsx
121 lines 3.6 kB history raw
1import { useEffect } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import { IndigoApp } from 'indigo-react';
5import Router from 'components/Router';
7import Provider from 'store/Provider';
9import { ROUTE_NAMES } from 'lib/routeNames';
10import { ROUTES } from 'lib/router';
11import { NETWORK_TYPES } from 'lib/network';
12import { walletFromMnemonic } from 'lib/wallet';
13import { isDevelopment, isMainnet, isGoerli } from 'lib/flags';
14import useImpliedTicket from 'lib/useImpliedTicket';
15import useHasDisclaimed from 'lib/useHasDisclaimed';
17import 'style/index.scss';
18import { ThemeProvider } from 'styled-components';
19import light from '@tlon/indigo-light';
21import WithErrorBoundary from 'components/WithErrorBoundary';
22import GlobalErrorBoundary from 'components/GlobalErrorBoundary';
23import Modal from 'components/L2/Modal';
24import { useRollerStore } from 'store/rollerStore';
25import { Box } from '@tlon/indigo-react';
26import LoadingOverlay from 'components/L2/LoadingOverlay';
27import { useRollerPoller } from 'lib/useRollerPoller';
29const INITIAL_NETWORK_TYPE = isGoerli
30 ? NETWORK_TYPES.GOERLI
31 : isDevelopment && !isMainnet
32 ? NETWORK_TYPES.LOCAL
33 : NETWORK_TYPES.MAINNET;
35// NB(shrugs): modify these variables to change the default local state.
36const SHOULD_STUB_LOCAL = import.meta.env.VITE_STUB_LOCAL === 'true';
37const IS_STUBBED = isDevelopment && SHOULD_STUB_LOCAL;
39const INITIAL_WALLET = IS_STUBBED
40 ? walletFromMnemonic(
41 import.meta.env.VITE_DEV_MNEMONIC as string,
42 import.meta.env.VITE_HD_PATH as string
43 )
44 : undefined;
45const INITIAL_MNEMONIC = IS_STUBBED
46 ? Just(import.meta.env.VITE_DEV_MNEMONIC)
47 : Nothing();
48const INITIAL_POINT_CURSOR = IS_STUBBED ? Just(65792) : Nothing();
50function useInitialRoutes() {
51 const [hasDisclaimed] = useHasDisclaimed();
52 const { impliedTicket } = useImpliedTicket();
54 const isActivateUrl = window.location.pathname === '/activate';
56 if (IS_STUBBED) {
57 return [
58 { key: ROUTE_NAMES.LOGIN },
59 // { key: ROUTE_NAMES.POINTS },
60 // { key: ROUTE_NAMES.POINT },
61 ];
62 }
64 if (impliedTicket || isActivateUrl) {
65 return [{ key: ROUTE_NAMES.ACTIVATE }];
66 }
68 if (!hasDisclaimed) {
69 return [{ key: ROUTE_NAMES.DISCLAIMER, data: { next: ROUTE_NAMES.LOGIN } }];
70 }
72 return [{ key: ROUTE_NAMES.LOGIN }];
75function Poller() {
76 useRollerPoller();
78 return null;
81function Bridge() {
82 const initialRoutes = useInitialRoutes();
83 const { loading, modalText, setModalText } = useRollerStore();
85 // full reload if the user changes their selected network in Metamask
86 //
87 useEffect(() => {
88 if (window.ethereum) {
89 window.ethereum.on('chainChanged', () => {
90 document.location.reload();
91 });
92 }
93 }, []);
95 return (
96 <WithErrorBoundary
97 render={(error: 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;