mango-v4-ui/components/account/ActivityFeedTable.tsx

501 lines
18 KiB
TypeScript
Raw Normal View History

2022-11-24 18:39:14 -08:00
import { EXPLORERS } from '@components/settings/PreferredExplorerSettings'
2022-09-28 06:04:58 -07:00
import { IconButton, LinkButton } from '@components/shared/Button'
2022-09-28 20:35:34 -07:00
import SheenLoader from '@components/shared/SheenLoader'
2022-11-20 02:44:14 -08:00
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2022-09-27 23:17:06 -07:00
import Tooltip from '@components/shared/Tooltip'
2022-09-28 06:04:58 -07:00
import { Transition } from '@headlessui/react'
import {
ChevronDownIcon,
ChevronRightIcon,
LinkIcon,
2022-12-12 14:53:04 -08:00
NoSymbolIcon,
2022-09-28 06:04:58 -07:00
} from '@heroicons/react/20/solid'
import mangoStore, { LiquidationFeedItem } from '@store/mangoStore'
2022-09-27 23:17:06 -07:00
import dayjs from 'dayjs'
import useLocalStorageState from 'hooks/useLocalStorageState'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-09-28 06:04:58 -07:00
import { useViewport } from 'hooks/useViewport'
2022-09-27 23:17:06 -07:00
import { useTranslation } from 'next-i18next'
2022-10-28 14:46:38 -07:00
import Image from 'next/legacy/image'
2022-09-28 06:04:58 -07:00
import { Fragment, useCallback, useState } from 'react'
2022-09-27 23:17:06 -07:00
import { PREFERRED_EXPLORER_KEY } from 'utils/constants'
import { formatDecimal, formatFixedDecimals } from 'utils/numbers'
2022-09-28 06:04:58 -07:00
import { breakpoints } from 'utils/theme'
2022-09-27 23:17:06 -07:00
const ActivityFeedTable = ({
activityFeed,
handleShowActivityDetails,
2022-09-28 20:35:34 -07:00
params,
2022-09-27 23:17:06 -07:00
}: {
activityFeed: any
handleShowActivityDetails: (x: LiquidationFeedItem) => void
2022-09-29 20:22:55 -07:00
params: string
2022-09-27 23:17:06 -07:00
}) => {
const { t } = useTranslation(['common', 'activity'])
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-09-27 23:17:06 -07:00
const actions = mangoStore((s) => s.actions)
2022-09-28 20:35:34 -07:00
const loadActivityFeed = mangoStore((s) => s.activityFeed.loading)
2022-09-27 23:17:06 -07:00
const [offset, setOffset] = useState(0)
const [preferredExplorer] = useLocalStorageState(
PREFERRED_EXPLORER_KEY,
EXPLORERS[0]
)
2022-09-28 06:04:58 -07:00
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-09-27 23:17:06 -07:00
const handleShowMore = useCallback(() => {
const mangoAccount = mangoStore.getState().mangoAccount.current
2022-09-28 20:35:34 -07:00
const set = mangoStore.getState().set
set((s) => {
s.activityFeed.loading = true
})
2022-09-27 23:17:06 -07:00
if (!mangoAccount) return
setOffset(offset + 25)
2022-09-28 20:35:34 -07:00
actions.fetchActivityFeed(
mangoAccount.publicKey.toString(),
offset + 25,
2022-09-29 20:22:55 -07:00
params
2022-09-28 20:35:34 -07:00
)
}, [actions, offset, params])
2022-09-27 23:17:06 -07:00
const getCreditAndDebit = (activity: any) => {
const { activity_type } = activity
2022-09-28 06:04:58 -07:00
let credit = { value: '0', symbol: '' }
let debit = { value: '0', symbol: '' }
2022-09-27 23:17:06 -07:00
if (activity_type === 'liquidate_token_with_token') {
const { side, liab_amount, liab_symbol, asset_amount, asset_symbol } =
activity.activity_details
if (side === 'liqee') {
2022-09-28 06:04:58 -07:00
credit = { value: formatDecimal(liab_amount), symbol: liab_symbol }
debit = {
value: formatDecimal(asset_amount * -1),
symbol: asset_symbol,
}
2022-09-27 23:17:06 -07:00
} else {
2022-09-28 06:04:58 -07:00
credit = { value: formatDecimal(asset_amount), symbol: asset_symbol }
debit = { value: formatDecimal(liab_amount * -1), symbol: liab_symbol }
2022-09-27 23:17:06 -07:00
}
}
if (activity_type === 'deposit') {
const { symbol, quantity } = activity.activity_details
2022-09-28 06:04:58 -07:00
credit = { value: formatDecimal(quantity), symbol }
debit = { value: '0', symbol: '' }
2022-09-27 23:17:06 -07:00
}
if (activity_type === 'withdraw') {
const { symbol, quantity } = activity.activity_details
2022-09-28 06:04:58 -07:00
credit = { value: '0', symbol: '' }
debit = { value: formatDecimal(quantity * -1), symbol }
2022-09-27 23:17:06 -07:00
}
2022-11-29 02:33:15 -08:00
if (activity_type === 'swap') {
const {
swap_in_amount,
swap_in_symbol,
swap_out_amount,
swap_out_symbol,
} = activity.activity_details
credit = {
value: formatDecimal(swap_out_amount),
symbol: swap_out_symbol,
}
debit = {
value: formatDecimal(swap_in_amount * -1),
symbol: swap_in_symbol,
}
}
2022-11-29 15:15:25 -08:00
if (activity_type === 'perp_trade') {
const { perp_market, price, quantity } = activity.activity_details
credit = { value: quantity, symbol: perp_market }
debit = { value: formatDecimal(quantity * price * -1), symbol: 'USDC' }
}
2022-09-27 23:17:06 -07:00
return { credit, debit }
}
const getValue = (activity: any) => {
const { activity_type } = activity
let value = 0
if (activity_type === 'liquidate_token_with_token') {
const { side, liab_amount, liab_price, asset_amount, asset_price } =
activity.activity_details
if (side === 'liqee') {
value = asset_amount * asset_price - liab_amount * liab_price
} else {
value = liab_amount * liab_price - asset_amount * asset_price
}
}
if (activity_type === 'deposit' || activity_type === 'withdraw') {
const { usd_equivalent } = activity.activity_details
2022-09-28 06:04:58 -07:00
value =
activity_type === 'withdraw' ? usd_equivalent * -1 : usd_equivalent
2022-09-27 23:17:06 -07:00
}
2022-11-29 02:33:15 -08:00
if (activity_type === 'swap') {
2022-12-20 02:01:49 -08:00
const { swap_out_amount, swap_out_price_usd } = activity.activity_details
value = swap_out_amount * swap_out_price_usd
2022-11-29 02:33:15 -08:00
}
2022-11-29 15:15:25 -08:00
if (activity_type === 'perp_trade') {
const { maker_fee, price, quantity, taker_fee } =
activity.activity_details
value = (quantity * price + maker_fee + taker_fee) * -1
}
2022-09-27 23:17:06 -07:00
return value
}
2022-10-27 13:58:54 -07:00
return mangoAccount ? (
2022-09-28 20:35:34 -07:00
activityFeed.length || loadActivityFeed ? (
2022-09-27 23:17:06 -07:00
<>
2022-09-28 06:04:58 -07:00
{showTableView ? (
2022-11-20 02:44:14 -08:00
<Table className="min-w-full">
2022-09-28 06:04:58 -07:00
<thead>
2022-11-20 02:44:14 -08:00
<TrHead className="sticky top-0 z-10">
<Th className="bg-th-bkg-1 text-left">{t('date')}</Th>
<Th className="bg-th-bkg-1 text-right">
2022-09-28 06:04:58 -07:00
{t('activity:activity')}
2022-11-20 02:44:14 -08:00
</Th>
<Th className="bg-th-bkg-1 text-right">
2022-09-28 06:04:58 -07:00
{t('activity:credit')}
2022-11-20 02:44:14 -08:00
</Th>
<Th className="bg-th-bkg-1 text-right">
2022-09-28 06:04:58 -07:00
{t('activity:debit')}
2022-11-20 02:44:14 -08:00
</Th>
<Th className="bg-th-bkg-1 text-right">
2022-09-28 06:04:58 -07:00
{t('activity:activity-value')}
2022-11-20 02:44:14 -08:00
</Th>
<Th className="bg-th-bkg-1 text-right">{t('explorer')}</Th>
</TrHead>
2022-09-28 06:04:58 -07:00
</thead>
<tbody>
2022-11-19 11:20:36 -08:00
{activityFeed.map((activity: any) => {
2022-09-28 06:04:58 -07:00
const { activity_type, block_datetime } = activity
const { signature } = activity.activity_details
const isLiquidation =
activity_type === 'liquidate_token_with_token'
const activityName = isLiquidation
? 'liquidation'
: activity_type
const amounts = getCreditAndDebit(activity)
const value = getValue(activity)
return (
2022-11-20 02:44:14 -08:00
<TrBody
2022-09-28 06:04:58 -07:00
key={signature}
className={`default-transition text-sm hover:bg-th-bkg-2 ${
isLiquidation ? 'cursor-pointer' : ''
}`}
onClick={
isLiquidation
? () => handleShowActivityDetails(activity)
: undefined
}
>
2022-11-20 02:44:14 -08:00
<Td>
2022-09-28 06:04:58 -07:00
<p className="font-body tracking-wide">
{dayjs(block_datetime).format('ddd D MMM')}
</p>
<p className="text-xs text-th-fgd-3">
{dayjs(block_datetime).format('h:mma')}
</p>
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right">
2022-09-28 06:04:58 -07:00
{t(`activity:${activityName}`)}
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right font-mono">
2022-09-28 06:04:58 -07:00
{amounts.credit.value}{' '}
<span className="font-body tracking-wide text-th-fgd-3">
{amounts.credit.symbol}
</span>
2022-11-20 02:44:14 -08:00
</Td>
<Td className="text-right font-mono">
2022-09-28 06:04:58 -07:00
{amounts.debit.value}{' '}
<span className="font-body tracking-wide text-th-fgd-3">
{amounts.debit.symbol}
</span>
2022-11-20 02:44:14 -08:00
</Td>
<Td
2022-11-14 03:46:48 -08:00
className={`text-right font-mono ${
2022-12-20 02:01:49 -08:00
activityName === 'swap' || activityName === 'perp'
? 'text-th-fgd-2'
: value >= 0
? 'text-th-up'
: 'text-th-down'
2022-11-14 03:46:48 -08:00
}`}
>
2022-12-20 02:01:49 -08:00
{value > 0 &&
activityName !== 'swap' &&
activityName !== 'perp'
? '+'
: ''}
2022-09-28 06:04:58 -07:00
{formatFixedDecimals(value, true)}
2022-11-20 02:44:14 -08:00
</Td>
<Td>
2022-09-28 06:04:58 -07:00
{activity_type !== 'liquidate_token_with_token' ? (
<div className="flex items-center justify-end">
<Tooltip
content={`View on ${t(
`settings:${preferredExplorer.name}`
)}`}
placement="top-end"
2022-09-27 23:17:06 -07:00
>
2022-09-28 06:04:58 -07:00
<a
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
<div className="h-6 w-6">
<Image
alt=""
width="24"
height="24"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
</div>
</a>
</Tooltip>
</div>
) : (
<div className="flex items-center justify-end">
<ChevronRightIcon className="h-6 w-6 text-th-fgd-3" />
</div>
)}
2022-11-20 02:44:14 -08:00
</Td>
</TrBody>
2022-09-28 06:04:58 -07:00
)
})}
</tbody>
2022-11-20 02:44:14 -08:00
</Table>
2022-09-28 06:04:58 -07:00
) : (
<div>
2022-11-19 11:20:36 -08:00
{activityFeed.map((activity: any) => (
2022-09-28 06:04:58 -07:00
<MobileActivityFeedItem
activity={activity}
getValue={getValue}
key={activity.activity_details.signature}
/>
))}
</div>
)}
2022-09-28 20:35:34 -07:00
{loadActivityFeed ? (
2022-09-30 05:31:22 -07:00
<div className="mt-2 space-y-0.5">
2022-10-28 05:23:28 -07:00
{[...Array(4)].map((x, i) => (
2022-09-28 20:35:34 -07:00
<SheenLoader className="flex flex-1" key={i}>
2022-09-30 05:31:22 -07:00
<div className="h-16 w-full bg-th-bkg-2" />
2022-09-28 20:35:34 -07:00
</SheenLoader>
))}
</div>
) : null}
{activityFeed.length && activityFeed.length % 25 === 0 ? (
2022-09-27 23:17:06 -07:00
<div className="flex justify-center pt-6">
<LinkButton onClick={handleShowMore}>Show More</LinkButton>
</div>
) : null}
</>
) : (
<div className="flex flex-col items-center p-8">
2022-12-12 14:53:04 -08:00
<NoSymbolIcon className="mb-1 h-6 w-6 text-th-fgd-4" />
<p>{t('activity:no-activity')}</p>
2022-09-27 23:17:06 -07:00
</div>
)
) : (
<div className="flex flex-col items-center p-8">
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
2022-12-12 14:53:04 -08:00
<p>{t('activity:connect-activity')}</p>
2022-09-27 23:17:06 -07:00
</div>
)
}
export default ActivityFeedTable
2022-09-28 06:04:58 -07:00
const MobileActivityFeedItem = ({
activity,
getValue,
}: {
activity: any
getValue: (x: any) => number
}) => {
const { t } = useTranslation(['common', 'activity'])
const [expandActivityDetails, setExpandActivityDetails] = useState(false)
const [preferredExplorer] = useLocalStorageState(
PREFERRED_EXPLORER_KEY,
EXPLORERS[0]
)
const { activity_type, block_datetime } = activity
const { signature } = activity.activity_details
const isLiquidation = activity_type === 'liquidate_token_with_token'
2022-11-29 02:33:15 -08:00
const isSwap = activity_type === 'swap'
2022-11-29 15:15:25 -08:00
const isPerp = activity_type === 'perp_trade'
2022-09-28 06:04:58 -07:00
const activityName = isLiquidation ? 'liquidation' : activity_type
const value = getValue(activity)
return (
2022-11-29 02:33:15 -08:00
<div key={signature} className="border-b border-th-bkg-3 p-4">
2022-09-28 06:04:58 -07:00
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-th-fgd-1">
{dayjs(block_datetime).format('ddd D MMM')}
</p>
<p className="text-xs text-th-fgd-3">
{dayjs(block_datetime).format('h:mma')}
</p>
</div>
<div className="flex items-center space-x-4">
<div>
<p className="text-right text-xs">
{t(`activity:${activityName}`)}
</p>
<p className="text-right font-mono text-sm text-th-fgd-1">
{isLiquidation ? (
formatFixedDecimals(value, true)
2022-11-29 02:33:15 -08:00
) : isSwap ? (
<>
<span className="mr-1">
{activity.activity_details.swap_in_amount.toLocaleString(
undefined,
{ maximumFractionDigits: 6 }
)}
</span>
<span className="font-body tracking-wide text-th-fgd-3">
{activity.activity_details.swap_in_symbol}
</span>
<span className="mx-1 font-body text-th-fgd-3">for</span>
<span className="mr-1">
{activity.activity_details.swap_out_amount.toLocaleString(
undefined,
{ maximumFractionDigits: 6 }
)}
</span>
<span className="font-body tracking-wide text-th-fgd-3">
{activity.activity_details.swap_out_symbol}
</span>
</>
2022-11-29 15:15:25 -08:00
) : isPerp ? (
<>
<span
className={`mr-1 font-body ${
activity.activity_details.taker_side === 'bid'
2022-11-30 19:32:32 -08:00
? 'text-th-up'
: 'text-th-down'
2022-11-29 15:15:25 -08:00
}`}
>
{activity.activity_details.taker_side === 'bid'
? 'BUY'
: 'SELL'}
</span>
<span className="mr-1">
{activity.activity_details.quantity}
</span>
<span className="font-body text-th-fgd-3">
{activity.activity_details.perp_market}
</span>
</>
2022-09-28 06:04:58 -07:00
) : (
<>
<span className="mr-1">
{activity.activity_details.quantity}
</span>
<span className="font-body tracking-wide text-th-fgd-3">
{activity.activity_details.symbol}
</span>
</>
)}
</p>
</div>
{isLiquidation ? (
<IconButton
onClick={() => setExpandActivityDetails((prev) => !prev)}
>
<ChevronDownIcon
className={`${
expandActivityDetails ? 'rotate-180' : 'rotate-360'
} h-6 w-6 flex-shrink-0 text-th-fgd-1`}
/>
</IconButton>
) : (
<a
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
<div className="flex h-10 w-10 items-center justify-center">
<Image
alt=""
width="24"
height="24"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
</div>
</a>
)}
</div>
</div>
<Transition
appear={true}
show={expandActivityDetails}
as={Fragment}
enter="transition ease-in duration-200"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition ease-out"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="mt-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pt-4">
<div className="col-span-1">
<p className="mb-0.5 text-sm">{t('activity:asset-liquidated')}</p>
<p className="font-mono text-sm text-th-fgd-1">
{formatDecimal(activity.activity_details.asset_amount)}{' '}
<span className="font-body tracking-wide">
{activity.activity_details.asset_symbol}
</span>
<span className="ml-2 font-body tracking-wide text-th-fgd-3">
at
</span>{' '}
{formatFixedDecimals(activity.activity_details.asset_price, true)}
</p>
<p className="font-mono text-xs text-th-fgd-3">
{formatFixedDecimals(
activity.activity_details.asset_price *
activity.activity_details.asset_amount,
true
)}
</p>
</div>
<div className="col-span-1">
<p className="mb-0.5 text-sm">{t('activity:asset-returned')}</p>
<p className="font-mono text-sm text-th-fgd-1">
{formatDecimal(activity.activity_details.liab_amount)}{' '}
<span className="font-body tracking-wide">
{activity.activity_details.liab_symbol}
</span>
<span className="ml-2 font-body tracking-wide text-th-fgd-3">
at
</span>{' '}
{formatFixedDecimals(activity.activity_details.liab_price, true)}
</p>
<p className="font-mono text-xs text-th-fgd-3">
{formatFixedDecimals(
activity.activity_details.liab_price *
activity.activity_details.liab_amount,
true
)}
</p>
</div>
<div className="col-span-2 flex justify-center pt-3">
<a
className="default-transition flex items-center text-sm text-th-fgd-1 hover:text-th-fgd-3"
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
<Image
alt=""
width="20"
height="20"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
<span className="ml-2 text-base">{`View on ${t(
`settings:${preferredExplorer.name}`
)}`}</span>
</a>
</div>
</div>
</Transition>
</div>
)
}