|
| 1 | +import { FC } from 'react' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import { |
| 4 | + Account, |
| 5 | + isAccountEmpty, |
| 6 | + RuntimeAccount, |
| 7 | + useGetConsensusAccountsAddress, |
| 8 | + useGetRuntimeAccountsAddress, |
| 9 | +} from '../../../oasis-nexus/api' |
| 10 | +import { getLayerLabels } from '../../utils/content' |
| 11 | +import { RouteUtils } from '../../utils/route-utils' |
| 12 | +import { Link as RouterLink } from 'react-router-dom' |
| 13 | +import { Link } from '@oasisprotocol/ui-library/src/components/link' |
| 14 | +import { Tooltip } from '@oasisprotocol/ui-library/src/components/tooltip' |
| 15 | +import { RuntimeBalanceDisplay } from '../Balance/RuntimeBalanceDisplay' |
| 16 | +import { RoundedBalance } from '../RoundedBalance' |
| 17 | + |
| 18 | +/** Ignoring pontus-x and cipher */ |
| 19 | +export const BalancesOnOtherLayers: FC<{ account: Account | RuntimeAccount }> = ({ account }) => { |
| 20 | + const { t } = useTranslation() |
| 21 | + const layerLabels = getLayerLabels(t) |
| 22 | + |
| 23 | + const queries = [ |
| 24 | + useGetConsensusAccountsAddress(account.network, account.address), |
| 25 | + useGetRuntimeAccountsAddress(account.network, 'sapphire', account.address), |
| 26 | + useGetRuntimeAccountsAddress(account.network, 'emerald', account.address), |
| 27 | + ] |
| 28 | + |
| 29 | + const activeOnLayers = queries |
| 30 | + .filter(query => query.data?.data.layer !== account.layer) |
| 31 | + .filter(query => query.data?.data && !isAccountEmpty(query.data.data)) |
| 32 | + .map((query, i) => { |
| 33 | + const account = query.data!.data |
| 34 | + |
| 35 | + // TODO: switch both to totalBalance when runtime API returns it |
| 36 | + const printBalance = |
| 37 | + account.layer === 'consensus' ? ( |
| 38 | + <> |
| 39 | + {t('account.totalBalance')}: <RoundedBalance value={account.total} ticker={account.ticker} /> |
| 40 | + </> |
| 41 | + ) : ( |
| 42 | + <> |
| 43 | + {t('account.availableBalance')}:{' '} |
| 44 | + <RuntimeBalanceDisplay balances={account.balances} scope={account} className="inline-block" /> |
| 45 | + </> |
| 46 | + ) |
| 47 | + |
| 48 | + return ( |
| 49 | + <span key={account.layer}> |
| 50 | + {i > 0 && ' and '} |
| 51 | + |
| 52 | + <Tooltip title={printBalance}> |
| 53 | + <Link asChild className="font-medium"> |
| 54 | + <RouterLink |
| 55 | + to={RouteUtils.getAccountRoute( |
| 56 | + account, |
| 57 | + (account as RuntimeAccount).address_eth ?? account.address, |
| 58 | + )} |
| 59 | + > |
| 60 | + {layerLabels[account.layer]} |
| 61 | + </RouterLink> |
| 62 | + </Link> |
| 63 | + </Tooltip> |
| 64 | + </span> |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + if (activeOnLayers.length <= 0) return null |
| 69 | + |
| 70 | + return ( |
| 71 | + <span className="text-muted-foreground"> |
| 72 | + ({`${t('account.hasBalancesOnOtherLayers')} `} {activeOnLayers}) |
| 73 | + </span> |
| 74 | + ) |
| 75 | +} |
0 commit comments