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

413 lines
15 KiB
TypeScript
Raw Normal View History

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-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 {
BoltIcon,
ChevronDownIcon,
ChevronRightIcon,
LinkIcon,
} from '@heroicons/react/20/solid'
2022-09-27 23:17:06 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-09-28 06:04:58 -07:00
import mangoStore, { LiquidationFeedItem } from '@store/mangoStore'
2022-09-27 23:17:06 -07:00
import dayjs from 'dayjs'
import useLocalStorageState from 'hooks/useLocalStorageState'
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-27 23:17:06 -07:00
import { EXPLORERS } from 'pages/settings'
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-10-27 13:58:54 -07:00
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
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
}
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
}
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 ? (
<table className="min-w-full">
<thead>
<tr 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">
{t('activity:activity')}
</th>
<th className="bg-th-bkg-1 text-right">
{t('activity:credit')}
</th>
<th className="bg-th-bkg-1 text-right">
{t('activity:debit')}
</th>
<th className="bg-th-bkg-1 text-right">
{t('activity:activity-value')}
</th>
<th className="bg-th-bkg-1 text-right">{t('explorer')}</th>
</tr>
</thead>
<tbody>
{activityFeed.map((activity: any, i: number) => {
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 (
<tr
key={signature}
className={`default-transition text-sm hover:bg-th-bkg-2 ${
isLiquidation ? 'cursor-pointer' : ''
}`}
onClick={
isLiquidation
? () => handleShowActivityDetails(activity)
: undefined
}
>
<td>
<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>
</td>
<td className="text-right">
{t(`activity:${activityName}`)}
</td>
<td className="text-right font-mono">
{amounts.credit.value}{' '}
<span className="font-body tracking-wide text-th-fgd-3">
{amounts.credit.symbol}
</span>
</td>
<td className="text-right font-mono">
{amounts.debit.value}{' '}
<span className="font-body tracking-wide text-th-fgd-3">
{amounts.debit.symbol}
</span>
</td>
<td className="text-right font-mono">
{formatFixedDecimals(value, true)}
</td>
<td>
{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>
)}
</td>
</tr>
)
})}
</tbody>
</table>
) : (
<div>
{activityFeed.map((activity: any, i: number) => (
<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">
<BoltIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>No account activity found...</p>
</div>
)
) : (
<div className="flex flex-col items-center p-8">
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>Connect to view your account activity</p>
</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'
const activityName = isLiquidation ? 'liquidation' : activity_type
const value = getValue(activity)
return (
<div key={signature} className="border-b border-th-bkg-3 px-6 py-4">
<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)
) : (
<>
<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>
)
}