import { Bank } from '@blockworks-foundation/mango-v4' import useJupiterMints from 'hooks/useJupiterMints' import { ArrowDownRightIcon, ArrowUpLeftIcon, NoSymbolIcon, QuestionMarkCircleIcon, } from '@heroicons/react/20/solid' import useMangoAccount from 'hooks/useMangoAccount' import { useViewport } from 'hooks/useViewport' import { useTranslation } from 'next-i18next' import Image from 'next/legacy/image' import { formatNumericValue } from 'utils/numbers' import { breakpoints } from 'utils/theme' import { Table, Td, Th, TrBody, TrHead } from '../shared/TableElements' import useMangoGroup from 'hooks/useMangoGroup' import ConnectEmptyState from '../shared/ConnectEmptyState' import { useWallet } from '@solana/wallet-adapter-react' import Decimal from 'decimal.js' import { getMaxWithdrawForBank } from '@components/swap/useTokenMax' import { IconButton } from '@components/shared/Button' import { useCallback, useState } from 'react' import BorrowRepayModal from '@components/modals/BorrowRepayModal' import Tooltip from '@components/shared/Tooltip' import BankAmountWithValue from '@components/shared/BankAmountWithValue' import { BankWithBalance } from 'hooks/useBanksWithBalances' const YourBorrowsTable = ({ banks }: { banks: BankWithBalance[] }) => { const { t } = useTranslation(['common', 'trade']) const [showBorrowModal, setShowBorrowModal] = useState(false) const [showRepayModal, setShowRepayModal] = useState(false) const [selectedToken, setSelectedToken] = useState('') const { mangoAccount, mangoAccountAddress } = useMangoAccount() const { group } = useMangoGroup() const { mangoTokens } = useJupiterMints() const { width } = useViewport() const { connected } = useWallet() const showTableView = width ? width > breakpoints.md : false const handleShowActionModals = useCallback( (token: string, action: 'borrow' | 'repay') => { setSelectedToken(token) action === 'borrow' ? setShowBorrowModal(true) : setShowRepayModal(true) }, [] ) return ( <> {banks?.length ? ( showTableView ? ( {banks.map((b) => { const bank: Bank = b.bank let logoURI if (mangoTokens.length) { logoURI = mangoTokens.find( (t) => t.address === bank.mint.toString() )?.logoURI } const available = b.maxBorrow const borrowedAmount = b.borrowedAmount return ( ) })}
{t('token')} {t('borrow:borrowed-amount')}
{t('available')}
{t('rate')}
{logoURI ? ( ) : ( )}
{bank.name}
{formatNumericValue(bank.getBorrowRateUi(), 2)}%
handleShowActionModals(bank.name, 'repay') } size="small" > handleShowActionModals(bank.name, 'borrow') } size="small" >
) : ( banks.map((b) => { const bank: Bank = b.bank let logoURI if (mangoTokens.length) { logoURI = mangoTokens.find( (t) => t.address === bank.mint.toString() )?.logoURI } const available = group && mangoAccount ? getMaxWithdrawForBank(group, bank, mangoAccount, true) : new Decimal(0) const borrowedAmount = mangoAccount ? Math.abs(mangoAccount.getTokenBalanceUi(bank)) : 0 return (
{logoURI ? ( ) : ( )}

{bank.name}

{t('borrow:borrowed-amount')}

{t('rate')}

{formatNumericValue(bank.getBorrowRateUi(), 2)}%

handleShowActionModals(bank.name, 'repay') } size="medium" > handleShowActionModals(bank.name, 'borrow') } size="medium" >
) }) ) ) : mangoAccountAddress || connected ? (

{t('borrow:no-borrows')}

) : (
)} {showBorrowModal ? ( setShowBorrowModal(false)} token={selectedToken} /> ) : null} {showRepayModal ? ( setShowRepayModal(false)} token={selectedToken} /> ) : null} ) } export default YourBorrowsTable