1
0
Fork 0
blog/src/features/Web3/components/TokenSelect/Token.tsx

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-10-28 13:55:30 +02:00
import { forwardRef, type HTMLAttributes } from 'react'
import * as Select from '@radix-ui/react-select'
import { formatCurrency } from '@coingecko/cryptoformat'
import './Token.css'
import { Icon as Check } from '@images/components/react/Check'
2023-11-05 16:31:09 +01:00
import type { GetToken } from '@features/Web3/hooks/useFetchTokens'
2023-10-28 13:55:30 +02:00
interface SelectItemProps extends HTMLAttributes<HTMLDivElement> {
2023-10-28 23:34:58 +02:00
token: GetToken | undefined
2023-10-28 13:55:30 +02:00
}
export const Token = forwardRef<HTMLDivElement, SelectItemProps>(
({ className, token, ...props }, forwardedRef) => {
2023-11-07 21:17:32 +01:00
const locale = window?.navigator?.language || 'en'
2023-10-28 13:55:30 +02:00
const balance =
2023-10-28 23:34:58 +02:00
token?.balance && token?.symbol
2023-11-07 21:17:32 +01:00
? formatCurrency(token.balance, token.symbol, locale, false, {
2023-10-28 13:55:30 +02:00
decimalPlaces: 3,
significantFigures: 3
})
: 0
2023-10-28 18:08:46 +02:00
const valueInUsd =
2023-10-28 23:34:58 +02:00
token?.balance && token?.price?.usd
? token?.balance * token?.price.usd
: 0
2023-11-07 21:17:32 +01:00
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', locale)
2023-10-28 18:08:46 +02:00
2024-03-12 22:20:40 +01:00
// const hasBalanceAndValue =
// balance && parseInt(balance) !== 0 && valueInUsd >= 1
const hasBalance = balance && parseInt(balance) !== 0
return hasBalance ? (
2023-10-28 13:55:30 +02:00
<Select.Item
2023-10-28 18:08:46 +02:00
className={`${className ? className : ''} Token`}
2023-10-28 13:55:30 +02:00
{...props}
2023-10-28 23:34:58 +02:00
value={token?.address || ''}
title={token?.address}
2023-10-28 13:55:30 +02:00
ref={forwardedRef}
>
2023-10-28 18:08:46 +02:00
<Select.ItemText>
<span className="TokenLogo">
2023-10-28 23:34:58 +02:00
{token?.logo ? (
2024-03-14 14:18:21 +01:00
<img
src={token.logo}
width="32"
height="32"
className="TokenLogoImage"
/>
2023-10-28 19:01:36 +02:00
) : (
2023-10-28 23:34:58 +02:00
token?.symbol?.substring(0, 3)
2023-10-28 19:01:36 +02:00
)}
2024-03-14 14:18:21 +01:00
<img
src={token?.chainLogo}
width="20"
height="20"
className="TokenChainLogo"
/>
2023-10-28 18:08:46 +02:00
</span>
</Select.ItemText>
<div>
2023-10-28 23:34:58 +02:00
<h3 className="TokenName">{token?.name}</h3>
2023-10-28 18:08:46 +02:00
<p className="TokenBalance">{balance}</p>
2023-10-28 13:55:30 +02:00
</div>
2024-03-12 22:20:40 +01:00
{valueInUsd ? (
<div className="TokenValue">{valueInUsdFormatted}</div>
) : null}
2023-10-28 18:08:46 +02:00
2023-10-28 13:55:30 +02:00
<Select.ItemIndicator className="SelectItemIndicator">
<Check />
</Select.ItemIndicator>
</Select.Item>
) : null
}
)