on 8d8b291 help sign in
bridge/src/lib/flowCommand.js
49 lines 1.3 kB history raw
1import ob from 'urbit-ob';
3import { ensurePatFormat } from 'form/formatters';
5// looks into the window context to find a "flow command", an object indicating
6// if bridge is to be used for one specific flow.
7// expects to find objects of the following shape, 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// }
14//
15// you may optionally include .success() and .failure(reason) callbacks.
17const COMMANDS = {
18 INVITE: 'invite',
19};
21const useFlowCommand = () => {
22 let flow = window.flow;
24 if (typeof flow === 'object') {
25 flow.success = flow.success || (() => {});
26 flow.failure = flow.failure || (() => {});
27 switch (flow.kind) {
28 case COMMANDS.INVITE:
29 flow.as = ensurePatFormat(flow.as);
30 flow.ship = ensurePatFormat(flow.ship);
31 if (!ob.isValidPatp(flow.as) || !ob.isValidPatp(flow.ship)) {
32 console.log('invalid inviting flow arguments', flow.as, flow.ship);
33 flow = null;
34 }
35 break;
36 //
37 default:
38 console.log('unrecognized kind of flow:', flow.kind);
39 flow = null;
40 break;
41 }
42 } else {
43 flow = null;
44 }
46 return flow;
47};
49export { COMMANDS, useFlowCommand };