Show your performance in fold

This commit is contained in:
James Grugett 2022-02-02 23:34:35 -06:00
parent 32ce410092
commit 00ac581736
2 changed files with 65 additions and 28 deletions

View File

@ -16,7 +16,7 @@ export function Leaderboard(props: {
const { title, users, columns, className } = props const { title, users, columns, className } = props
return ( return (
<div className={clsx('w-full px-1', className)}> <div className={clsx('w-full px-1', className)}>
<Title text={title} /> <Title text={title} className="!mt-0" />
{users.length === 0 ? ( {users.length === 0 ? (
<div className="text-gray-500 ml-2">None yet</div> <div className="text-gray-500 ml-2">None yet</div>
) : ( ) : (

View File

@ -74,10 +74,10 @@ export async function getStaticProps(props: { params: { slugs: string[] } }) {
) )
const creatorScores = scoreCreators(contracts, bets) const creatorScores = scoreCreators(contracts, bets)
const [topCreators, topCreatorScores] = await toUserScores(creatorScores) const topCreators = await toTopUsers(creatorScores)
const traderScores = scoreTraders(contracts, bets) const traderScores = scoreTraders(contracts, bets)
const [topTraders, topTraderScores] = await toUserScores(traderScores) const topTraders = await toTopUsers(traderScores)
return { return {
props: { props: {
@ -87,17 +87,17 @@ export async function getStaticProps(props: { params: { slugs: string[] } }) {
activeContracts, activeContracts,
activeContractBets, activeContractBets,
activeContractComments, activeContractComments,
traderScores,
topTraders, topTraders,
topTraderScores, creatorScores,
topCreators, topCreators,
topCreatorScores,
}, },
revalidate: 60, // regenerate after a minute revalidate: 60, // regenerate after a minute
} }
} }
async function toUserScores(userScores: { [userId: string]: number }) { async function toTopUsers(userScores: { [userId: string]: number }) {
const topUserPairs = _.take( const topUserPairs = _.take(
_.sortBy(Object.entries(userScores), ([_, score]) => -1 * score), _.sortBy(Object.entries(userScores), ([_, score]) => -1 * score),
10 10
@ -106,14 +106,7 @@ async function toUserScores(userScores: { [userId: string]: number }) {
const topUsers = await Promise.all( const topUsers = await Promise.all(
topUserPairs.map(([userId]) => getUser(userId)) topUserPairs.map(([userId]) => getUser(userId))
) )
const existingPairs = topUserPairs.filter(([id, _]) => return topUsers.filter((user) => user)
topUsers.find((user) => user?.id === id)
)
const topExistingUsers = existingPairs.map(
([id]) => topUsers.find((user) => user?.id === id) as User
)
const topUserScores = existingPairs.map(([_, score]) => score)
return [topExistingUsers, topUserScores] as const
} }
export async function getStaticPaths() { export async function getStaticPaths() {
@ -128,19 +121,19 @@ export default function FoldPage(props: {
activeContracts: Contract[] activeContracts: Contract[]
activeContractBets: Bet[][] activeContractBets: Bet[][]
activeContractComments: Comment[][] activeContractComments: Comment[][]
traderScores: { [userId: string]: number }
topTraders: User[] topTraders: User[]
topTraderScores: number[] creatorScores: { [userId: string]: number }
topCreators: User[] topCreators: User[]
topCreatorScores: number[]
}) { }) {
const { const {
curator, curator,
activeContractBets, activeContractBets,
activeContractComments, activeContractComments,
traderScores,
topTraders, topTraders,
topTraderScores, creatorScores,
topCreators, topCreators,
topCreatorScores,
} = props } = props
const router = useRouter() const router = useRouter()
@ -272,25 +265,28 @@ export default function FoldPage(props: {
/> />
)} )}
</Col> </Col>
<Col className="hidden md:flex max-w-xs w-full gap-10"> <Col className="hidden md:flex max-w-xs w-full gap-12">
<FoldOverview fold={fold} curator={curator} /> <FoldOverview fold={fold} curator={curator} />
<FoldLeaderboards <FoldLeaderboards
traderScores={traderScores}
creatorScores={creatorScores}
topTraders={topTraders} topTraders={topTraders}
topTraderScores={topTraderScores}
topCreators={topCreators} topCreators={topCreators}
topCreatorScores={topCreatorScores} user={user}
/> />
</Col> </Col>
</Row> </Row>
)} )}
{page === 'leaderboards' && ( {page === 'leaderboards' && (
<Col className="gap-8 lg:flex-row"> <Col className="gap-8 lg:flex-row px-4">
<FoldLeaderboards <FoldLeaderboards
traderScores={traderScores}
creatorScores={creatorScores}
topTraders={topTraders} topTraders={topTraders}
topTraderScores={topTraderScores}
topCreators={topCreators} topCreators={topCreators}
topCreatorScores={topCreatorScores} user={user}
yourPerformanceClassName="lg:hidden"
/> />
</Col> </Col>
)} )}
@ -337,14 +333,54 @@ function FoldOverview(props: { fold: Fold; curator: User }) {
} }
function FoldLeaderboards(props: { function FoldLeaderboards(props: {
traderScores: { [userId: string]: number }
creatorScores: { [userId: string]: number }
topTraders: User[] topTraders: User[]
topTraderScores: number[]
topCreators: User[] topCreators: User[]
topCreatorScores: number[] user: User | null | undefined
yourPerformanceClassName?: string
}) { }) {
const { topTraders, topTraderScores, topCreators, topCreatorScores } = props const {
traderScores,
creatorScores,
topTraders,
topCreators,
user,
yourPerformanceClassName,
} = props
const yourTraderScore = user ? traderScores[user.id] : undefined
const yourCreatorScore = user ? creatorScores[user.id] : undefined
const topTraderScores = topTraders.map((user) => traderScores[user.id])
const topCreatorScores = topCreators.map((user) => creatorScores[user.id])
return ( return (
<> <>
{user && (
<Col className={yourPerformanceClassName}>
<div className="bg-indigo-500 text-white text-sm px-4 py-3 rounded">
Your performance
</div>
<div className="bg-white p-2">
<table className="table table-compact text-gray-500 w-full">
<tbody>
<tr>
<td>Trading profit</td>
<td>{formatMoney(yourTraderScore ?? 0)}</td>
</tr>
{yourCreatorScore && (
<tr>
<td>Created market vol</td>
<td>{formatMoney(yourCreatorScore)}</td>
</tr>
)}
</tbody>
</table>
</div>
</Col>
)}
<Leaderboard <Leaderboard
className="max-w-xl" className="max-w-xl"
title="🏅 Top traders" title="🏅 Top traders"
@ -357,13 +393,14 @@ function FoldLeaderboards(props: {
}, },
]} ]}
/> />
<Leaderboard <Leaderboard
className="max-w-xl" className="max-w-xl"
title="🏅 Top creators" title="🏅 Top creators"
users={topCreators} users={topCreators}
columns={[ columns={[
{ {
header: 'Market pool', header: 'Market vol',
renderCell: (user) => renderCell: (user) =>
formatMoney(topCreatorScores[topCreators.indexOf(user)]), formatMoney(topCreatorScores[topCreators.indexOf(user)]),
}, },