1import React, { useCallback, useRef } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import * as azimuth from 'azimuth-js';
4import { Grid, H4 } from 'indigo-react';
6import View from 'components/View';
7import { ForwardButton } from 'components/Buttons';
8import Passport from 'components/Passport';
10import { useHistory } from 'store/history';
12import * as need from 'lib/need';
13import { ROUTE_NAMES } from 'lib/routeNames';
14import FooterButton from 'components/FooterButton';
15import { DEFAULT_HD_PATH, walletFromMnemonic } from 'lib/wallet';
16import { useNetwork } from 'store/network';
17import { generateWallet } from 'lib/invite';
18import { generateTemporaryOwnershipWallet } from 'lib/walletgen';
19import { useActivateFlow } from './ActivateFlow';
20import { useLocalRouter } from 'lib/LocalRouter';
21import useImpliedTicket from 'lib/useImpliedTicket';
22import timeout from 'lib/timeout';
23import useHasDisclaimed from 'lib/useHasDisclaimed';
25import BridgeForm from 'form/BridgeForm';
26import SubmitButton from 'form/SubmitButton';
27import { TicketInput } from 'form/Inputs';
29export default function ActivateCode() {
30 const history = useHistory();
31 const { names, push } = useLocalRouter();
32 const { contracts } = useNetwork();
33 const impliedTicket = useImpliedTicket();
34 const [hasDisclaimed] = useHasDisclaimed();
36 const cachedInviteWallet = useRef(Nothing());
37 const cachedPoint = useRef();
44 } = useActivateFlow();
46 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
49 const goToPassport = useCallback(() => {
53 push(names.DISCLAIMER);
55 }, [names, push, hasDisclaimed]);
57 // validate should be a pure function but we don't want to have to recompute
58 // all of this information on submit, so cache the invite wallet and avoid
59 // re-renders that may trigger re-validations (causing infinite loop)
60 const validate = useCallback(
62 await timeout(100); // allow the ui changes to flush before we lag it out
64 const _contracts = need.contracts(contracts);
65 const { seed } = await generateTemporaryOwnershipWallet(ticket);
67 // TODO(fang): isn't all this accessible in the ownership object?
68 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
69 cachedInviteWallet.current = inviteWallet;
71 const _inviteWallet = need.wallet(inviteWallet);
73 const owned = await azimuth.azimuth.getOwnedPoints(
77 const transferring = await azimuth.azimuth.getTransferringFor(
81 const incoming = [...owned, ...transferring];
83 if (incoming.length > 0) {
84 if (incoming.length > 1) {
87 // 'This invite code has multiple points available.\n' +
88 // "Once you've activated this point, activate the next with the same process."
92 const point = parseInt(incoming[0], 10);
93 setDerivedPoint(Just(point));
94 cachedPoint.current = point;
97 'Invite code has no claimable point.\n' +
98 'Check your invite code and try again?'
102 [contracts, setDerivedPoint]
105 // set our state on submission
106 const onSubmit = useCallback(
108 setInviteWallet(cachedInviteWallet.current);
109 setDerivedWallet(Just(await generateWallet(cachedPoint)));
113 [goToPassport, setDerivedWallet, setInviteWallet]
119 <Grid.Item full as={Passport} point={derivedPoint} />
120 <Grid.Item full as={H4} className="mt3 mb2">
125 initialValues={{ ticket: impliedTicket || '' }}>
126 {({ validating, submitting, handleSubmit }) => (
132 label="Activation Code"
141 handleSubmit={handleSubmit}>
152 <FooterButton as={ForwardButton} onClick={goToLogin}>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list