-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathStaking.tsx
More file actions
155 lines (145 loc) · 5.22 KB
/
Copy pathStaking.tsx
File metadata and controls
155 lines (145 loc) · 5.22 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
import { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { Card, CardContent, CardHeader, CardTitle } from '@oasisprotocol/ui-library/src/components/cards'
import Link from '@mui/material/Link'
import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton'
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@oasisprotocol/ui-library/src/components/tabs'
import {
Runtime,
RuntimeAccount,
useGetRuntimeAccountsAddressDebondingDelegations,
useGetRuntimeAccountsAddressDelegations,
} from '../../../oasis-nexus/api'
import { useRequiredScopeParam } from '../../../app/hooks/useScopeParam'
import { AppErrors } from '../../../types/errors'
import { NUMBER_OF_ITEMS_ON_DASHBOARD as PAGE_SIZE } from '../../../config'
import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination'
import { Delegations } from '../../components/Delegations'
import { dapps } from '../../utils/externalLinks'
import { t } from 'i18next'
import { AccountCardEmptyState } from './AccountCardEmptyState'
import { Typography } from '@oasisprotocol/ui-library/src/components/typography'
type StakingProps = {
account: RuntimeAccount | undefined
isLoading: boolean
}
export const Staking: FC<StakingProps> = ({ account, isLoading }) => {
const { t } = useTranslation()
return (
<Card variant="layout">
<CardHeader>
<CardTitle>
<Typography variant="h3">{t('stakingOverview')}</Typography>
</CardTitle>
</CardHeader>
<CardContent>
<Tabs defaultValue="staked" aria-label={t('validator.delegations')}>
<TabsList variant="layout" className="md:max-w-[400px] rounded-b-md mb-2">
<TabsTrigger value="staked">{t('common.staked')}</TabsTrigger>
<TabsTrigger value="debonding">{t('common.debonding')}</TabsTrigger>
</TabsList>
{isLoading && <Skeleton className="h-[200px] mt-8" />}
{!isLoading && account && (
<>
<TabsContent value="staked" className="min-h-28">
<ActiveDelegations address={account?.address} />
</TabsContent>
<TabsContent value="debonding" className="min-h-28">
<DebondingDelegations address={account?.address} />
</TabsContent>
</>
)}
</Tabs>
</CardContent>
</Card>
)
}
type DelegationCardProps = {
address: string
}
const ActiveDelegations: FC<DelegationCardProps> = ({ address }) => {
const pagination = useSearchParamsPagination('activeDelegations')
const offset = (pagination.selectedPage - 1) * PAGE_SIZE
const scope = useRequiredScopeParam()
const { network } = scope
const delegationsQuery = useGetRuntimeAccountsAddressDelegations(network, scope.layer as Runtime, address, {
limit: PAGE_SIZE,
offset,
})
const { isLoading, isFetched, data } = delegationsQuery
if (isFetched && offset && !delegationsQuery.data?.data?.delegations?.length) {
throw AppErrors.PageDoesNotExist
}
if (isFetched && !delegationsQuery.data?.data.delegations.length) {
return (
<AccountCardEmptyState label={t('account.notStaking')}>
<Link href={dapps.stake} rel="noopener noreferrer" target="_blank">
{t('account.startStaking')}
</Link>
</AccountCardEmptyState>
)
}
return (
<>
{isFetched && (
<Delegations
delegations={delegationsQuery.data?.data.delegations}
isLoading={isLoading}
limit={PAGE_SIZE}
linkType="validator"
pagination={{
className: 'mt-2',
selectedPage: pagination.selectedPage,
linkToPage: pagination.linkToPage,
totalCount: data?.data.total_count,
isTotalCountClipped: data?.data.is_total_count_clipped,
rowsPerPage: PAGE_SIZE,
}}
/>
)}
</>
)
}
const DebondingDelegations: FC<DelegationCardProps> = ({ address }) => {
const pagination = useSearchParamsPagination('debondingDelegations')
const offset = (pagination.selectedPage - 1) * PAGE_SIZE
const scope = useRequiredScopeParam()
const { network } = scope
const delegationsQuery = useGetRuntimeAccountsAddressDebondingDelegations(
network,
scope.layer as Runtime,
address,
{
limit: PAGE_SIZE,
offset,
},
)
const { isLoading, isFetched, data } = delegationsQuery
if (isFetched && offset && !delegationsQuery.data?.data?.debonding_delegations?.length) {
throw AppErrors.PageDoesNotExist
}
if (isFetched && !delegationsQuery.data?.data.debonding_delegations.length) {
return <AccountCardEmptyState label={t('account.notDebonding')} />
}
return (
<>
{isFetched && (
<Delegations
debonding
delegations={delegationsQuery.data?.data.debonding_delegations}
isLoading={isLoading}
limit={PAGE_SIZE}
linkType="validator"
pagination={{
className: 'mt-2',
selectedPage: pagination.selectedPage,
linkToPage: pagination.linkToPage,
totalCount: data?.data.total_count,
isTotalCountClipped: data?.data.is_total_count_clipped,
rowsPerPage: PAGE_SIZE,
}}
/>
)}
</>
)
}