import React from 'react' import Image from 'next/image' import { ColumnDef, flexRender, getCoreRowModel, getSortedRowModel, SortingState, useReactTable, } from '@tanstack/react-table' import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/solid' import { formatCurrency } from 'utils/formatters' import AssetRow from './AssetRow' interface Market { denom: string symbol: string icon: string chain: string borrowed: { amount: number value: number } | null borrowRate: number marketLiquidity: number } // const data = [ // { // denom: 'uosmo', // symbol: 'OSMO', // icon: '/tokens/osmo.svg', // chain: 'Osmosis', // borrowed: { // amount: 2.005494, // value: 2.2060434000000004, // }, // borrowRate: 0.1, // marketLiquidity: 1000000, // }, // { // denom: 'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2', // symbol: 'ATOM', // icon: '/tokens/atom.svg', // chain: 'Cosmos', // borrowed: null, // borrowRate: 0.25, // marketLiquidity: 1000, // }, // { // denom: 'uusdc', // symbol: 'USDC', // icon: '/tokens/atom.svg', // chain: 'Ethereum', // borrowed: { // amount: 100, // value: 99.9776, // }, // borrowRate: 0.35, // marketLiquidity: 333, // }, // ] type Props = { data: Market[] onBorrowClick: (denom: string) => void onRepayClick: (denom: string) => void } const BorrowTable = ({ data, onBorrowClick, onRepayClick }: Props) => { const [sorting, setSorting] = React.useState([]) const columns = React.useMemo[]>( () => [ { header: 'Asset', id: 'symbol', accessorFn: (row) => (
token
{row.symbol}
{row.chain}
), cell: (info) => info.getValue(), }, { accessorKey: 'borrowRate', header: 'Borrow Rate', accessorFn: (row) => (
{row.borrowRate ? `${(row.borrowRate * 100).toFixed(2)}%` : '-'}
), cell: (info) => info.getValue(), }, { accessorKey: 'age', header: 'Borrowed', accessorFn: (row) => (
{row.borrowed ? (
{row.borrowed.amount}
{formatCurrency(row.borrowed.value)}
) : ( '-' )}
), cell: (info) => info.getValue(), }, { accessorKey: 'marketLiquidity', header: 'Liquidity Available', }, { accessorKey: 'status', enableSorting: false, header: 'Manage', width: 150, cell: ({ row }) => (
{row.getIsExpanded() ? ( ) : ( )}
), }, ], [] ) const table = useReactTable({ data, columns, state: { sorting, }, onSortingChange: setSorting, getCoreRowModel: getCoreRowModel(), getSortedRowModel: getSortedRowModel(), debugTable: true, }) return (
{table.getHeaderGroups().map((headerGroup) => (
{headerGroup.headers.map((header) => { return (
{header.isPlaceholder ? null : (
{flexRender(header.column.columnDef.header, header.getContext())} {{ asc: ' 🔼', desc: ' 🔽', }[header.column.getIsSorted() as string] ?? null}
)}
) })}
))}
{table.getRowModel().rows.length === 0 ? (
No Data
) : ( table.getRowModel().rows.map((row) => { return ( onBorrowClick(row.original.denom)} onRepayClick={() => onRepayClick(row.original.denom)} /> ) }) )}
) } export default BorrowTable