mango-v4-ui/components/rewards/Leaderboards.tsx

235 lines
7.8 KiB
TypeScript
Raw Normal View History

2023-06-13 05:52:58 -07:00
import MedalIcon from '@components/icons/MedalIcon'
import ProfileImage from '@components/profile/ProfileImage'
import { ArrowLeftIcon, ChevronRightIcon } from '@heroicons/react/20/solid'
import { useViewport } from 'hooks/useViewport'
2023-10-05 04:10:29 -07:00
import { useEffect, useState } from 'react'
2023-06-13 05:52:58 -07:00
import Select from '@components/forms/Select'
import { IconButton } from '@components/shared/Button'
import AcornIcon from '@components/icons/AcornIcon'
import WhaleIcon from '@components/icons/WhaleIcon'
import RobotIcon from '@components/icons/RobotIcon'
import MangoIcon from '@components/icons/MangoIcon'
2023-06-18 20:47:25 -07:00
import { useQuery } from '@tanstack/react-query'
import SheenLoader from '@components/shared/SheenLoader'
import { abbreviateAddress } from 'utils/formatting'
import { PublicKey } from '@solana/web3.js'
import { formatNumericValue } from 'utils/numbers'
2023-06-19 05:41:51 -07:00
import { useTranslation } from 'next-i18next'
import { fetchLeaderboard } from 'apis/rewards'
2023-10-05 04:10:29 -07:00
import {
useAccountPointsAndRank,
useAccountTier,
useCurrentSeason,
} from 'hooks/useRewards'
import Badge from './Badge'
import { tiers } from './RewardsPage'
2023-10-05 04:10:29 -07:00
import useMangoAccount from 'hooks/useMangoAccount'
2023-06-13 05:52:58 -07:00
const Leaderboards = ({
goBack,
leaderboard,
}: {
goBack: () => void
leaderboard: string
}) => {
2023-06-19 05:41:51 -07:00
const { t } = useTranslation('rewards')
2023-10-05 04:10:29 -07:00
const { isDesktop } = useViewport()
const { mangoAccountAddress } = useMangoAccount()
const [leaderboardToShow, setLeaderboardToShow] =
useState<string>(leaderboard)
2023-06-13 05:52:58 -07:00
const renderTierIcon = (tier: string) => {
2023-06-18 20:47:25 -07:00
if (tier === 'bot') {
2023-06-13 05:52:58 -07:00
return <RobotIcon className="mr-2 h-5 w-5" />
2023-06-18 20:47:25 -07:00
} else if (tier === 'mango') {
2023-06-13 05:52:58 -07:00
return <MangoIcon className="mr-2 h-5 w-5" />
2023-06-18 20:47:25 -07:00
} else if (tier === 'whale') {
2023-06-13 05:52:58 -07:00
return <WhaleIcon className="mr-2 h-5 w-5" />
} else return <AcornIcon className="mr-2 h-5 w-5" />
}
const { data: seasonData } = useCurrentSeason()
2023-10-05 04:10:29 -07:00
const { data: accountTier } = useAccountTier(
mangoAccountAddress,
seasonData?.season_id,
)
const { data: accountPointsAndRank } = useAccountPointsAndRank(
mangoAccountAddress,
seasonData?.season_id,
)
2023-06-18 20:47:25 -07:00
2023-10-05 04:10:29 -07:00
const { data: leaderboardData, isLoading: loadingLeaderboardData } = useQuery(
['rewards-leaderboard-data', leaderboardToShow],
() => fetchLeaderboard(seasonData!.season_id),
2023-06-18 20:47:25 -07:00
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
enabled: !!seasonData,
2023-07-21 11:47:53 -07:00
},
2023-06-18 20:47:25 -07:00
)
2023-10-05 04:10:29 -07:00
const leadersForTier =
2023-10-05 04:10:29 -07:00
leaderboardData && leaderboardData.length
? leaderboardData.find((x) => x.tier === leaderboardToShow)
?.leaderboard || []
: []
2023-06-18 20:47:25 -07:00
2023-10-05 04:10:29 -07:00
useEffect(() => {
if (typeof window !== 'undefined') {
window.scrollTo(0, 0)
}
}, [])
2023-06-18 20:47:25 -07:00
2023-06-13 05:52:58 -07:00
return (
<div className="mx-auto min-h-screen max-w-[1140px] flex-col items-center p-8 lg:p-10">
2023-06-13 05:52:58 -07:00
<div className="mb-4 flex items-center justify-between">
<div className="flex items-center">
<IconButton className="mr-2" hideBg onClick={goBack} size="small">
<ArrowLeftIcon className="h-5 w-5" />
</IconButton>
<h2 className="rewards-h2 mr-4">Leaderboard</h2>
2023-06-13 05:52:58 -07:00
<Badge
label={`Season ${seasonData?.season_id}`}
fillColor="bg-th-active"
2023-06-13 05:52:58 -07:00
/>
</div>
<Select
className="w-32"
2023-10-05 04:10:29 -07:00
icon={renderTierIcon(leaderboardToShow)}
value={t(leaderboardToShow)}
onChange={(tier) => setLeaderboardToShow(tier)}
2023-06-13 05:52:58 -07:00
>
{tiers.map((tier) => (
<Select.Option key={tier} value={tier}>
<div className="flex w-full items-center">
{renderTierIcon(tier)}
2023-06-19 05:41:51 -07:00
{t(tier)}
2023-06-13 05:52:58 -07:00
</div>
</Select.Option>
))}
</Select>
</div>
<div className="space-y-2">
2023-10-05 04:10:29 -07:00
{accountTier?.tier === leaderboardToShow &&
accountPointsAndRank?.rank ? (
2023-10-09 19:23:25 -07:00
<div className="flex w-full items-center justify-between rounded-lg border border-th-active p-3 md:rounded-xl md:p-4">
2023-10-05 04:10:29 -07:00
<div className="flex items-center space-x-3">
<div
className={`relative flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-full ${
accountPointsAndRank.rank < 4 ? '' : 'bg-th-bkg-3'
} md:mr-2`}
>
<p
className={`relative z-10 font-rewards text-base ${
accountPointsAndRank.rank < 4
? 'text-th-bkg-1'
: 'text-th-fgd-1'
}`}
>
{accountPointsAndRank.rank}
</p>
{accountPointsAndRank.rank < 4 ? (
<MedalIcon
className="absolute"
rank={accountPointsAndRank.rank}
/>
) : null}
</div>
<ProfileImage
imageSize={!isDesktop ? '32' : '40'}
imageUrl={''}
placeholderSize={!isDesktop ? '20' : '24'}
/>
<div className="text-left">
<p className="text-th-fgd-2 md:text-base">YOU</p>
</div>
</div>
<div>
<span className="mr-3 text-right font-mono md:text-base">
{formatNumericValue(accountPointsAndRank.total_points)}
</span>
</div>
</div>
) : null}
{!loadingLeaderboardData ? (
leadersForTier && leadersForTier.length ? (
leadersForTier.map((acc, i: number) => (
<LeaderboardCard rank={i + 1} key={i} account={acc} />
))
2023-06-18 20:47:25 -07:00
) : (
2023-06-19 05:41:51 -07:00
<div className="flex justify-center rounded-lg border border-th-bkg-3 p-8">
<span className="text-th-fgd-3">Leaderboard not available</span>
</div>
2023-06-18 20:47:25 -07:00
)
) : (
<div className="space-y-2">
{[...Array(20)].map((x, i) => (
<SheenLoader className="flex flex-1" key={i}>
<div className="h-16 w-full bg-th-bkg-2" />
</SheenLoader>
))}
</div>
)}
2023-06-13 05:52:58 -07:00
</div>
</div>
)
}
export default Leaderboards
2023-06-18 20:47:25 -07:00
const LeaderboardCard = ({
rank,
account,
2023-06-18 20:47:25 -07:00
}: {
rank: number
account: {
mango_account: string
tier: string
total_points: number
}
2023-06-18 20:47:25 -07:00
}) => {
2023-10-05 04:10:29 -07:00
const { isDesktop } = useViewport()
const { mango_account, total_points } = account
2023-06-13 05:52:58 -07:00
return (
<a
className="flex w-full items-center justify-between rounded-lg border border-th-bkg-3 p-3 md:rounded-xl md:p-4 md:hover:bg-th-bkg-2"
href={`/?address=${mango_account}`}
2023-06-13 05:52:58 -07:00
rel="noopener noreferrer"
target="_blank"
>
<div className="flex items-center space-x-3">
<div
className={`relative flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-full ${
rank < 4 ? '' : 'bg-th-bkg-3'
} md:mr-2`}
>
<p
className={`relative z-10 font-rewards text-base ${
rank < 4 ? 'text-th-bkg-1' : 'text-th-fgd-1'
2023-06-13 05:52:58 -07:00
}`}
>
{rank}
</p>
{rank < 4 ? <MedalIcon className="absolute" rank={rank} /> : null}
</div>
<ProfileImage
2023-10-05 04:10:29 -07:00
imageSize={!isDesktop ? '32' : '40'}
2023-06-13 05:52:58 -07:00
imageUrl={''}
2023-10-05 04:10:29 -07:00
placeholderSize={!isDesktop ? '20' : '24'}
2023-06-13 05:52:58 -07:00
/>
<div className="text-left">
<p className="capitalize text-th-fgd-2 md:text-base">
{abbreviateAddress(new PublicKey(mango_account))}
2023-06-13 05:52:58 -07:00
</p>
</div>
</div>
<div className="flex items-center">
2023-06-18 20:47:25 -07:00
<span className="mr-3 text-right font-mono md:text-base">
{formatNumericValue(total_points)}
2023-06-18 20:47:25 -07:00
</span>
<ChevronRightIcon className="h-6 w-6 text-th-fgd-3" />
2023-06-13 05:52:58 -07:00
</div>
</a>
)
}