on 5a7d61b help sign in
bridge/src/lib/flowCommand.js
55 lines 1.5 kB history raw
1import ob from 'urbit-ob';
3import { ensurePatFormat } from 'form/formatters';
5// looks into the GET parameters and attempts to construct a "flow" object,
6// which would indicate bridge is to be used for one specific flow.
7// expects to find any of the following sets of parameters, varying by kind.
8//
9// ? kind = invite
10// & as = ~ship // who to send the invite as
11// & ship = ~ship // what planet to send as the invite
12// & email = ex@amp.le // email to send the invite code to
13//
15const COMMANDS = {
16 INVITE: 'invite',
17};
19const useFlowCommand = () => {
20 let flow = {};
21 window.location.search
22 .substr(1)
23 .split('&')
24 .forEach(arg => {
25 if ('' === arg) return;
26 const pam = arg.split('=');
27 flow[pam[0]] = pam.length <= 1 ? true : decodeURIComponent(pam[1]);
28 });
30 if (typeof flow === 'object') {
31 switch (flow.kind) {
32 case COMMANDS.INVITE:
33 flow.as = ensurePatFormat(flow.as);
34 flow.ship = ensurePatFormat(flow.ship);
35 if (!ob.isValidPatp(flow.as) || !ob.isValidPatp(flow.ship)) {
36 console.log('invalid inviting flow arguments', flow.as, flow.ship);
37 flow = null;
38 }
39 break;
40 //
41 default:
42 console.log('unrecognized kind of flow:', flow.kind);
43 // eslint-disable-next-line no-fallthrough
44 case undefined:
45 flow = null;
46 break;
47 }
48 } else {
49 flow = null;
50 }
52 return flow;
53};
55export { COMMANDS, useFlowCommand };