1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-06-28 16:48:00 +02:00
blog/src/features/Web3/components/TokenSelect/Token.tsx

58 lines
1.8 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 { Check } from '@images/components/react'
2023-10-30 20:37:33 +01:00
import type { GetToken } from '@features/Web3/stores/tokens'
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) => {
const balance =
2023-10-28 23:34:58 +02:00
token?.balance && token?.symbol
2023-10-28 13:55:30 +02:00
? formatCurrency(token.balance, token.symbol, 'en', false, {
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-10-28 18:08:46 +02:00
const valueInUsdFormatted = formatCurrency(valueInUsd, 'USD', 'en')
return balance && parseInt(balance) !== 0 && valueInUsd >= 1 ? (
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 ? (
2023-10-28 19:01:36 +02:00
<img src={token.logo} width="32" height="32" />
) : (
2023-10-28 23:34:58 +02:00
token?.symbol?.substring(0, 3)
2023-10-28 19:01:36 +02:00
)}
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>
2023-10-28 18:08:46 +02:00
<div className="TokenValue">{valueInUsdFormatted}</div>
2023-10-28 13:55:30 +02:00
<Select.ItemIndicator className="SelectItemIndicator">
<Check />
</Select.ItemIndicator>
</Select.Item>
) : null
}
)