on 91efb05 help sign in
bridge/src/views/Party/PartySetPoolSize.js
185 lines 5.3 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import cn from 'classnames';
3import { Grid, Text } from 'indigo-react';
4import * as azimuth from 'azimuth-js';
6import { useNetwork } from 'store/network';
7import { usePointCursor } from 'store/pointCursor';
8import { usePointCache } from 'store/pointCache';
10import * as need from 'lib/need';
11import useEthereumTransaction from 'lib/useEthereumTransaction';
12import { GAS_LIMITS } from 'lib/constants';
13import { useLocalRouter } from 'lib/LocalRouter';
14import useCurrentPermissions from 'lib/useCurrentPermissions';
15import { patp2dec } from 'lib/patp2dec';
16import { convertToInt } from 'lib/convertToInt';
18import ViewHeader from 'components/ViewHeader';
19import InlineEthereumTransaction from 'components/InlineEthereumTransaction';
20import View from 'components/View';
21import WarningBox from 'components/WarningBox';
23import BridgeForm from 'form/BridgeForm';
24import { PointInput, NumberInput } from 'form/Inputs';
25import {
26 composeValidator,
27 buildNumberValidator,
28 buildPointValidator,
29} from 'form/validators';
30import FormError from 'form/FormError';
31import CopiableAddress from 'components/copiable/CopiableAddress';
33function useSetPoolSize() {
34 const { contracts } = useNetwork();
35 const { pointCursor } = usePointCursor();
36 const { syncExtras } = usePointCache();
38 const _contracts = need.contracts(contracts);
39 const _point = need.point(pointCursor);
41 return useEthereumTransaction(
42 useCallback(
43 (poolOwner, poolSize) =>
44 azimuth.delegatedSending.setPoolSize(
45 _contracts,
46 _point,
47 poolOwner,
48 poolSize
49 ),
50 [_contracts, _point]
51 ),
52 useCallback(() => syncExtras(_point), [_point, syncExtras]),
53 GAS_LIMITS.DEFAULT // TODO: GAS_LIMITS.SET_POOL_SIZE
54 );
57export default function PartySetPoolSize() {
58 const { pop } = useLocalRouter();
59 const { contracts } = useNetwork();
60 const { pointCursor } = usePointCursor();
61 const { getDetails } = usePointCache();
63 const _point = need.point(pointCursor);
64 const _details = need.details(getDetails(_point));
65 const hasKeysSet =
66 0 !== convertToInt(_details.encryptionKey, 16) &&
67 0 !== convertToInt(_details.authenticationKey, 16) &&
68 0 !== _details.cryptoSuiteVersion;
70 const _contracts = need.contracts(contracts);
72 const { spawnIsDelegatedSending } = useCurrentPermissions();
74 const {
75 construct,
76 unconstruct,
77 completed,
78 inputsLocked,
79 bind,
80 } = useSetPoolSize();
82 const validate = useMemo(
83 () =>
84 composeValidator({
85 poolOwner: buildPointValidator(),
86 poolSize: buildNumberValidator(0),
87 }),
88 []
89 );
91 const onValues = useCallback(
92 ({ valid, values }) => {
93 if (valid) {
94 construct(patp2dec(values.poolOwner), values.poolSize);
95 } else {
96 unconstruct();
97 }
98 },
99 [construct, unconstruct]
100 );
102 const initialValues = useMemo(() => ({ poolSize: 5 }), []);
104 return (
105 <View pop={pop} inset>
106 <Grid>
107 <Grid.Item full as={ViewHeader}>
108 Set Pool Size
109 </Grid.Item>
111 {!spawnIsDelegatedSending && (
112 <Grid.Item full as={WarningBox} className="mb4 f6">
113 The spawn proxy must be set to{' '}
114 <CopiableAddress>
115 {_contracts.delegatedSending.address}
116 </CopiableAddress>{' '}
117 for invitations to be available for use.
118 </Grid.Item>
119 )}
121 {!hasKeysSet && (
122 <Grid.Item full as={WarningBox} className="mb4 f6">
123 Networking keys must be configured for invitations to be available
124 for use.
125 </Grid.Item>
126 )}
128 <BridgeForm
129 validate={validate}
130 onValues={onValues}
131 initialValues={initialValues}>
132 {({ handleSubmit, values }) => (
133 <>
134 <Grid.Item
135 full
136 as={Text}
137 className={cn('f5 wrap', {
138 green3: completed,
139 })}>
140 {completed ? (
141 <>
142 {values.poolSize} invites have been allocated to{' '}
143 <CopiableAddress>{values.poolOwner}</CopiableAddress>
144 </>
145 ) : (
146 `Allocate invites to a child point.`
147 )}
148 </Grid.Item>
150 {!completed && (
151 <>
152 <Grid.Item
153 full
154 as={PointInput}
155 className="mt4"
156 name="poolOwner"
157 label="Point"
158 disabled={inputsLocked}
159 />
160 <Grid.Item
161 full
162 as={NumberInput}
163 className="mb4"
164 name="poolSize"
165 label="Pool Size"
166 disabled={inputsLocked}
167 />
168 </>
169 )}
171 <Grid.Item full as={FormError} />
173 <Grid.Item
174 full
175 as={InlineEthereumTransaction}
176 {...bind}
177 onReturn={() => pop()}
178 />
179 </>
180 )}
181 </BridgeForm>
182 </Grid>
183 </View>
184 );