2022-05-09 13:04:36 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Leaderboard } from 'web/components/leaderboard'
|
|
|
|
import { Page } from 'web/components/page'
|
2022-06-22 16:05:54 +00:00
|
|
|
import {
|
|
|
|
getTopCreators,
|
|
|
|
getTopTraders,
|
2022-06-22 20:29:40 +00:00
|
|
|
LeaderboardPeriod,
|
2022-06-22 16:05:54 +00:00
|
|
|
getTopFollowed,
|
|
|
|
User,
|
|
|
|
} from 'web/lib/firebase/users'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
2022-06-22 20:29:40 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { Tabs } from 'web/components/layout/tabs'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { useTracking } from 'web/hooks/use-tracking'
|
2022-01-18 03:20:25 +00:00
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz() {
|
2022-06-22 20:29:40 +00:00
|
|
|
return queryLeaderboardUsers('allTime')
|
|
|
|
}
|
|
|
|
const queryLeaderboardUsers = async (period: LeaderboardPeriod) => {
|
2022-06-22 16:05:54 +00:00
|
|
|
const [topTraders, topCreators, topFollowed] = await Promise.all([
|
2022-06-22 20:29:40 +00:00
|
|
|
getTopTraders(period).catch(() => {}),
|
|
|
|
getTopCreators(period).catch(() => {}),
|
2022-06-22 16:05:54 +00:00
|
|
|
getTopFollowed().catch(() => {}),
|
2022-01-18 03:20:25 +00:00
|
|
|
])
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
topTraders,
|
|
|
|
topCreators,
|
2022-06-22 16:05:54 +00:00
|
|
|
topFollowed,
|
2022-01-18 03:20:25 +00:00
|
|
|
},
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Leaderboards(props: {
|
|
|
|
topTraders: User[]
|
|
|
|
topCreators: User[]
|
2022-06-22 16:05:54 +00:00
|
|
|
topFollowed: User[]
|
2022-01-18 03:20:25 +00:00
|
|
|
}) {
|
2022-03-09 02:43:30 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
topTraders: [],
|
|
|
|
topCreators: [],
|
2022-06-22 16:05:54 +00:00
|
|
|
topFollowed: [],
|
2022-03-09 02:43:30 +00:00
|
|
|
}
|
2022-06-22 20:29:40 +00:00
|
|
|
const { topFollowed } = props
|
|
|
|
const [topTradersState, setTopTraders] = useState(props.topTraders)
|
|
|
|
const [topCreatorsState, setTopCreators] = useState(props.topCreators)
|
|
|
|
const [isLoading, setLoading] = useState(false)
|
|
|
|
const [period, setPeriod] = useState<LeaderboardPeriod>('allTime')
|
2022-01-18 03:20:25 +00:00
|
|
|
|
2022-06-22 20:29:40 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setLoading(true)
|
|
|
|
queryLeaderboardUsers(period).then((res) => {
|
|
|
|
setTopTraders(res.props.topTraders as User[])
|
|
|
|
setTopCreators(res.props.topCreators as User[])
|
|
|
|
setLoading(false)
|
|
|
|
})
|
|
|
|
}, [period])
|
|
|
|
|
|
|
|
const LeaderboardWithPeriod = (period: LeaderboardPeriod) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Col className="mx-4 items-center gap-10 lg:flex-row">
|
|
|
|
{!isLoading ? (
|
|
|
|
<>
|
|
|
|
{period === 'allTime' ? ( //TODO: show other periods once they're available
|
|
|
|
<Leaderboard
|
|
|
|
title="🏅 Top bettors"
|
|
|
|
users={topTradersState}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Total profit',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(user.profitCached[period]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Leaderboard
|
|
|
|
title="🏅 Top creators"
|
|
|
|
users={topCreatorsState}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Total bet',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(user.creatorVolumeCached[period]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<LoadingIndicator spinnerClassName={'border-gray-500'} />
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
{period === 'allTime' ? (
|
2022-06-22 20:52:23 +00:00
|
|
|
<Col className="mx-4 my-10 items-center gap-10 lg:mx-0 lg:w-1/2 lg:flex-row">
|
2022-06-22 20:29:40 +00:00
|
|
|
<Leaderboard
|
2022-06-22 21:22:45 +00:00
|
|
|
title="🏅 Top followed"
|
2022-06-22 20:29:40 +00:00
|
|
|
users={topFollowed}
|
|
|
|
columns={[
|
|
|
|
{
|
2022-06-22 21:17:21 +00:00
|
|
|
header: 'Total followers',
|
2022-06-22 20:29:40 +00:00
|
|
|
renderCell: (user) => user.followerCountCached,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
<></>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2022-06-15 21:34:34 +00:00
|
|
|
useTracking('view leaderboards')
|
|
|
|
|
2022-01-18 03:20:25 +00:00
|
|
|
return (
|
2022-06-16 21:43:34 +00:00
|
|
|
<Page>
|
2022-06-22 20:29:40 +00:00
|
|
|
<Title text={'Leaderboards'} className={'hidden md:block'} />
|
|
|
|
<Tabs
|
|
|
|
defaultIndex={0}
|
|
|
|
onClick={(title, index) => {
|
|
|
|
const period = ['allTime', 'monthly', 'weekly', 'daily'][index]
|
|
|
|
setPeriod(period as LeaderboardPeriod)
|
|
|
|
}}
|
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
title: 'All Time',
|
|
|
|
content: LeaderboardWithPeriod('allTime'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Monthly',
|
|
|
|
content: LeaderboardWithPeriod('monthly'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Weekly',
|
|
|
|
content: LeaderboardWithPeriod('weekly'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Daily',
|
|
|
|
content: LeaderboardWithPeriod('daily'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2022-01-18 03:20:25 +00:00
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|