2022-01-25 20:47:25 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Fold } from 'common/fold'
|
|
|
|
import { Comment } from 'common/comment'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
|
|
|
import { Contract } from 'web/lib/firebase/contracts'
|
2022-01-25 20:47:25 +00:00
|
|
|
import {
|
|
|
|
foldPath,
|
|
|
|
getFoldBySlug,
|
|
|
|
getFoldContracts,
|
2022-05-09 17:38:33 +00:00
|
|
|
} from '../../../lib/firebase/folds'
|
|
|
|
import { TagsList } from '../../../components/tags-list'
|
|
|
|
import { Row } from '../../../components/layout/row'
|
|
|
|
import { UserLink } from '../../../components/user-page'
|
|
|
|
import { getUser, User } from '../../../lib/firebase/users'
|
|
|
|
import { Spacer } from '../../../components/layout/spacer'
|
|
|
|
import { Col } from '../../../components/layout/col'
|
|
|
|
import { useUser } from '../../../hooks/use-user'
|
|
|
|
import { useFold } from '../../../hooks/use-fold'
|
2022-01-25 20:47:25 +00:00
|
|
|
import { useRouter } from 'next/router'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { scoreCreators, scoreTraders } from 'common/scoring'
|
|
|
|
import { Leaderboard } from 'web/components/leaderboard'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { EditFoldButton } from 'web/components/folds/edit-fold-button'
|
2022-01-25 21:44:28 +00:00
|
|
|
import Custom404 from '../../404'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { FollowFoldButton } from 'web/components/folds/follow-fold-button'
|
|
|
|
import { SEO } from 'web/components/SEO'
|
|
|
|
import { useTaggedContracts } from 'web/hooks/use-contracts'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
|
|
|
import { filterDefined } from 'common/util/array'
|
|
|
|
import { findActiveContracts } from 'web/components/feed/find-active-contracts'
|
|
|
|
import { Tabs } from 'web/components/layout/tabs'
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz(props: { params: { slugs: string[] } }) {
|
2022-01-25 20:47:25 +00:00
|
|
|
const { slugs } = props.params
|
|
|
|
|
|
|
|
const fold = await getFoldBySlug(slugs[0])
|
|
|
|
const curatorPromise = fold ? getUser(fold.curatorId) : null
|
|
|
|
|
|
|
|
const contracts = fold ? await getFoldContracts(fold).catch((_) => []) : []
|
2022-02-03 05:58:27 +00:00
|
|
|
|
2022-03-02 02:08:44 +00:00
|
|
|
const bets = await Promise.all(
|
2022-02-03 06:19:59 +00:00
|
|
|
contracts.map((contract) => listAllBets(contract.id))
|
|
|
|
)
|
|
|
|
|
2022-03-17 07:29:08 +00:00
|
|
|
let activeContracts = findActiveContracts(contracts, [], _.flatten(bets), {})
|
2022-01-25 20:47:25 +00:00
|
|
|
const [resolved, unresolved] = _.partition(
|
|
|
|
activeContracts,
|
|
|
|
({ isResolved }) => isResolved
|
|
|
|
)
|
|
|
|
activeContracts = [...unresolved, ...resolved]
|
|
|
|
|
|
|
|
const creatorScores = scoreCreators(contracts, bets)
|
|
|
|
const traderScores = scoreTraders(contracts, bets)
|
2022-02-03 06:19:59 +00:00
|
|
|
const [topCreators, topTraders] = await Promise.all([
|
|
|
|
toTopUsers(creatorScores),
|
|
|
|
toTopUsers(traderScores),
|
|
|
|
])
|
|
|
|
|
|
|
|
const curator = await curatorPromise
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
fold,
|
|
|
|
curator,
|
|
|
|
contracts,
|
|
|
|
activeContracts,
|
2022-02-03 05:34:35 +00:00
|
|
|
traderScores,
|
2022-01-25 20:47:25 +00:00
|
|
|
topTraders,
|
2022-02-03 05:34:35 +00:00
|
|
|
creatorScores,
|
2022-01-25 20:47:25 +00:00
|
|
|
topCreators,
|
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 05:34:35 +00:00
|
|
|
async function toTopUsers(userScores: { [userId: string]: number }) {
|
2022-01-25 20:47:25 +00:00
|
|
|
const topUserPairs = _.take(
|
|
|
|
_.sortBy(Object.entries(userScores), ([_, score]) => -1 * score),
|
|
|
|
10
|
2022-02-10 20:51:14 +00:00
|
|
|
).filter(([_, score]) => score >= 0.5)
|
2022-01-28 18:43:35 +00:00
|
|
|
|
2022-01-25 20:47:25 +00:00
|
|
|
const topUsers = await Promise.all(
|
|
|
|
topUserPairs.map(([userId]) => getUser(userId))
|
|
|
|
)
|
2022-02-03 05:34:35 +00:00
|
|
|
return topUsers.filter((user) => user)
|
2022-01-25 20:47:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStaticPaths() {
|
|
|
|
return { paths: [], fallback: 'blocking' }
|
|
|
|
}
|
2022-01-25 21:44:28 +00:00
|
|
|
const foldSubpages = [undefined, 'activity', 'markets', 'leaderboards'] as const
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
export default function FoldPage(props: {
|
2022-01-25 21:44:28 +00:00
|
|
|
fold: Fold | null
|
2022-01-25 20:47:25 +00:00
|
|
|
curator: User
|
|
|
|
contracts: Contract[]
|
|
|
|
activeContracts: Contract[]
|
|
|
|
activeContractBets: Bet[][]
|
|
|
|
activeContractComments: Comment[][]
|
2022-02-03 05:34:35 +00:00
|
|
|
traderScores: { [userId: string]: number }
|
2022-01-25 20:47:25 +00:00
|
|
|
topTraders: User[]
|
2022-02-03 05:34:35 +00:00
|
|
|
creatorScores: { [userId: string]: number }
|
2022-01-25 20:47:25 +00:00
|
|
|
topCreators: User[]
|
|
|
|
}) {
|
2022-03-09 02:43:30 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
fold: null,
|
|
|
|
curator: null,
|
|
|
|
contracts: [],
|
|
|
|
activeContracts: [],
|
|
|
|
activeContractBets: [],
|
|
|
|
activeContractComments: [],
|
|
|
|
traderScores: {},
|
|
|
|
topTraders: [],
|
|
|
|
creatorScores: {},
|
|
|
|
topCreators: [],
|
|
|
|
}
|
2022-03-05 00:06:11 +00:00
|
|
|
const { curator, traderScores, topTraders, creatorScores, topCreators } =
|
|
|
|
props
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const { slugs } = router.query as { slugs: string[] }
|
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
const page = (slugs?.[1] ?? 'activity') as typeof foldSubpages[number]
|
2022-01-25 21:44:28 +00:00
|
|
|
|
2022-01-27 06:38:22 +00:00
|
|
|
const fold = useFold(props.fold?.id) ?? props.fold
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
2022-01-25 21:44:28 +00:00
|
|
|
const isCurator = user && fold && user.id === fold.curatorId
|
|
|
|
|
2022-01-31 04:12:12 +00:00
|
|
|
const taggedContracts = useTaggedContracts(fold?.tags) ?? props.contracts
|
|
|
|
const contractsMap = _.fromPairs(
|
|
|
|
taggedContracts.map((contract) => [contract.id, contract])
|
|
|
|
)
|
|
|
|
|
2022-03-01 20:40:46 +00:00
|
|
|
const contracts = filterDefined(
|
|
|
|
props.contracts.map((contract) => contractsMap[contract.id])
|
|
|
|
)
|
2022-03-05 00:06:11 +00:00
|
|
|
|
2022-01-25 21:44:28 +00:00
|
|
|
if (fold === null || !foldSubpages.includes(page) || slugs[2]) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
const rightSidebar = (
|
|
|
|
<Col className="mt-6 gap-12">
|
|
|
|
<Row className="justify-end">
|
|
|
|
{isCurator ? (
|
|
|
|
<EditFoldButton className="ml-1" fold={fold} />
|
|
|
|
) : (
|
|
|
|
<FollowFoldButton className="ml-1" fold={fold} />
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
<FoldOverview fold={fold} curator={curator} />
|
2022-03-31 06:24:35 +00:00
|
|
|
<YourPerformance
|
|
|
|
traderScores={traderScores}
|
|
|
|
creatorScores={creatorScores}
|
|
|
|
user={user}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
|
|
|
|
const leaderboardsTab = (
|
|
|
|
<Col className="gap-8 px-4 lg:flex-row">
|
2022-03-31 05:35:20 +00:00
|
|
|
<FoldLeaderboards
|
|
|
|
traderScores={traderScores}
|
|
|
|
creatorScores={creatorScores}
|
|
|
|
topTraders={topTraders}
|
|
|
|
topCreators={topCreators}
|
|
|
|
user={user}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
|
2022-01-25 20:47:25 +00:00
|
|
|
return (
|
2022-03-31 05:35:20 +00:00
|
|
|
<Page rightSidebar={rightSidebar}>
|
2022-01-28 19:45:32 +00:00
|
|
|
<SEO
|
|
|
|
title={fold.name}
|
|
|
|
description={`Curated by ${curator.name}. ${fold.about}`}
|
|
|
|
url={foldPath(fold)}
|
|
|
|
/>
|
2022-01-28 18:34:13 +00:00
|
|
|
|
2022-01-28 18:13:12 +00:00
|
|
|
<div className="px-3 lg:px-1">
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="mb-6 justify-between">
|
2022-01-28 18:13:12 +00:00
|
|
|
<Title className="!m-0" text={fold.name} />
|
2022-01-26 18:02:53 +00:00
|
|
|
</Row>
|
2022-01-28 18:13:12 +00:00
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="mb-6 gap-2 text-gray-500 md:hidden">
|
2022-01-28 18:13:12 +00:00
|
|
|
<Row>
|
|
|
|
<div className="mr-1">Curated by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={curator.name}
|
|
|
|
username={curator.username}
|
|
|
|
/>
|
|
|
|
</Row>
|
2022-02-10 20:57:41 +00:00
|
|
|
<Linkify text={fold.about ?? ''} />
|
2022-01-28 18:13:12 +00:00
|
|
|
</Col>
|
|
|
|
</div>
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-03-31 06:24:35 +00:00
|
|
|
<Tabs
|
2022-05-01 16:36:54 +00:00
|
|
|
defaultIndex={page === 'leaderboards' ? 1 : 0}
|
2022-03-31 06:24:35 +00:00
|
|
|
tabs={[
|
|
|
|
{
|
|
|
|
title: 'Markets',
|
2022-05-09 17:38:33 +00:00
|
|
|
content: <div>This view is deprecated.</div>,
|
2022-03-31 06:24:35 +00:00
|
|
|
href: foldPath(fold, 'markets'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Leaderboards',
|
|
|
|
content: leaderboardsTab,
|
|
|
|
href: foldPath(fold, 'leaderboards'),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2022-01-25 20:47:25 +00:00
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function FoldOverview(props: { fold: Fold; curator: User }) {
|
|
|
|
const { fold, curator } = props
|
|
|
|
const { about, tags } = fold
|
|
|
|
|
|
|
|
return (
|
2022-01-27 06:38:22 +00:00
|
|
|
<Col>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="rounded-t bg-indigo-500 px-4 py-3 text-sm text-white">
|
2022-01-25 20:47:25 +00:00
|
|
|
About community
|
|
|
|
</div>
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="gap-2 rounded-b bg-white p-4">
|
2022-01-25 20:47:25 +00:00
|
|
|
<Row>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mr-1 text-gray-500">Curated by</div>
|
2022-01-25 20:47:25 +00:00
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={curator.name}
|
|
|
|
username={curator.username}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
{about && (
|
|
|
|
<>
|
|
|
|
<Spacer h={2} />
|
2022-02-10 20:57:41 +00:00
|
|
|
<div className="text-gray-500">
|
|
|
|
<Linkify text={about} />
|
|
|
|
</div>
|
2022-01-25 20:47:25 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-02-02 20:20:02 +00:00
|
|
|
<div className="divider" />
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mb-2 text-gray-500">
|
2022-02-02 20:20:02 +00:00
|
|
|
Includes markets matching any of these tags:
|
|
|
|
</div>
|
|
|
|
|
2022-02-03 20:13:51 +00:00
|
|
|
<TagsList tags={tags} noLabel />
|
2022-01-25 20:47:25 +00:00
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-31 06:24:35 +00:00
|
|
|
function YourPerformance(props: {
|
2022-02-03 05:34:35 +00:00
|
|
|
traderScores: { [userId: string]: number }
|
|
|
|
creatorScores: { [userId: string]: number }
|
2022-03-31 06:24:35 +00:00
|
|
|
|
2022-02-03 05:34:35 +00:00
|
|
|
user: User | null | undefined
|
2022-01-25 20:47:25 +00:00
|
|
|
}) {
|
2022-03-31 06:24:35 +00:00
|
|
|
const { traderScores, creatorScores, user } = props
|
2022-02-03 05:34:35 +00:00
|
|
|
|
|
|
|
const yourTraderScore = user ? traderScores[user.id] : undefined
|
|
|
|
const yourCreatorScore = user ? creatorScores[user.id] : undefined
|
|
|
|
|
2022-03-31 06:24:35 +00:00
|
|
|
return user ? (
|
|
|
|
<Col>
|
|
|
|
<div className="rounded bg-indigo-500 px-4 py-3 text-sm text-white">
|
|
|
|
Your performance
|
|
|
|
</div>
|
|
|
|
<div className="bg-white p-2">
|
|
|
|
<table className="table-compact table w-full text-gray-500">
|
|
|
|
<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>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
function FoldLeaderboards(props: {
|
|
|
|
traderScores: { [userId: string]: number }
|
|
|
|
creatorScores: { [userId: string]: number }
|
|
|
|
topTraders: User[]
|
|
|
|
topCreators: User[]
|
|
|
|
user: User | null | undefined
|
|
|
|
}) {
|
|
|
|
const { traderScores, creatorScores, topTraders, topCreators } = props
|
|
|
|
|
2022-02-03 05:34:35 +00:00
|
|
|
const topTraderScores = topTraders.map((user) => traderScores[user.id])
|
|
|
|
const topCreatorScores = topCreators.map((user) => creatorScores[user.id])
|
|
|
|
|
2022-01-25 20:47:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
2022-04-26 14:42:45 +00:00
|
|
|
title="🏅 Top bettors"
|
2022-01-25 20:47:25 +00:00
|
|
|
users={topTraders}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Profit',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(topTraderScores[topTraders.indexOf(user)]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2022-02-03 05:34:35 +00:00
|
|
|
|
2022-01-25 20:47:25 +00:00
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
|
|
|
title="🏅 Top creators"
|
|
|
|
users={topCreators}
|
|
|
|
columns={[
|
|
|
|
{
|
2022-02-03 05:34:35 +00:00
|
|
|
header: 'Market vol',
|
2022-01-25 20:47:25 +00:00
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(topCreatorScores[topCreators.indexOf(user)]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|