|
| 1 | +import { Select, SelectItem, SelectContent, SelectTrigger, SelectValue } from '@/components/ui/select'; |
| 2 | +import { useSafeAccountBalance } from '@/features/safe-sdk/hooks/use-safe-account-balance'; |
| 3 | +import { formatEther } from 'viem'; |
| 4 | + |
| 5 | +interface AssertSelectProps { |
| 6 | + value: string; |
| 7 | + onChange: (value: string) => void; |
| 8 | +} |
| 9 | + |
| 10 | +export function AssertSelect({ value, onChange }: AssertSelectProps) { |
| 11 | + const { balances } = useSafeAccountBalance(); |
| 12 | + |
| 13 | + const getTokenInfo = (balance: any) => { |
| 14 | + if (balance.tokenAddress === null && balance.token === null) { |
| 15 | + return { |
| 16 | + address: 'eth', |
| 17 | + name: 'ETH', |
| 18 | + symbol: 'ETH', |
| 19 | + logoUri: null, |
| 20 | + }; |
| 21 | + } |
| 22 | + |
| 23 | + return { |
| 24 | + address: balance.tokenAddress, |
| 25 | + name: balance.token?.name || 'Unknown Token', |
| 26 | + symbol: balance.token?.symbol || '???', |
| 27 | + logoUri: balance.token?.logoUri, |
| 28 | + }; |
| 29 | + }; |
| 30 | + |
| 31 | + const formatBalance = (balance: string) => { |
| 32 | + try { |
| 33 | + const formatted = formatEther(BigInt(balance)); |
| 34 | + return parseFloat(formatted) |
| 35 | + .toFixed(6) |
| 36 | + .replace(/\.?0+$/, ''); |
| 37 | + } catch (error) { |
| 38 | + console.warn('Error formatting balance:', error); |
| 39 | + return '0'; |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <Select value={value} onValueChange={onChange}> |
| 45 | + <SelectTrigger className='w-full'> |
| 46 | + <SelectValue placeholder='Select asset' /> |
| 47 | + </SelectTrigger> |
| 48 | + <SelectContent> |
| 49 | + {balances.map((balance, index) => { |
| 50 | + const tokenInfo = getTokenInfo(balance); |
| 51 | + const formattedBalance = formatBalance(balance.balance); |
| 52 | + return ( |
| 53 | + <SelectItem key={tokenInfo.address || index} value={tokenInfo.address}> |
| 54 | + <div className='flex items-center gap-2'> |
| 55 | + <div> |
| 56 | + <div className='font-medium'>{tokenInfo.symbol}</div> |
| 57 | + <div className='text-xs text-muted-foreground'> |
| 58 | + {tokenInfo.name !== tokenInfo.symbol && <span>{tokenInfo.name} • </span>} |
| 59 | + Balance: {formattedBalance} |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + </SelectItem> |
| 64 | + ); |
| 65 | + })} |
| 66 | + </SelectContent> |
| 67 | + </Select> |
| 68 | + ); |
| 69 | +} |
0 commit comments