import { ColumnDef, flexRender, getCoreRowModel, getSortedRowModel, SortingState, useReactTable, } from '@tanstack/react-table' import Image from 'next/image' import React from 'react' import ChevronUpIcon from 'components/Icons/collapse.svg' import ChevronDownIcon from 'components/Icons/expand.svg' 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 } 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