forked from 1Hive/gardens-ui
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAddProposalPanel.js
More file actions
358 lines (325 loc) · 9.01 KB
/
AddProposalPanel.js
File metadata and controls
358 lines (325 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import React, { useCallback, useMemo, useState } from 'react'
import {
Button,
DropDown,
Field,
GU,
Info,
isAddress,
Link,
MEDIUM_RADIUS,
TextInput,
useTheme,
} from '@tecommons/ui'
import { useAppState } from '../providers/AppState'
import connect from '../base/connect'
import BigNumber from '../lib/bigNumber'
import { toDecimals } from '../lib/math-utils'
import { formatTokenAmount } from '../lib/token-utils'
import { calculateThreshold, getMaxConviction } from '../lib/conviction'
import { ZERO_ADDR } from '../constants'
const FORUM_POST_REGEX = /https:\/\/forum.tecommons.org\/t\/.*?\/([0-9]+)/
const NULL_PROPOSAL_TYPE = -1
const FUNDING_PROPOSAL = 1
const DEFAULT_FORM_DATA = {
title: '',
link: '',
proposalType: NULL_PROPOSAL_TYPE,
amount: {
value: '0',
valueBN: new BigNumber(0),
},
beneficiary: '',
}
const AddProposalPanel = React.memo(({ onSubmit }) => {
const theme = useTheme()
const {
alpha,
maxRatio,
requestToken,
stakeToken,
effectiveSupply,
vaultBalance,
weight,
} = useAppState()
const [formData, setFormData] = useState(DEFAULT_FORM_DATA)
const fundingMode = formData.proposalType === FUNDING_PROPOSAL
const handleAmountEditMode = useCallback(
editMode => {
setFormData(formData => {
const { amount } = formData
const newValue = amount.valueBN.gte(0)
? formatTokenAmount(
amount.valueBN,
stakeToken.decimals,
false,
false,
{
commas: !editMode,
replaceZeroBy: editMode ? '' : '0',
rounding: stakeToken.decimals,
}
)
: ''
return {
...formData,
amount: {
...amount,
value: newValue,
},
}
})
},
[stakeToken]
)
const handleTitleChange = useCallback(event => {
const updatedTitle = event.target.value
setFormData(formData => ({ ...formData, title: updatedTitle }))
}, [])
const handleAmountChange = useCallback(
event => {
const updatedAmount = event.target.value
const newAmountBN = new BigNumber(
isNaN(updatedAmount)
? -1
: toDecimals(updatedAmount, stakeToken.decimals)
)
setFormData(formData => ({
...formData,
amount: {
value: updatedAmount,
valueBN: newAmountBN,
},
}))
},
[stakeToken.decimals]
)
const handleProposalTypeChange = useCallback(selected => {
setFormData(formData => ({
...formData,
proposalType: selected,
}))
}, [])
const handleBeneficiaryChange = useCallback(event => {
const updatedBeneficiary = event.target.value
setFormData(formData => ({ ...formData, beneficiary: updatedBeneficiary }))
}, [])
const handleLinkChange = useCallback(event => {
const updatedLink = event.target.value
setFormData(formData => ({ ...formData, link: updatedLink }))
}, [])
const handleFormSubmit = useCallback(
event => {
event.preventDefault()
const { amount, beneficiary, link, title } = formData
const convertedAmount = amount.valueBN.toString(10)
onSubmit({
title,
link,
amount: convertedAmount,
beneficiary: beneficiary || ZERO_ADDR,
})
},
[formData, onSubmit]
)
const errors = useMemo(() => {
const errors = []
const { amount, beneficiary, link } = formData
if (requestToken) {
if (amount.valueBN.eq(-1)) {
errors.push('Invalid requested amount')
}
if (beneficiary && !isAddress(beneficiary)) {
errors.push('Beneficiary is not a valid ethereum address')
}
}
if (link && !FORUM_POST_REGEX.test(link)) {
errors.push('Forum post link not provided ')
}
return errors
}, [formData, requestToken])
const neededThreshold = useMemo(() => {
const threshold = calculateThreshold(
formData.amount.valueBN,
vaultBalance,
effectiveSupply,
alpha,
maxRatio,
weight
)
const max = getMaxConviction(effectiveSupply, alpha)
return Math.round((threshold / max) * 100)
}, [alpha, formData.amount, maxRatio, effectiveSupply, vaultBalance, weight])
const submitDisabled =
formData.proposalType === NULL_PROPOSAL_TYPE ||
(formData.proposalType === FUNDING_PROPOSAL &&
(formData.amount.value === '0' || !formData.beneficiary)) ||
!formData.title ||
!formData.link ||
errors.length > 0
return (
<form onSubmit={handleFormSubmit}>
<Field
label="Select proposal type"
css={`
margin-top: ${3 * GU}px;
`}
>
<DropDown
header="Select proposal type"
placeholder="Proposal type"
selected={formData.proposalType}
onChange={handleProposalTypeChange}
items={['Signaling proposal', 'Funding proposal']}
required
wide
/>
</Field>
<Field
label="Title"
css={`
margin-top: ${2 * GU}px;
`}
>
<TextInput
onChange={handleTitleChange}
value={formData.title}
wide
required
/>
</Field>
{requestToken && fundingMode && (
<>
<Field
label="Requested Amount"
onFocus={() => handleAmountEditMode(true)}
onBlur={() => handleAmountEditMode(false)}
>
<TextInput
value={formData.amount.value}
onChange={handleAmountChange}
required
wide
adornment={
<span
css={`
background: ${theme.background};
border-left: 1px solid ${theme.border};
border-radius: 0px ${MEDIUM_RADIUS}px ${MEDIUM_RADIUS}px 0px;
padding: 7px ${1.5 * GU}px;
`}
>
{requestToken.symbol}
</span>
}
adornmentPosition="end"
adornmentSettings={{ padding: 1 }}
/>
</Field>
<Field label="Beneficiary">
<TextInput
onChange={handleBeneficiaryChange}
value={formData.beneficiary}
wide
required
/>
</Field>
</>
)}
<Field label="Link">
<TextInput
onChange={handleLinkChange}
value={formData.link}
wide
required
/>
</Field>
<Info title="Proposal creation guidelines">
In order to create a proposal you must first create a post on the{' '}
<Link href={`${connect.discourse}/new-topic?category=proposals`}>
TECommons Forum
</Link>{' '}
under the 🌱 Proposals category and paste the link to the corresponding
post in the LINK field.
</Info>
<Button
wide
mode="strong"
type="submit"
disabled={errors.length > 0 || submitDisabled}
css={`
margin-top: ${3 * GU}px;
`}
>
Submit
</Button>
{formData.proposalType !== NULL_PROPOSAL_TYPE && (
<Info
title="Action"
css={`
margin-top: ${3 * GU}px;
`}
>
{fundingMode ? (
<>
<span>
This action will create a proposal which can be voted on
</span>{' '}
<span
css={`
font-weight: 700;
`}
>
by staking {stakeToken.symbol}.
</span>{' '}
<span>
The action will be executable if the accrued total stake reaches
above the threshold.
</span>
</>
) : (
<>
<span>
This action will create a proposal which can be voted on,
</span>{' '}
<span
css={`
font-weight: 700;
`}
>
it’s a proposal without a requested amount.
</span>{' '}
<span>The action will not be executable.</span>
</>
)}
</Info>
)}
{fundingMode && formData.amount.valueBN.gte(0) && (
<Info
mode={neededThreshold ? 'info' : 'warning'}
css={`
margin-top: ${2 * GU}px;
`}
>
{neededThreshold
? `Required conviction for requested amount in order for the proposal to
pass is ~%${neededThreshold}`
: `Proposal might never pass with requested amount`}
</Info>
)}
{errors.length > 0 && (
<Info
mode="warning"
css={`
margin-top: ${2 * GU}px;
`}
>
{errors.map((err, index) => (
<div key={index}>{err}</div>
))}
</Info>
)}
</form>
)
})
export default AddProposalPanel