|
| 1 | +import { FC } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import { Card, CardContent, CardHeader, CardTitle } from '@oasisprotocol/ui-library/src/components/cards' |
| 4 | +import Link from '@mui/material/Link' |
| 5 | +import { Skeleton } from '@oasisprotocol/ui-library/src/components/ui/skeleton' |
| 6 | +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@oasisprotocol/ui-library/src/components/tabs' |
| 7 | +import { |
| 8 | + Runtime, |
| 9 | + RuntimeAccount, |
| 10 | + useGetRuntimeAccountsAddressDebondingDelegations, |
| 11 | + useGetRuntimeAccountsAddressDelegations, |
| 12 | +} from '../../../oasis-nexus/api' |
| 13 | +import { useRequiredScopeParam } from '../../../app/hooks/useScopeParam' |
| 14 | +import { AppErrors } from '../../../types/errors' |
| 15 | +import { NUMBER_OF_ITEMS_ON_DASHBOARD as PAGE_SIZE } from '../../../config' |
| 16 | +import { useSearchParamsPagination } from '../../components/Table/useSearchParamsPagination' |
| 17 | +import { Delegations } from '../../components/Delegations' |
| 18 | +import { dapps } from '../../utils/externalLinks' |
| 19 | +import { t } from 'i18next' |
| 20 | +import { AccountCardEmptyState } from './AccountCardEmptyState' |
| 21 | +import { Typography } from '@oasisprotocol/ui-library/src/components/typography' |
| 22 | + |
| 23 | +type StakingProps = { |
| 24 | + account: RuntimeAccount | undefined |
| 25 | + isLoading: boolean |
| 26 | +} |
| 27 | + |
| 28 | +export const Staking: FC<StakingProps> = ({ account, isLoading }) => { |
| 29 | + const { t } = useTranslation() |
| 30 | + |
| 31 | + return ( |
| 32 | + <Card variant="layout"> |
| 33 | + <CardHeader> |
| 34 | + <CardTitle> |
| 35 | + <Typography variant="h3">{t('stakingOverview')}</Typography> |
| 36 | + </CardTitle> |
| 37 | + </CardHeader> |
| 38 | + <CardContent> |
| 39 | + <Tabs defaultValue="staked" aria-label={t('validator.delegations')}> |
| 40 | + <TabsList variant="layout" className="md:max-w-[400px] rounded-b-md mb-2"> |
| 41 | + <TabsTrigger value="staked">{t('common.staked')}</TabsTrigger> |
| 42 | + <TabsTrigger value="debonding">{t('common.debonding')}</TabsTrigger> |
| 43 | + </TabsList> |
| 44 | + {isLoading && <Skeleton className="h-[200px] mt-8" />} |
| 45 | + {!isLoading && account && ( |
| 46 | + <> |
| 47 | + <TabsContent value="staked" className="min-h-28"> |
| 48 | + <ActiveDelegations address={account?.address} /> |
| 49 | + </TabsContent> |
| 50 | + <TabsContent value="debonding" className="min-h-28"> |
| 51 | + <DebondingDelegations address={account?.address} /> |
| 52 | + </TabsContent> |
| 53 | + </> |
| 54 | + )} |
| 55 | + </Tabs> |
| 56 | + </CardContent> |
| 57 | + </Card> |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +type DelegationCardProps = { |
| 62 | + address: string |
| 63 | +} |
| 64 | + |
| 65 | +const ActiveDelegations: FC<DelegationCardProps> = ({ address }) => { |
| 66 | + const pagination = useSearchParamsPagination('activeDelegations') |
| 67 | + const offset = (pagination.selectedPage - 1) * PAGE_SIZE |
| 68 | + const scope = useRequiredScopeParam() |
| 69 | + const { network } = scope |
| 70 | + const delegationsQuery = useGetRuntimeAccountsAddressDelegations(network, scope.layer as Runtime, address, { |
| 71 | + limit: PAGE_SIZE, |
| 72 | + offset, |
| 73 | + }) |
| 74 | + const { isLoading, isFetched, data } = delegationsQuery |
| 75 | + if (isFetched && offset && !delegationsQuery.data?.data?.delegations?.length) { |
| 76 | + throw AppErrors.PageDoesNotExist |
| 77 | + } |
| 78 | + |
| 79 | + if (isFetched && !delegationsQuery.data?.data.delegations.length) { |
| 80 | + return ( |
| 81 | + <AccountCardEmptyState label={t('account.notStaking')}> |
| 82 | + <Link href={dapps.stake} rel="noopener noreferrer" target="_blank"> |
| 83 | + {t('account.startStaking')} |
| 84 | + </Link> |
| 85 | + </AccountCardEmptyState> |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <> |
| 91 | + {isFetched && ( |
| 92 | + <Delegations |
| 93 | + delegations={delegationsQuery.data?.data.delegations} |
| 94 | + isLoading={isLoading} |
| 95 | + limit={PAGE_SIZE} |
| 96 | + linkType="validator" |
| 97 | + pagination={{ |
| 98 | + className: 'mt-2', |
| 99 | + selectedPage: pagination.selectedPage, |
| 100 | + linkToPage: pagination.linkToPage, |
| 101 | + totalCount: data?.data.total_count, |
| 102 | + isTotalCountClipped: data?.data.is_total_count_clipped, |
| 103 | + rowsPerPage: PAGE_SIZE, |
| 104 | + }} |
| 105 | + /> |
| 106 | + )} |
| 107 | + </> |
| 108 | + ) |
| 109 | +} |
| 110 | + |
| 111 | +const DebondingDelegations: FC<DelegationCardProps> = ({ address }) => { |
| 112 | + const pagination = useSearchParamsPagination('debondingDelegations') |
| 113 | + const offset = (pagination.selectedPage - 1) * PAGE_SIZE |
| 114 | + const scope = useRequiredScopeParam() |
| 115 | + const { network } = scope |
| 116 | + const delegationsQuery = useGetRuntimeAccountsAddressDebondingDelegations( |
| 117 | + network, |
| 118 | + scope.layer as Runtime, |
| 119 | + address, |
| 120 | + { |
| 121 | + limit: PAGE_SIZE, |
| 122 | + offset, |
| 123 | + }, |
| 124 | + ) |
| 125 | + const { isLoading, isFetched, data } = delegationsQuery |
| 126 | + if (isFetched && offset && !delegationsQuery.data?.data?.debonding_delegations?.length) { |
| 127 | + throw AppErrors.PageDoesNotExist |
| 128 | + } |
| 129 | + |
| 130 | + if (isFetched && !delegationsQuery.data?.data.debonding_delegations.length) { |
| 131 | + return <AccountCardEmptyState label={t('account.notDebonding')} /> |
| 132 | + } |
| 133 | + |
| 134 | + return ( |
| 135 | + <> |
| 136 | + {isFetched && ( |
| 137 | + <Delegations |
| 138 | + debonding |
| 139 | + delegations={delegationsQuery.data?.data.debonding_delegations} |
| 140 | + isLoading={isLoading} |
| 141 | + limit={PAGE_SIZE} |
| 142 | + linkType="validator" |
| 143 | + pagination={{ |
| 144 | + className: 'mt-2', |
| 145 | + selectedPage: pagination.selectedPage, |
| 146 | + linkToPage: pagination.linkToPage, |
| 147 | + totalCount: data?.data.total_count, |
| 148 | + isTotalCountClipped: data?.data.is_total_count_clipped, |
| 149 | + rowsPerPage: PAGE_SIZE, |
| 150 | + }} |
| 151 | + /> |
| 152 | + )} |
| 153 | + </> |
| 154 | + ) |
| 155 | +} |
0 commit comments