-
Notifications
You must be signed in to change notification settings - Fork 22
/
useProcessTQ.ts
75 lines (67 loc) · 1.99 KB
/
useProcessTQ.ts
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
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import {
ProcessedTQ,
ProcessedTQType,
ProcessedThresholdQuorum,
} from '@dao-dao/types'
import { Threshold } from '@dao-dao/types/contracts/DaoProposalSingle.common'
import { formatPercentOf100 } from '@dao-dao/utils'
export const useProcessTQ = () => {
const { t } = useTranslation()
return useCallback(
(data: Threshold): ProcessedThresholdQuorum => {
//! Threshold
let threshold: ProcessedTQ
if ('absolute_count' in data) {
threshold = {
type: ProcessedTQType.Absolute,
value: Number(data.absolute_count.threshold),
display: data.absolute_count.threshold,
}
} else {
const thresholdSource =
'absolute_percentage' in data
? data.absolute_percentage.percentage
: data.threshold_quorum.threshold
if ('majority' in thresholdSource) {
threshold = {
type: ProcessedTQType.Majority,
display: t('info.majority'),
}
} else {
const percent = Number(thresholdSource.percent) * 100
threshold = {
type: ProcessedTQType.Percent,
value: percent,
display: formatPercentOf100(percent),
}
}
}
//! Quorum
let quorum: ProcessedTQ | undefined
const quorumSource =
'threshold_quorum' in data ? data.threshold_quorum.quorum : undefined
if (quorumSource) {
if ('majority' in quorumSource) {
quorum = {
type: ProcessedTQType.Majority,
display: t('info.majority'),
}
} else {
const percent = Number(quorumSource.percent) * 100
quorum = {
type: ProcessedTQType.Percent,
value: percent,
display: formatPercentOf100(percent),
}
}
}
return {
threshold,
...(quorum && { quorum }),
}
},
[t]
)
}