on e366ef3 help sign in
bridge/src/views/InvitesManage.js
188 lines 5.2 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import React from 'react';
3import * as ob from 'urbit-ob';
4import * as azimuth from 'azimuth-js';
5import * as need from '../lib/need';
7import {
8 Row,
9 Col,
10 Warning,
11 Input,
12 PointInput,
13 InnerLabel,
14 ValidatedSigil,
15} from '../components/old/Base';
16import StatelessTransaction from '../components/old/StatelessTransaction';
17import { withNetwork } from '../store/network';
18import { compose } from '../lib/lib';
19import { withPointCursor } from '../store/pointCursor';
21class InvitesManage extends React.Component {
22 constructor(props) {
23 super(props);
25 this.state = {
26 invitesWillWork: Nothing(),
27 planetInput: '',
28 targetPlanet: Nothing(),
29 currentPoolSize: Nothing(),
30 cachedPoolSizes: {},
31 targetPoolSize: 5,
32 };
34 this.checkCurrentInvites = this.checkCurrentInvites.bind(this);
35 this.handlePointInput = this.handlePointInput.bind(this);
36 this.updatePoolSize = this.updatePoolSize.bind(this);
37 this.handlePoolSizeInput = this.handlePoolSizeInput.bind(this);
38 this.createUnsignedTxn = this.createUnsignedTxn.bind(this);
39 this.statelessRef = React.createRef();
40 }
42 componentDidMount() {
43 this.point = need.pointCursor(this.props);
44 this.contracts = need.contracts(this.props);
46 azimuth.azimuth
47 .isSpawnProxy(
48 this.contracts,
49 this.point,
50 this.contracts.delegatedSending.address
51 )
52 .then(isSpawnProxy => {
53 this.setState({ invitesWillWork: Just(isSpawnProxy) });
54 });
55 }
57 componentDidUpdate(prevProps) {
58 //
59 }
61 handlePointInput(targetPlanet) {
62 let newState = {
63 targetPlanet: Nothing(),
64 planetInput: targetPlanet,
65 canGenerate: false,
66 currentPoolSize: Nothing(),
67 };
68 if (targetPlanet.length === 14 && ob.isValidPatp(targetPlanet)) {
69 const target = ob.patp2dec(targetPlanet);
70 //TODO there must be a simpler way to do this check
71 const isChild = ob.sein(targetPlanet) === ob.patp(this.point);
72 if (isChild) {
73 newState.targetPlanet = Just(target);
74 newState.currentPoolSize = this.checkCurrentInvites(target);
75 newState.canGenerate = true;
76 }
77 }
78 this.setState(newState);
79 }
81 checkCurrentInvites(who) {
82 const cachedPoolSizes = this.state.cachedPoolSizes;
83 if (typeof cachedPoolSizes[who] !== 'undefined') {
84 return Just(cachedPoolSizes[who]);
85 } else {
86 azimuth.delegatedSending.getPool(this.contracts, who).then(pool => {
87 azimuth.delegatedSending
88 .invitesInPool(this.contracts, pool, this.point)
89 .then(size => {
90 this.updatePoolSize(who, size);
91 });
92 });
93 return Nothing();
94 }
95 }
97 updatePoolSize(who, size) {
98 const cache = this.state.cachedPoolSizes;
99 cache[who] = size;
100 this.setState({ cachedPoolSizes: cache });
101 const target = this.state.targetPlanet;
102 const match = Just.hasInstance(target) ? who === target.value : false;
103 if (match) {
104 this.setState({ currentPoolSize: Just(size) });
105 }
106 }
108 handlePoolSizeInput(targetSize) {
109 this.setState({ targetPoolSize: targetSize });
110 }
112 createUnsignedTxn() {
113 return Just(
114 azimuth.delegatedSending.setPoolSize(
115 this.contracts,
116 this.point,
117 this.state.targetPlanet.value,
118 this.state.targetPoolSize
119 )
120 );
121 }
123 render() {
124 let spawnProxyWarning = null;
125 if (this.state.invitesWillWork.value === false) {
126 spawnProxyWarning = (
127 <Warning>
128 <h3 className={'mb-2'}>{'Warning'}</h3>
129 {'Planets under this star will not be able to send invites ' +
130 'until the invite contract ('}
131 <code>{this.contracts.delegatedSending.address}</code>
132 {') is made spawn proxy for this star.'}
133 </Warning>
134 );
135 }
137 let poolSizeText = this.state.currentPoolSize.matchWith({
138 Just: ps => `(currently ${ps.value})`,
139 Nothing: _ => '',
140 });
142 return (
143 <Row>
144 <Col>
145 <p>
146 {
147 'manage invites here, for stars. can only give invites to child planets'
148 }
149 </p>
151 {spawnProxyWarning}
153 <PointInput
154 prop-format="innerlabel"
155 prop-size="lg"
156 placeholder={'~sampel-sipnem'}
157 value={this.state.planetInput}
158 onChange={this.handlePointInput}>
159 <InnerLabel>{'Planet to set invites for'}</InnerLabel>
160 <ValidatedSigil
161 patp={this.state.targetPlanet}
162 validator={() => this.validatePoint}
163 />
164 </PointInput>
166 <Input
167 prop-format="innerlabel"
168 prop-size="lg"
169 value={this.state.targetPoolSize}
170 onChange={this.handlePoolSizeInput}>
171 <InnerLabel>{`Available invites ${poolSizeText}`}</InnerLabel>
172 </Input>
174 <StatelessTransaction
175 canGenerate={this.state.canGenerate}
176 createUnsignedTxn={this.createUnsignedTxn}
177 ref={this.statelessRef}
178 />
179 </Col>
180 </Row>
181 );
182 }
185export default compose(
186 withNetwork,
187 withPointCursor
188)(InvitesManage);