|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import { |
| 4 | + EverstakeRewardsEndpointType, |
| 5 | + fetchEverstakeRewards, |
| 6 | + selectStakingRewards, |
| 7 | + StakeAccountRewards, |
| 8 | +} from '@suite-common/wallet-core'; |
| 9 | +import { formatNetworkAmount } from '@suite-common/wallet-utils'; |
| 10 | +import { Badge, Card, Column, Icon, Row, SkeletonStack, Text, Tooltip } from '@trezor/components'; |
| 11 | +import { spacings } from '@trezor/theme'; |
| 12 | +import { SOLANA_EPOCH_DAYS } from '@suite-common/wallet-constants'; |
| 13 | + |
| 14 | +import { |
| 15 | + CoinBalance, |
| 16 | + FiatValue, |
| 17 | + FormattedDate, |
| 18 | + HiddenPlaceholder, |
| 19 | + Translation, |
| 20 | +} from 'src/components/suite'; |
| 21 | +import { DashboardSection } from 'src/components/dashboard'; |
| 22 | +import { useDispatch, useSelector } from 'src/hooks/suite'; |
| 23 | +import { Account } from 'src/types/wallet'; |
| 24 | +import { Pagination } from 'src/components/wallet'; |
| 25 | +import SkeletonTransactionItem from 'src/views/wallet/transactions/TransactionList/SkeletonTransactionItem'; |
| 26 | +import { ColDate } from 'src/views/wallet/transactions/TransactionList/TransactionsGroup/CommonComponents'; |
| 27 | + |
| 28 | +const PAGE_SIZE_DEFAULT = 10; |
| 29 | + |
| 30 | +interface RewardsListProps { |
| 31 | + account: Account; |
| 32 | +} |
| 33 | + |
| 34 | +export const RewardsList = ({ account }: RewardsListProps) => { |
| 35 | + const anchor = useSelector(state => state.router.anchor); |
| 36 | + const { data, isLoading } = |
| 37 | + useSelector(state => selectStakingRewards(state, account?.symbol)) || {}; |
| 38 | + |
| 39 | + const { rewards } = data ?? {}; |
| 40 | + const sectionRef = useRef<HTMLDivElement>(null); |
| 41 | + |
| 42 | + const dispatch = useDispatch(); |
| 43 | + |
| 44 | + const perPage = PAGE_SIZE_DEFAULT; |
| 45 | + const startPage = 1; |
| 46 | + |
| 47 | + const [currentPage, setSelectedPage] = useState(startPage); |
| 48 | + const [slicedRewards, setSlicedRewards] = useState<StakeAccountRewards[]>([]); |
| 49 | + |
| 50 | + const startIndex = (currentPage - 1) * perPage; |
| 51 | + const stopIndex = startIndex + perPage; |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + // Fetch rewards only for the Solana mainnet |
| 55 | + if (account.symbol === 'sol') { |
| 56 | + dispatch( |
| 57 | + fetchEverstakeRewards({ |
| 58 | + symbol: account.symbol, |
| 59 | + endpointType: EverstakeRewardsEndpointType.GetRewards, |
| 60 | + address: account.descriptor, |
| 61 | + }), |
| 62 | + ); |
| 63 | + } |
| 64 | + }, [anchor, account, dispatch]); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + if (rewards) { |
| 68 | + const slicedRewards = rewards?.slice(startIndex, stopIndex); |
| 69 | + setSlicedRewards(slicedRewards); |
| 70 | + } |
| 71 | + }, [currentPage, rewards, startIndex, stopIndex]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + // reset page on account change |
| 75 | + setSelectedPage(startPage); |
| 76 | + }, [account.descriptor, account.symbol, startPage]); |
| 77 | + |
| 78 | + const totalItems = rewards?.length ?? 0; |
| 79 | + const showPagination = totalItems > perPage; |
| 80 | + const isLastPage = stopIndex >= totalItems; |
| 81 | + |
| 82 | + const onPageSelected = (page: number) => { |
| 83 | + setSelectedPage(page); |
| 84 | + if (sectionRef.current) { |
| 85 | + sectionRef.current.scrollIntoView(); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + return ( |
| 90 | + <DashboardSection |
| 91 | + ref={sectionRef} |
| 92 | + heading={<Translation id="TR_REWARDS" />} |
| 93 | + data-testid="@wallet/accounts/rewards-list" |
| 94 | + > |
| 95 | + {isLoading ? ( |
| 96 | + <SkeletonStack $col $childMargin="0px 0px 16px 0px"> |
| 97 | + <SkeletonTransactionItem /> |
| 98 | + <SkeletonTransactionItem /> |
| 99 | + <SkeletonTransactionItem /> |
| 100 | + </SkeletonStack> |
| 101 | + ) : ( |
| 102 | + <> |
| 103 | + {slicedRewards?.map(reward => ( |
| 104 | + <React.Fragment key={reward.epoch}> |
| 105 | + <Row> |
| 106 | + <ColDate> |
| 107 | + <FormattedDate |
| 108 | + value={reward?.time ?? undefined} |
| 109 | + day="numeric" |
| 110 | + month="long" |
| 111 | + year="numeric" |
| 112 | + /> |
| 113 | + </ColDate> |
| 114 | + </Row> |
| 115 | + <Card> |
| 116 | + <Row |
| 117 | + justifyContent="space-between" |
| 118 | + margin={{ horizontal: spacings.xs, bottom: spacings.xs }} |
| 119 | + > |
| 120 | + <Row gap={spacings.xs}> |
| 121 | + <Icon name="arrowLineDown" variant="tertiary" /> |
| 122 | + <Column> |
| 123 | + <Text typographyStyle="body" variant="tertiary"> |
| 124 | + <Translation id="TR_REWARD" /> |
| 125 | + </Text> |
| 126 | + <Tooltip |
| 127 | + maxWidth={250} |
| 128 | + content={ |
| 129 | + <Translation |
| 130 | + id="TR_STAKE_REWARDS_TOOLTIP" |
| 131 | + values={{ count: SOLANA_EPOCH_DAYS }} |
| 132 | + /> |
| 133 | + } |
| 134 | + > |
| 135 | + <Badge size="small"> |
| 136 | + <Row gap={spacings.xxs} alignItems="center"> |
| 137 | + <Translation |
| 138 | + id="TR_STAKE_REWARDS_BAGE" |
| 139 | + values={{ count: reward.epoch }} |
| 140 | + /> |
| 141 | + <Icon name="info" size="small" /> |
| 142 | + </Row> |
| 143 | + </Badge> |
| 144 | + </Tooltip> |
| 145 | + </Column> |
| 146 | + </Row> |
| 147 | + {reward?.amount && ( |
| 148 | + <Column alignItems="end"> |
| 149 | + <HiddenPlaceholder> |
| 150 | + <CoinBalance |
| 151 | + value={formatNetworkAmount( |
| 152 | + reward?.amount, |
| 153 | + account.symbol, |
| 154 | + )} |
| 155 | + symbol={account.symbol} |
| 156 | + /> |
| 157 | + </HiddenPlaceholder> |
| 158 | + <HiddenPlaceholder> |
| 159 | + <Text typographyStyle="hint" variant="tertiary"> |
| 160 | + <FiatValue |
| 161 | + amount={formatNetworkAmount( |
| 162 | + reward?.amount, |
| 163 | + account.symbol, |
| 164 | + )} |
| 165 | + symbol={account.symbol} |
| 166 | + /> |
| 167 | + </Text> |
| 168 | + </HiddenPlaceholder> |
| 169 | + </Column> |
| 170 | + )} |
| 171 | + </Row> |
| 172 | + </Card> |
| 173 | + </React.Fragment> |
| 174 | + ))} |
| 175 | + </> |
| 176 | + )} |
| 177 | + |
| 178 | + {showPagination && ( |
| 179 | + <Pagination |
| 180 | + hasPages={true} |
| 181 | + currentPage={currentPage} |
| 182 | + isLastPage={isLastPage} |
| 183 | + perPage={perPage} |
| 184 | + totalItems={totalItems} |
| 185 | + onPageSelected={onPageSelected} |
| 186 | + /> |
| 187 | + )} |
| 188 | + </DashboardSection> |
| 189 | + ); |
| 190 | +}; |
0 commit comments