mango-v4-ui/components/swap/TokenSelect.tsx

66 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-07-04 21:40:47 -07:00
import { ChevronDownIcon } from '@heroicons/react/20/solid'
2022-11-18 11:11:06 -08:00
import useMangoGroup from 'hooks/useMangoGroup'
2022-11-21 20:20:05 -08:00
import { Bank } from '@blockworks-foundation/mango-v4'
2023-09-06 20:51:57 -07:00
import { Dispatch, SetStateAction, useMemo } from 'react'
2023-04-16 18:04:13 -07:00
import { formatTokenSymbol } from 'utils/tokens'
2023-07-04 21:40:47 -07:00
import TokenLogo from '@components/shared/TokenLogo'
2023-09-06 06:06:57 -07:00
import { SwapFormTokenListType } from './SwapFormTokenList'
import useMangoAccount from 'hooks/useMangoAccount'
2023-09-06 20:51:57 -07:00
import { useTranslation } from 'react-i18next'
2022-07-05 20:37:49 -07:00
type TokenSelectProps = {
2022-11-21 20:20:05 -08:00
bank: Bank | undefined
2023-09-06 06:06:57 -07:00
showTokenList: Dispatch<SetStateAction<SwapFormTokenListType>>
2023-09-21 14:22:04 -07:00
tokenType: SwapFormTokenListType
2022-07-05 20:37:49 -07:00
}
2023-09-21 14:22:04 -07:00
const TokenSelect = ({ bank, showTokenList, tokenType }: TokenSelectProps) => {
2023-09-06 20:51:57 -07:00
const { t } = useTranslation('trade')
2022-11-18 11:11:06 -08:00
const { group } = useMangoGroup()
2023-09-06 06:06:57 -07:00
const { mangoAccount } = useMangoAccount()
2023-09-06 20:51:57 -07:00
const posType = useMemo(() => {
2023-09-21 14:22:04 -07:00
if (!bank || !mangoAccount || tokenType !== 'reduce-input') return ''
2023-09-06 06:06:57 -07:00
const uiPos = mangoAccount.getTokenBalanceUi(bank)
if (uiPos > 0) {
2023-09-06 20:51:57 -07:00
return 'long'
2023-09-06 06:06:57 -07:00
} else if (uiPos < 0) {
2023-09-06 20:51:57 -07:00
return 'short'
2023-09-06 06:06:57 -07:00
}
2023-09-21 14:22:04 -07:00
}, [bank, mangoAccount, tokenType])
2023-09-06 20:51:57 -07:00
if (!group) return null
2023-09-06 06:06:57 -07:00
2023-09-06 20:51:57 -07:00
return bank ? (
<button
2023-09-21 14:22:04 -07:00
onClick={() => showTokenList(tokenType)}
className="flex h-[56px] w-full items-center rounded-lg rounded-r-none bg-th-input-bkg px-3 py-2 text-th-fgd-2 focus-visible:bg-th-bkg-3 md:hover:cursor-pointer md:hover:bg-th-bkg-1 md:hover:text-th-fgd-1"
2022-07-22 23:15:51 -07:00
>
<div className="mr-2.5 flex min-w-[24px] items-center">
2023-07-04 21:40:47 -07:00
<TokenLogo bank={bank} />
</div>
2022-07-22 23:15:51 -07:00
<div className="flex w-full items-center justify-between">
2023-09-06 20:51:57 -07:00
<div className="flex items-center">
<div className="text-lg font-bold text-th-fgd-1">
{formatTokenSymbol(bank.name)}
</div>
{posType ? (
<span
className={`ml-2 inline-block rounded border px-1 text-xs uppercase ${
posType === 'long'
? 'border-th-up text-th-up'
: 'border-th-down text-th-down'
}`}
>
{t(`trade:${posType}`)}
</span>
) : null}
2023-04-16 18:04:13 -07:00
</div>
2022-07-22 23:15:51 -07:00
<ChevronDownIcon className="h-6 w-6" />
</div>
</button>
2023-09-06 20:51:57 -07:00
) : null
2022-07-05 20:37:49 -07:00
}
export default TokenSelect