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

113 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-07-10 19:01:16 -07:00
import { Dispatch, SetStateAction } from 'react'
2022-11-18 11:11:06 -08:00
import { RouteInfo, Token } from '../../types/jupiter'
2022-07-05 20:37:49 -07:00
import Modal from '../shared/Modal'
2022-11-18 11:11:06 -08:00
import useJupiterMints from '../../hooks/useJupiterMints'
2023-01-24 17:12:13 -08:00
import FormatNumericValue from '@components/shared/FormatNumericValue'
2022-07-05 20:37:49 -07:00
type RoutesModalProps = {
2022-07-10 19:01:16 -07:00
onClose: () => void
2023-02-04 13:55:42 -08:00
setSelectedRoute: Dispatch<SetStateAction<RouteInfo | undefined | null>>
2022-07-05 20:37:49 -07:00
show: boolean
routes: RouteInfo[]
selectedRoute: RouteInfo
inputTokenSymbol: string
2022-08-02 11:04:00 -07:00
outputTokenInfo: Token
2022-07-05 20:37:49 -07:00
}
const RoutesModal = ({
onClose,
show,
routes,
selectedRoute,
2022-07-10 19:01:16 -07:00
setSelectedRoute,
2022-07-05 20:37:49 -07:00
inputTokenSymbol,
outputTokenInfo,
}: RoutesModalProps) => {
2022-12-15 14:51:19 -08:00
const { jupiterTokens } = useJupiterMints()
2022-07-10 19:01:16 -07:00
const handleSelectRoute = (route: RouteInfo) => {
setSelectedRoute(route)
onClose()
}
2022-07-05 20:37:49 -07:00
return (
2022-07-10 19:01:16 -07:00
<Modal isOpen={show} onClose={() => onClose()}>
2022-07-05 20:37:49 -07:00
<div className="mb-4 text-center text-lg font-bold text-th-fgd-1">
{routes?.length} routes found
</div>
<div className="thin-scroll max-h-96 overflow-y-auto overflow-x-hidden">
{routes?.map((route, index) => {
2022-11-18 11:11:06 -08:00
const selected = selectedRoute.outAmount === route.outAmount
2022-07-05 20:37:49 -07:00
return (
<div
key={index}
2023-04-19 18:12:45 -07:00
className={`mb-2 rounded border bg-th-bkg-3 hover:bg-th-bkg-4 ${
2022-07-05 20:37:49 -07:00
selected
2022-11-30 19:32:32 -08:00
? 'border-th-active text-th-active hover:border-th-active'
2022-07-05 20:37:49 -07:00
: 'border-transparent text-th-fgd-1'
}`}
>
<button
className="w-full p-4"
2022-07-10 19:01:16 -07:00
onClick={() => handleSelectRoute(route)}
2022-07-05 20:37:49 -07:00
>
<div className="flex items-center justify-between">
<div className="flex flex-col text-left">
2023-03-08 12:40:33 -08:00
<div className="overflow-ellipsis font-bold">
2022-07-05 20:37:49 -07:00
{route.marketInfos.map((info, index) => {
let includeSeparator = false
if (
route.marketInfos.length > 1 &&
index !== route.marketInfos.length - 1
) {
includeSeparator = true
}
return (
2022-11-18 11:11:06 -08:00
<span key={index}>{`${info.label} ${
2022-07-05 20:37:49 -07:00
includeSeparator ? 'x ' : ''
}`}</span>
)
})}
</div>
2022-07-12 20:58:13 -07:00
<div className="text-xs text-th-fgd-4">
2022-07-05 20:37:49 -07:00
{inputTokenSymbol} {' '}
{route.marketInfos.map((r, index) => {
const showArrow =
index !== route.marketInfos.length - 1 ? true : false
return (
<span key={index}>
<span>
{
2022-12-15 14:51:19 -08:00
jupiterTokens.find(
2022-07-05 20:37:49 -07:00
(item) =>
item?.address === r?.outputMint?.toString()
)?.symbol
}
</span>
{showArrow ? ' → ' : ''}
</span>
)
})}
</div>
</div>
<div className="text-lg">
2023-01-24 17:12:13 -08:00
<FormatNumericValue
value={
route.outAmount / 10 ** (outputTokenInfo?.decimals || 1)
}
decimals={outputTokenInfo?.decimals || 6}
/>
2022-07-05 20:37:49 -07:00
</div>
</div>
</button>
</div>
)
})}
</div>
</Modal>
)
}
export default RoutesModal