1import React, { useCallback, useMemo, 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';
33import FormError from 'form/FormError';
34import { FORM_ERROR } from 'final-form';
36export default function ActivateCode() {
37 const history = useHistory();
38 const { names, push } = useLocalRouter();
39 const { contracts } = useNetwork();
40 const impliedTicket = useImpliedTicket();
41 const [hasDisclaimed] = useHasDisclaimed();
43 const cachedInviteWallet = useRef(Nothing());
44 const cachedPoint = useRef();
51 } = useActivateFlow();
53 const goToLogin = useCallback(() => history.popAndPush(ROUTE_NAMES.LOGIN), [
56 const goToPassport = useCallback(() => {
60 push(names.DISCLAIMER);
62 }, [names, push, hasDisclaimed]);
64 // validate should be a pure function but we don't want to have to recompute
65 // all of this information on submit, so cache the invite wallet and avoid
66 // re-renders that may trigger re-validations (causing infinite loop)
67 const validateForm = useCallback(
68 async ({ ticket }, errors) => {
69 if (hasErrors(errors)) {
73 console.log('FUCK — deriving seed');
75 await timeout(100); // allow the ui changes to flush before we lag it out
77 const _contracts = need.contracts(contracts);
78 const { seed } = await generateTemporaryOwnershipWallet(ticket);
80 // TODO(fang): isn't all this accessible in the ownership object?
81 const inviteWallet = walletFromMnemonic(seed, DEFAULT_HD_PATH);
82 cachedInviteWallet.current = inviteWallet;
84 const _inviteWallet = need.wallet(inviteWallet);
86 const owned = await azimuth.azimuth.getOwnedPoints(
90 const transferring = await azimuth.azimuth.getTransferringFor(
94 const incoming = [...owned, ...transferring];
96 if (incoming.length > 0) {
97 if (incoming.length > 1) {
100 // 'This invite code has multiple points available.\n' +
101 // "Once you've activated this point, activate the next with the same process."
105 const point = parseInt(incoming[0], 10);
106 setDerivedPoint(Just(point));
107 cachedPoint.current = point;
111 'Invite code has no claimable point.\n' +
112 'Check your invite code and try again?',
116 [contracts, setDerivedPoint]
119 const validate = useMemo(
120 () => composeValidator({ ticket: buildTicketValidator() }, validateForm),
124 // set our state on submission
125 const onSubmit = useCallback(
127 setInviteWallet(cachedInviteWallet.current);
128 setDerivedWallet(Just(await generateWallet(cachedPoint)));
130 [setDerivedWallet, setInviteWallet]
133 const afterSubmit = useCallback(async () => goToPassport(), [goToPassport]);
138 <Grid.Item full as={Passport} point={derivedPoint} />
139 <Grid.Item full as={H4} className="mt3 mb2">
145 afterSubmit={afterSubmit}
146 initialValues={{ ticket: impliedTicket || '' }}>
147 {({ validating, submitting, handleSubmit }) => (
153 label="Activation Code"
156 <Grid.Item full as={FormError} />
162 handleSubmit={handleSubmit}>
173 <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