mango-v4-ui/components/trade/MarketLogos.tsx

143 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-10-26 03:49:05 -07:00
import { Serum3Market, PerpMarket } from '@blockworks-foundation/mango-v4'
2022-11-18 11:11:06 -08:00
import useJupiterMints from 'hooks/useJupiterMints'
2022-09-26 04:53:49 -07:00
import { QuestionMarkCircleIcon } from '@heroicons/react/20/solid'
2022-10-28 14:46:38 -07:00
import Image from 'next/legacy/image'
2022-10-02 20:14:26 -07:00
import { useMemo } from 'react'
2022-11-20 12:32:38 -08:00
import useMangoGroup from 'hooks/useMangoGroup'
2022-12-09 14:58:49 -08:00
import LogoWithFallback from '@components/shared/LogoWithFallback'
2023-07-04 21:40:47 -07:00
import { CUSTOM_TOKEN_ICONS } from 'utils/constants'
2022-09-26 04:53:49 -07:00
2022-11-29 20:01:55 -08:00
const MarketLogos = ({
market,
2023-03-16 02:38:17 -07:00
size = 'medium',
2022-11-29 20:01:55 -08:00
}: {
market: Serum3Market | PerpMarket
2023-09-10 23:15:50 -07:00
size?: 'xs' | 'small' | 'medium' | 'large'
2022-11-29 20:01:55 -08:00
}) => {
2022-11-20 12:32:38 -08:00
const { group } = useMangoGroup()
2022-11-18 11:11:06 -08:00
const { mangoTokens } = useJupiterMints()
2022-10-02 20:14:26 -07:00
const logos = useMemo(() => {
2022-11-18 11:11:06 -08:00
if (!group || !mangoTokens.length || !market)
2022-10-02 20:14:26 -07:00
return { baseLogoURI: '', quoteLogoURI: '' }
2023-07-02 18:04:46 -07:00
let baseLogoURI, quoteLogoURI
2022-10-10 19:16:13 -07:00
if (market instanceof Serum3Market) {
const baseBank = group.getFirstBankByTokenIndex(market.baseTokenIndex)
const quoteBank = group.getFirstBankByTokenIndex(market.quoteTokenIndex)
2023-07-04 21:40:47 -07:00
const baseSymbol = baseBank.name.toLowerCase()
const quoteSymbol = quoteBank.name.toLowerCase()
const hasCustomBaseIcon = CUSTOM_TOKEN_ICONS[baseSymbol]
const hasCustomQuoteIcon = CUSTOM_TOKEN_ICONS[quoteSymbol]
2022-10-10 19:16:13 -07:00
2023-07-02 18:04:46 -07:00
const jupiterBaseToken = mangoTokens.find(
2023-07-21 11:47:53 -07:00
(t) => t.address === baseBank.mint.toString(),
2022-10-10 19:16:13 -07:00
)
2023-07-02 18:04:46 -07:00
const jupiterQuoteToken = mangoTokens.find(
2023-07-21 11:47:53 -07:00
(t) => t.address === quoteBank.mint.toString(),
2022-10-10 19:16:13 -07:00
)
2023-07-02 18:04:46 -07:00
2023-07-04 21:40:47 -07:00
baseLogoURI = hasCustomBaseIcon
? `/icons/${baseSymbol}.svg`
: jupiterBaseToken?.logoURI
quoteLogoURI = hasCustomQuoteIcon
? `/icons/${quoteSymbol}.svg`
: jupiterQuoteToken?.logoURI
2022-10-10 19:16:13 -07:00
} else {
2023-07-04 21:40:47 -07:00
const marketName = market.name.split('-')[0].toLowerCase()
const hasCustomIcon = CUSTOM_TOKEN_ICONS[marketName]
2023-07-02 18:04:46 -07:00
const jupiterBaseToken =
mangoTokens.find((t) => t.symbol.toLowerCase() === marketName) ||
mangoTokens.find((t) => t.symbol.toLowerCase()?.includes(marketName))
2023-07-04 21:40:47 -07:00
baseLogoURI = hasCustomIcon
? `/icons/${marketName}.svg`
: jupiterBaseToken?.logoURI
2022-10-10 19:16:13 -07:00
}
2022-10-02 20:14:26 -07:00
return {
baseLogoURI,
quoteLogoURI,
2022-12-09 14:58:49 -08:00
name: market.name.split(/-|\//)[0],
2022-10-02 20:14:26 -07:00
}
2022-11-18 11:11:06 -08:00
}, [group, mangoTokens, market])
2022-10-10 19:16:13 -07:00
2023-09-10 23:15:50 -07:00
const pxSize =
size === 'xs'
? '12'
: size === 'small'
? '16'
: size === 'large'
? '24'
: '20'
2023-03-16 02:38:17 -07:00
2022-09-26 04:53:49 -07:00
return (
2022-10-26 03:49:05 -07:00
<div
2023-03-16 02:38:17 -07:00
className={`relative ${
2023-09-10 23:15:50 -07:00
size === 'xs'
? 'h-[12px]'
: size === 'small'
2023-03-16 02:38:17 -07:00
? 'mr-1.5 h-4'
: size === 'large'
2023-10-17 04:28:43 -07:00
? 'mr-3 h-6'
2023-03-16 02:38:17 -07:00
: 'mr-2 h-5'
} ${
2022-11-29 20:01:55 -08:00
market instanceof Serum3Market
2023-09-10 23:15:50 -07:00
? size === 'xs'
? 'w-[23px]'
: size === 'small'
2022-11-29 20:01:55 -08:00
? 'w-[27px]'
2023-03-16 02:38:17 -07:00
: size === 'large'
? 'w-[40px]'
2022-11-29 20:01:55 -08:00
: 'w-[34px]'
2023-09-10 23:15:50 -07:00
: size === 'xs'
? 'w-[12px]'
2023-03-16 02:38:17 -07:00
: size === 'small'
2022-11-29 20:01:55 -08:00
? 'w-[16px]'
2023-03-16 02:38:17 -07:00
: size === 'large'
? 'w-[24px]'
2022-11-29 20:01:55 -08:00
: 'w-[20px]'
2023-09-11 21:17:59 -07:00
} flex-shrink-0`}
2022-10-26 03:49:05 -07:00
>
2023-07-06 05:08:10 -07:00
<div className="absolute left-0 top-0 z-10 rounded-full bg-th-bkg-2">
2022-12-09 14:58:49 -08:00
<LogoWithFallback
alt=""
2023-07-02 18:46:30 -07:00
className="flex-shrink-0"
2023-03-16 02:38:17 -07:00
width={pxSize}
height={pxSize}
2022-12-09 15:08:15 -08:00
src={logos.baseLogoURI || `/icons/${logos?.name?.toLowerCase()}.svg`}
2023-03-16 02:38:17 -07:00
fallback={<FallbackIcon size={size} />}
2022-12-09 14:58:49 -08:00
/>
2022-09-26 04:53:49 -07:00
</div>
<div className="absolute right-0 top-0">
2022-10-10 19:16:13 -07:00
{logos.quoteLogoURI && market instanceof Serum3Market ? (
2022-09-26 04:53:49 -07:00
<Image
alt=""
2022-12-15 20:45:01 -08:00
className="flex-shrink-0 opacity-60"
2023-03-16 02:38:17 -07:00
width={pxSize}
height={pxSize}
2022-10-02 20:14:26 -07:00
src={logos.quoteLogoURI}
2022-09-26 04:53:49 -07:00
/>
2022-10-26 03:49:05 -07:00
) : market instanceof PerpMarket ? null : (
2023-03-16 02:38:17 -07:00
<FallbackIcon size={size} />
2022-09-26 04:53:49 -07:00
)}
</div>
</div>
)
}
export default MarketLogos
2023-03-16 02:38:17 -07:00
2023-09-10 23:15:50 -07:00
const FallbackIcon = ({
size,
}: {
size: 'xs' | 'small' | 'medium' | 'large'
}) => {
2023-03-16 02:38:17 -07:00
return (
<QuestionMarkCircleIcon
className={`${
size === 'small' ? 'h-4 w-4' : size === 'large' ? 'h-6 w-6' : 'h-5 w-5'
} flex-shrink-0 text-th-fgd-3`}
/>
)
}