on master help sign in
cleanup: comments and restructuring
commite2171df4b0635d2564cdfcb223694a1e8be1a9a0 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 11 Jun 2019, 09:44 (7 years ago) parent1a0f264
3 files changed+61−60
src/bridge/Bridge.js+35−60 src/bridge/lib/flags.js+1 src/bridge/lib/web3.js+25
src/bridge/Bridge.js+35−60
@@ -1,15 +1,13 @@
1+import React from "react";
12import { Just, Nothing } from "folktale/maybe";
23import { includes } from "lodash";
3−import * as azimuth from "azimuth-js";
4−import { CONTRACT_ADDRESSES } from "./lib/contracts";
5−import React from "react";
64
7−import Web3 from "web3";
85import Footer from "./components/Footer";
96import Header from "./components/Header";
107import { Container, Row, Col } from "./components/Base";
118
129import { router } from "./lib/router";
10+import { initWeb3 } from "./lib/web3";
1311import { ROUTE_NAMES } from "./lib/routeNames";
1412import { HistoryProvider, withHistory, useHistory } from "./lib/history";
1513import { TxnConfirmationsProvider } from "./store/txnConfirmations";
@@ -21,34 +19,32 @@ import {
2119 addressFromSecp256k1Public,
2220} from "./lib/wallet";
2321import { BRIDGE_ERROR } from "./lib/error";
22+import { isLocal } from "./lib/flags";
2423
25−const initWeb3 = networkType => {
26− if (networkType === NETWORK_NAMES.MAINNET) {
27− const endpoint = `https://mainnet.infura.io/v3/${
28− process.env.REACT_APP_INFURA_ENDPOINT
29− }`;
24+// NB(shrugs): toggle this shouldStubLocal variable to use the default local state.
25+// don't commit changes to this line, but there shouldn't be a problem because we'll never stub
26+// on a production build.
27+const shouldStubLocal = false;
28+const isStubbed = isLocal && shouldStubLocal;
29+const kInitialRoutes = isStubbed
30+ ? [
31+ { name: ROUTE_NAMES.SHIPS },
32+ { name: ROUTE_NAMES.WALLET },
33+ { name: ROUTE_NAMES.NETWORK },
34+ { name: ROUTE_NAMES.LANDING },
35+ ]
36+ : [{ name: ROUTE_NAMES.LANDING }];
3037
31− const provider = new Web3.providers.HttpProvider(endpoint);
32− const web3 = new Web3(provider);
33− const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.MAINNET);
34− return { web3: web3, contracts: contracts };
35− } else if (networkType === NETWORK_NAMES.LOCAL) {
36− const protocol = process.env.NODE_ENV === "development" ? "ws" : "wss";
37− const endpoint = `${protocol}://localhost:8545`;
38− const provider = new Web3.providers.WebsocketProvider(endpoint);
39− const web3 = new Web3(provider);
40− const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.DEV);
41− return { web3: web3, contracts: contracts };
42− }
43−};
44−
45−export const View = function(props) {
38+// the router itself is a component that renders a specific view based on the latest history state
39+const Router = function(props) {
4640 const history = useHistory();
4741 const Route = router(history.peek());
4842
4943 return <Route {...props} />;
5044};
5145
46+// NB(shrugs): separate component because it needs withHistory
47+// this will be must better structured as part of the UI overhaul
5248const VariableWidthColumn = withHistory(({ history, children }) => (
5349 // For the invite acceptance flow, widen the screen to use the full
5450 // container, and hide the breadcrumbs
@@ -70,10 +66,7 @@ class Bridge extends React.Component {
7066 constructor(props) {
7167 super(props);
7268
73− const networkType =
74− process.env.NODE_ENV === "development"
75− ? NETWORK_NAMES.LOCAL
76− : NETWORK_NAMES.MAINNET;
69+ const networkType = isLocal ? NETWORK_NAMES.LOCAL : NETWORK_NAMES.MAINNET;
7770
7871 // Sidestepping full network selection to allow for point viewing,
7972 // but still respecting the networkType
@@ -127,40 +120,22 @@ class Bridge extends React.Component {
127120 }
128121
129122 componentDidMount() {
130− // NB (jtobin):
123+ // NB (jtobin, shrugs):
131124 //
132125 // If running in development, the following can be tweaked to get you set
133126 // up with a desirable initial state.
134− // if (process.env.NODE_ENV === 'development') {
135− //
136− // const socket = 'ws://localhost:8545'
137− // const provider = new Web3.providers.WebsocketProvider(socket)
138− // const web3 = new Web3(provider)
139− // const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.DEV)
140− //
141− // const mnemonic = process.env.REACT_APP_DEV_MNEMONIC
142− // const hdpath = process.env.REACT_APP_HD_PATH
143− //
144− // this.setState({
145− // routeCrumbs: Stack([
146− // // ROUTE_NAMES.CREATE_GALAXY,
147− // // ROUTE_NAMES.SHIP,
148− // ROUTE_NAMES.SHIPS,
149− // // ROUTE_NAMES.MNEMONIC,
150− // ROUTE_NAMES.WALLET,
151− // ROUTE_NAMES.NETWORK,
152− // ROUTE_NAMES.LANDING
153− // ]),
154− // networkType: NETWORK_NAMES.LOCAL,
155− // pointCursor: Just(0),
156− // web3: Just(web3),
157− // contracts: Just(contracts),
158− // walletType: WALLET_NAMES.MNEMONIC,
159− // wallet: walletFromMnemonic(mnemonic, hdpath),
160− // urbitWallet: Nothing(),
161− // authMnemonic: Just('benefit crew supreme gesture quantum web media hazard theory mercy wing kitten')
162− // })
163− // }
127+ if (isStubbed) {
128+ const mnemonic = process.env.REACT_APP_DEV_MNEMONIC;
129+ const hdpath = process.env.REACT_APP_HD_PATH;
130+
131+ this.setState({
132+ pointCursor: Just(0),
133+ walletType: WALLET_NAMES.MNEMONIC,
134+ wallet: walletFromMnemonic(mnemonic, hdpath),
135+ urbitWallet: Nothing(),
136+ authMnemonic: Just(mnemonic),
137+ });
138+ }
164139 }
165140
166141 setNetworkType(symbol) {
@@ -276,7 +251,7 @@ class Bridge extends React.Component {
276251 />
277252
278253 <Row className={"row wrapper"}>
279− <View
254+ <Router
280255 // network
281256 setNetworkType={this.setNetworkType}
282257 setNetwork={this.setNetwork}
src/bridge/lib/flags.jsnew file+1
@@ -0,0 +1 @@
1+export const isLocal = process.env.NODE_ENV === "development";
src/bridge/lib/web3.jsnew file+25
@@ -0,0 +1,25 @@
1+import * as azimuth from "azimuth-js";
2+import Web3 from "web3";
3+
4+import { CONTRACT_ADDRESSES } from "./contracts";
5+import { NETWORK_NAMES } from "./network";
6+
7+export const initWeb3 = networkType => {
8+ if (networkType === NETWORK_NAMES.MAINNET) {
9+ const endpoint = `https://mainnet.infura.io/v3/${
10+ process.env.REACT_APP_INFURA_ENDPOINT
11+ }`;
12+
13+ const provider = new Web3.providers.HttpProvider(endpoint);
14+ const web3 = new Web3(provider);
15+ const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.MAINNET);
16+ return { web3: web3, contracts: contracts };
17+ } else if (networkType === NETWORK_NAMES.LOCAL) {
18+ const protocol = process.env.NODE_ENV === "development" ? "ws" : "wss";
19+ const endpoint = `${protocol}://localhost:8545`;
20+ const provider = new Web3.providers.WebsocketProvider(endpoint);
21+ const web3 = new Web3(provider);
22+ const contracts = azimuth.initContracts(web3, CONTRACT_ADDRESSES.DEV);
23+ return { web3: web3, contracts: contracts };
24+ }
25+};