-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPayoutsList.tsx
183 lines (160 loc) · 5.22 KB
/
PayoutsList.tsx
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
import type { ApiPromise } from '@polkadot/api'
import { WalletAccount } from '@talismn/connect-wallets'
import { useState, useEffect } from 'react'
import { Badge, Button, Col } from 'react-bootstrap'
import styled from 'styled-components'
import { DataHeaderRow, DataRow } from '../../../../components/base'
import { FormatBalance } from '../../../../components/FormatBalance'
import { useBlockTime } from '../../../../hooks/useBlockTime'
import { Identicon } from '../../components/Identicon'
import { payout } from '../helper'
const StyledDataRow = styled(DataRow)`
background-color: ${(props) => (props.$isDefender ? props.theme.colors.black : '')};
border: ${(props) => (props.$isDefender ? `2px solid ${props.theme.colors.secondary}` : '')};
&:hover {
cursor: pointer;
}
@media (max-width: 992px) {
padding-block: 12px;
margin-inline: 2px;
}
`
type PayoutsListProps = {
api: ApiPromise
members: ExtendedSocietyMember[]
activeAccount: WalletAccount | undefined
handleUpdate: () => void
}
type TimeRemainingProps = {
api: ApiPromise
block: number
latestBlock: number | null
onClainPayout: () => void
}
const TimeRemaining = ({ block, latestBlock, api, onClainPayout }: TimeRemainingProps) => {
if (!latestBlock)
return (
<Badge pill bg="black" className="me-2 p-2">
Calculating...
</Badge>
)
const blocksLeft = block - latestBlock
const [, time] = useBlockTime(blocksLeft, api)
if (!time)
return (
<Badge pill bg="black" className="me-2 p-2">
Calculating...
</Badge>
)
if (blocksLeft <= 0)
return (
<>
<Badge pill bg="primary" className="me-2 p-2">
Matured
</Badge>
{/* @TODO: Only show claim button if it's the current address */}
<Button variant="primary" onClick={onClainPayout}>
Claim Payout
</Button>
</>
)
return (
<Badge pill bg="secondary" text="black" className="me-2 p-2">
Maturing in {time}
</Badge>
)
}
const PayoutsList = ({ api, members, activeAccount }: PayoutsListProps): JSX.Element => {
const [latestBlock, setLatestBlock] = useState<number | null>(null)
const isMember = (member: ExtendedSocietyMember) => activeAccount?.address === member.accountId.toHuman()
useEffect(() => {
const fetchLatestBlock = async () => {
const header = await api.rpc.chain.getHeader()
setLatestBlock(header.number.toNumber())
}
fetchLatestBlock()
const unsub = api.rpc.chain.subscribeNewHeads((header) => {
setLatestBlock(header.number.toNumber())
})
return () => {
unsub.then((u) => u())
}
}, [api])
if (members.length === 0) return <>No members</>
const onClainPayout = () => {
const tx = api.tx.society.payout()
const notReallySure = () => {
console.log('oiii ??????')
}
payout(tx, api, activeAccount, notReallySure)
}
return (
<>
<DataHeaderRow className="d-none d-lg-flex text-center">
<Col lg={1}>#</Col>
<Col lg={5} className="text-center text-lg-start">
Wallet Hash
</Col>
<Col lg={2} className="text-center text-lg-start">
Paid
</Col>
<Col lg={2} className="text-center text-lg-start">
Pending
</Col>
<Col lg={2}></Col>
</DataHeaderRow>
{members.map((member: ExtendedSocietyMember) => (
<StyledDataRow key={member.accountId.toString()}>
<Col lg={1} className="text-center">
<Identicon value={member.accountId.toHuman()} size={32} theme={'polkadot'} />
</Col>
<Col lg={5} className="text-center text-lg-start">
{member.accountId.toHuman()}
</Col>
<Col lg={2} className="text-center text-lg-start">
<FormatBalance balance={member.extendedPayouts.paid} />
</Col>
<Col lg={2} className="text-center text-lg-start">
<FormatBalance balance={member.extendedPayouts.pending} />
</Col>
<Col lg={2} className="text-center text-lg-end">
{member.isFounder && (
<Badge pill bg="dark" className="me-2 p-2">
Founder
</Badge>
)}
{member.rank.toNumber() > 0 && (
<Badge pill bg="dark" className="me-2 p-2">
Ranked
</Badge>
)}
{member.hasPayouts && (
<TimeRemaining
block={member.extendedPayouts.block}
latestBlock={latestBlock}
api={api}
onClainPayout={onClainPayout}
/>
)}
{member.extendedPayouts.pending == 0 && member.extendedPayouts.paid > 0 && (
<Badge pill bg="black" className="me-2 p-2">
Paid
</Badge>
)}
{!member.hasPayouts && member.extendedPayouts.paid == 0 && (
<Badge pill bg="black" className="me-2 p-2">
Paid before V2
</Badge>
)}
{isMember(member) && (
<Badge bg="primary" className="me-2 p-2">
You
</Badge>
)}
</Col>
</StyledDataRow>
))}
</>
)
}
export { PayoutsList }