on 984250e help sign in
bridge/src/lib/flowCommand.js
64 lines 1.7 kB history raw
1import { useMemo } from 'react';
2import { isValidAddress } from './wallet';
4// looks into the GET parameters and attempts to construct a "flow" object,
5// which would indicate bridge is to be used for one specific flow.
6// expects to find any of the following sets of parameters, varying by kind.
7//
8// ? kind = invite
9// & as = ~ship // who to send the invite as
10// & ship = ~ship // what planet to send as the invite
11// & email = ex@amp.le // email to send the invite code to
12//
13// ? kind = takeLockup
14// & lock = linear | conditional // which lockup contract, defaults to linear
15// & from = 0x1234abcd // ethereum address to accept transfer from
16//
18const COMMANDS = {
19 TAKE_LOCKUP: 'takeLockup',
20};
22const useFlowCommand = () => {
23 const flow = useMemo(() => {
24 let flow = {};
26 window.location.search
27 .substr(1)
28 .split('&')
29 .forEach(arg => {
30 if ('' === arg) return;
31 const pam = arg.split('=');
32 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
33 });
35 if (typeof flow === 'object') {
36 switch (flow.kind) {
37 case COMMANDS.TAKE_LOCKUP:
38 flow.lock = flow.lock || 'linear';
39 if (
40 (flow.lock !== 'linear' && flow.lock !== 'conditional') ||
41 !isValidAddress(flow.from)
42 ) {
43 flow = null;
44 }
45 break;
46 //
47 default:
48 console.log('unrecognized kind of flow:', flow.kind);
49 // eslint-disable-next-line no-fallthrough
50 case undefined:
51 flow = null;
52 break;
53 }
54 } else {
55 flow = null;
56 }
58 return flow;
59 }, []);
61 return flow;
62};
64export { COMMANDS, useFlowCommand };