import { ColumnDef, flexRender, getCoreRowModel, getSortedRowModel, SortingState, useReactTable, } from '@tanstack/react-table' import classNames from 'classnames' import { useMemo, useState } from 'react' import { useLocation, useNavigate } from 'react-router-dom' import { getAmountChangeColor } from 'components/Account/AccountBalancesTable/functions' import useAccountBalanceData from 'components/Account/AccountBalancesTable/useAccountBalanceData' import AccountFundFullPage from 'components/Account/AccountFund/AccountFundFullPage' import AssetRate from 'components/Asset/AssetRate' import ActionButton from 'components/Button/ActionButton' import DisplayCurrency from 'components/DisplayCurrency' import { FormattedNumber } from 'components/FormattedNumber' import { SortAsc, SortDesc, SortNone } from 'components/Icons' import Text from 'components/Text' import { ASSETS } from 'constants/assets' import { MAX_AMOUNT_DECIMALS, MIN_AMOUNT } from 'constants/math' import { ORACLE_DENOM } from 'constants/oracle' import useCurrentAccount from 'hooks/useCurrentAccount' import useMarketAssets from 'hooks/useMarketAssets' import useStore from 'store' import { BNCoin } from 'types/classes/BNCoin' import { byDenom } from 'utils/array' import { getAssetByDenom } from 'utils/assets' import { demagnify, formatAmountToPrecision } from 'utils/formatters' import { getPage, getRoute } from 'utils/route' interface Props { account: Account lendingData: LendingMarketTableData[] borrowingData: BorrowMarketTableData[] tableBodyClassName?: string } export default function Index(props: Props) { const { account, lendingData, borrowingData, tableBodyClassName } = props const { data: markets } = useMarketAssets() const currentAccount = useCurrentAccount() const navigate = useNavigate() const { pathname } = useLocation() const address = useStore((s) => s.address) const baseCurrency = useStore((s) => s.baseCurrency) const [sorting, setSorting] = useState([]) const updatedAccount = useStore((s) => s.updatedAccount) const accountBalanceData = useAccountBalanceData({ account, updatedAccount, lendingData, borrowingData, }) const columns = useMemo[]>( () => [ { header: 'Asset', accessorKey: 'symbol', id: 'symbol', cell: ({ row }) => { return ( {row.original.symbol} {row.original.type === 'lending' && (lent)} {row.original.type === 'vault' && (farm)} ) }, }, { header: 'Value', accessorKey: 'value', id: 'value', cell: ({ row }) => { const color = getAmountChangeColor(row.original.type, row.original.amountChange) const coin = new BNCoin({ denom: ORACLE_DENOM, amount: row.original.value.toString(), }) return }, }, { id: 'size', accessorKey: 'size', header: 'Size', cell: ({ row }) => { const asset = getAssetByDenom(row.original.denom) if (row.original.type === 'vault' || !asset) return

const color = getAmountChangeColor(row.original.type, row.original.amountChange) const className = classNames('text-xs text-right', color) const amount = demagnify( row.original.amount, getAssetByDenom(row.original.denom) ?? ASSETS[0], ) if (amount >= 1) return ( ) const formattedAmount = formatAmountToPrecision(amount, MAX_AMOUNT_DECIMALS) return ( ) }, }, { id: 'apy', accessorKey: 'apy', header: 'APY', cell: ({ row }) => { if (row.original.type === 'deposits') return const isEnabled = markets.find(byDenom(row.original.denom))?.borrowEnabled ?? false return ( ) }, }, ], [markets], ) const table = useReactTable({ data: accountBalanceData, columns, state: { sorting, }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), }) if (accountBalanceData.length === 0) return (
{ if (currentAccount?.id !== account.id) { navigate(getRoute(getPage(pathname), address, account.id)) } useStore.setState({ focusComponent: { component: , onClose: () => { useStore.setState({ getStartedModal: true }) }, }, }) }} />
) return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => { return ( ) })} ))} {table.getRowModel().rows.map((row) => { return ( {row.getVisibleCells().map((cell) => { const borderClass = cell.row.original.type === 'borrowing' ? 'border-loss' : 'border-profit' return ( ) })} ) })}
{header.column.getCanSort() ? { asc: , desc: , false: , }[header.column.getIsSorted() as string] ?? null : null} {flexRender(header.column.columnDef.header, header.getContext())}
{flexRender(cell.column.columnDef.cell, cell.getContext())}
) }