2022-01-25 20:47:25 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import Link from 'next/link'
|
|
|
|
|
|
|
|
import { Fold } from '../../../../common/fold'
|
|
|
|
import { Comment } from '../../../../common/comment'
|
|
|
|
import { Page } from '../../../components/page'
|
|
|
|
import { Title } from '../../../components/title'
|
|
|
|
import { Bet, listAllBets } from '../../../lib/firebase/bets'
|
|
|
|
import { listAllComments } from '../../../lib/firebase/comments'
|
|
|
|
import { Contract } from '../../../lib/firebase/contracts'
|
|
|
|
import {
|
|
|
|
foldPath,
|
|
|
|
getFoldBySlug,
|
|
|
|
getFoldContracts,
|
|
|
|
} from '../../../lib/firebase/folds'
|
|
|
|
import { ActivityFeed, findActiveContracts } from '../../activity'
|
|
|
|
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'
|
|
|
|
import { SearchableGrid } from '../../../components/contracts-list'
|
|
|
|
import { useQueryAndSortParams } from '../../../hooks/use-sort-and-query-params'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import clsx from 'clsx'
|
|
|
|
import { scoreCreators, scoreTraders } from '../../../lib/firebase/scoring'
|
|
|
|
import { Leaderboard } from '../../../components/leaderboard'
|
2022-01-27 18:45:35 +00:00
|
|
|
import { formatMoney, toCamelCase } from '../../../lib/util/format'
|
2022-01-25 20:47:25 +00:00
|
|
|
import { EditFoldButton } from '../../../components/edit-fold-button'
|
2022-01-25 21:44:28 +00:00
|
|
|
import Custom404 from '../../404'
|
2022-01-26 20:03:32 +00:00
|
|
|
import { FollowFoldButton } from '../../../components/follow-fold-button'
|
2022-01-27 18:45:35 +00:00
|
|
|
import FeedCreate from '../../../components/feed-create'
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
export async function getStaticProps(props: { params: { slugs: string[] } }) {
|
|
|
|
const { slugs } = props.params
|
|
|
|
|
|
|
|
const fold = await getFoldBySlug(slugs[0])
|
|
|
|
const curatorPromise = fold ? getUser(fold.curatorId) : null
|
|
|
|
|
|
|
|
const contracts = fold ? await getFoldContracts(fold).catch((_) => []) : []
|
|
|
|
const contractComments = await Promise.all(
|
|
|
|
contracts.map((contract) => listAllComments(contract.id).catch((_) => []))
|
|
|
|
)
|
|
|
|
|
|
|
|
let activeContracts = findActiveContracts(
|
|
|
|
contracts,
|
|
|
|
_.flatten(contractComments),
|
|
|
|
365
|
|
|
|
)
|
|
|
|
const [resolved, unresolved] = _.partition(
|
|
|
|
activeContracts,
|
|
|
|
({ isResolved }) => isResolved
|
|
|
|
)
|
|
|
|
activeContracts = [...unresolved, ...resolved]
|
|
|
|
|
|
|
|
const activeContractBets = await Promise.all(
|
|
|
|
activeContracts.map((contract) => listAllBets(contract.id).catch((_) => []))
|
|
|
|
)
|
|
|
|
const activeContractComments = activeContracts.map(
|
|
|
|
(contract) =>
|
|
|
|
contractComments[contracts.findIndex((c) => c.id === contract.id)]
|
|
|
|
)
|
|
|
|
|
|
|
|
const curator = await curatorPromise
|
|
|
|
|
|
|
|
const bets = await Promise.all(
|
|
|
|
contracts.map((contract) => listAllBets(contract.id))
|
|
|
|
)
|
|
|
|
|
|
|
|
const creatorScores = scoreCreators(contracts, bets)
|
|
|
|
const [topCreators, topCreatorScores] = await toUserScores(creatorScores)
|
|
|
|
|
|
|
|
const traderScores = scoreTraders(contracts, bets)
|
|
|
|
const [topTraders, topTraderScores] = await toUserScores(traderScores)
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
fold,
|
|
|
|
curator,
|
|
|
|
contracts,
|
|
|
|
activeContracts,
|
|
|
|
activeContractBets,
|
|
|
|
activeContractComments,
|
|
|
|
topTraders,
|
|
|
|
topTraderScores,
|
|
|
|
topCreators,
|
|
|
|
topCreatorScores,
|
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function toUserScores(userScores: { [userId: string]: number }) {
|
|
|
|
const topUserPairs = _.take(
|
|
|
|
_.sortBy(Object.entries(userScores), ([_, score]) => -1 * score),
|
|
|
|
10
|
|
|
|
)
|
|
|
|
const topUsers = await Promise.all(
|
|
|
|
topUserPairs.map(([userId]) => getUser(userId))
|
|
|
|
)
|
|
|
|
const topUserScores = topUserPairs.map(([_, score]) => score)
|
|
|
|
return [topUsers, topUserScores] as const
|
|
|
|
}
|
|
|
|
|
|
|
|
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[][]
|
|
|
|
topTraders: User[]
|
|
|
|
topTraderScores: number[]
|
|
|
|
topCreators: User[]
|
|
|
|
topCreatorScores: number[]
|
|
|
|
}) {
|
|
|
|
const {
|
|
|
|
curator,
|
|
|
|
contracts,
|
|
|
|
activeContracts,
|
|
|
|
activeContractBets,
|
|
|
|
activeContractComments,
|
|
|
|
topTraders,
|
|
|
|
topTraderScores,
|
|
|
|
topCreators,
|
|
|
|
topCreatorScores,
|
|
|
|
} = props
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const { slugs } = router.query as { slugs: string[] }
|
|
|
|
|
2022-01-25 21:44:28 +00:00
|
|
|
const page = (slugs[1] ?? 'activity') as typeof foldSubpages[number]
|
|
|
|
|
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 { query, setQuery, sort, setSort } = useQueryAndSortParams({
|
|
|
|
defaultSort: 'most-traded',
|
|
|
|
})
|
|
|
|
|
|
|
|
const user = useUser()
|
2022-01-25 21:44:28 +00:00
|
|
|
const isCurator = user && fold && user.id === fold.curatorId
|
|
|
|
|
|
|
|
if (fold === null || !foldSubpages.includes(page) || slugs[2]) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
2022-01-25 20:47:25 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Page wide>
|
2022-01-26 18:02:53 +00:00
|
|
|
<Row className="justify-between mb-6 px-1">
|
|
|
|
<Title className="!m-0" text={fold.name} />
|
2022-01-26 20:03:32 +00:00
|
|
|
{isCurator ? (
|
|
|
|
<EditFoldButton className="ml-1" fold={fold} />
|
|
|
|
) : (
|
|
|
|
<FollowFoldButton className="ml-1" fold={fold} />
|
|
|
|
)}
|
2022-01-26 18:02:53 +00:00
|
|
|
</Row>
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-01-26 18:02:53 +00:00
|
|
|
<Col className="md:hidden text-gray-500 gap-2 mb-6 px-1">
|
|
|
|
<Row>
|
|
|
|
<div className="mr-1">Curated by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={curator.name}
|
|
|
|
username={curator.username}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
<div>{fold.about}</div>
|
|
|
|
</Col>
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-01-26 18:02:53 +00:00
|
|
|
<div className="tabs mb-2">
|
|
|
|
<Link href={foldPath(fold)} shallow>
|
|
|
|
<a
|
|
|
|
className={clsx(
|
|
|
|
'tab tab-bordered',
|
|
|
|
page === 'activity' && 'tab-active'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
Activity
|
|
|
|
</a>
|
|
|
|
</Link>
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-01-26 18:02:53 +00:00
|
|
|
<Link href={foldPath(fold, 'markets')} shallow>
|
|
|
|
<a
|
|
|
|
className={clsx(
|
|
|
|
'tab tab-bordered',
|
|
|
|
page === 'markets' && 'tab-active'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
Markets
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
<Link href={foldPath(fold, 'leaderboards')} shallow>
|
|
|
|
<a
|
|
|
|
className={clsx(
|
|
|
|
'tab tab-bordered',
|
|
|
|
page === 'leaderboards' && 'tab-active',
|
|
|
|
page !== 'leaderboards' && 'md:hidden'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
Leaderboards
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-01-26 18:02:53 +00:00
|
|
|
{(page === 'activity' || page === 'markets') && (
|
|
|
|
<Row className={clsx(page === 'activity' ? 'gap-16' : 'gap-8')}>
|
|
|
|
<Col className="flex-1">
|
2022-01-27 18:45:35 +00:00
|
|
|
{user !== null && (
|
|
|
|
<FeedCreate
|
|
|
|
user={user}
|
|
|
|
tag={toCamelCase(fold.name)}
|
|
|
|
className={clsx(page !== 'activity' && 'hidden')}
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-26 18:02:53 +00:00
|
|
|
{page === 'activity' ? (
|
2022-01-26 20:39:25 +00:00
|
|
|
<>
|
|
|
|
<ActivityFeed
|
|
|
|
contracts={activeContracts}
|
|
|
|
contractBets={activeContractBets}
|
|
|
|
contractComments={activeContractComments}
|
|
|
|
/>
|
|
|
|
{activeContracts.length === 0 && (
|
|
|
|
<div className="text-gray-500 mt-4">
|
|
|
|
No activity from matching markets.{' '}
|
|
|
|
{isCurator && 'Try editing to add more tags!'}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
2022-01-26 18:02:53 +00:00
|
|
|
) : (
|
2022-01-25 20:47:25 +00:00
|
|
|
<SearchableGrid
|
|
|
|
contracts={contracts}
|
|
|
|
query={query}
|
|
|
|
setQuery={setQuery}
|
|
|
|
sort={sort}
|
|
|
|
setSort={setSort}
|
|
|
|
/>
|
2022-01-26 18:02:53 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
2022-01-27 06:38:22 +00:00
|
|
|
<Col className="hidden md:flex max-w-xs w-full gap-10">
|
2022-01-26 18:02:53 +00:00
|
|
|
<FoldOverview fold={fold} curator={curator} />
|
|
|
|
<FoldLeaderboards
|
|
|
|
topTraders={topTraders}
|
|
|
|
topTraderScores={topTraderScores}
|
|
|
|
topCreators={topCreators}
|
|
|
|
topCreatorScores={topCreatorScores}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)}
|
2022-01-25 20:47:25 +00:00
|
|
|
|
2022-01-26 18:02:53 +00:00
|
|
|
{page === 'leaderboards' && (
|
|
|
|
<Col className="gap-8 lg:flex-row">
|
|
|
|
<FoldLeaderboards
|
|
|
|
topTraders={topTraders}
|
|
|
|
topTraderScores={topTraderScores}
|
|
|
|
topCreators={topCreators}
|
|
|
|
topCreatorScores={topCreatorScores}
|
|
|
|
/>
|
2022-01-25 20:47:25 +00:00
|
|
|
</Col>
|
2022-01-26 18:02:53 +00:00
|
|
|
)}
|
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-01-26 18:02:53 +00:00
|
|
|
<div className="px-4 py-3 bg-indigo-500 text-white text-sm rounded-t">
|
2022-01-25 20:47:25 +00:00
|
|
|
About community
|
|
|
|
</div>
|
2022-01-26 18:02:53 +00:00
|
|
|
<Col className="p-4 bg-white gap-2 rounded-b">
|
2022-01-25 20:47:25 +00:00
|
|
|
<Row>
|
|
|
|
<div className="text-gray-500 mr-1">Curated by</div>
|
|
|
|
<UserLink
|
|
|
|
className="text-neutral"
|
|
|
|
name={curator.name}
|
|
|
|
username={curator.username}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
{about && (
|
|
|
|
<>
|
|
|
|
<Spacer h={2} />
|
|
|
|
<div className="text-gray-500">{about}</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Spacer h={2} />
|
|
|
|
|
|
|
|
<TagsList tags={tags.map((tag) => `#${tag}`)} />
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function FoldLeaderboards(props: {
|
|
|
|
topTraders: User[]
|
|
|
|
topTraderScores: number[]
|
|
|
|
topCreators: User[]
|
|
|
|
topCreatorScores: number[]
|
|
|
|
}) {
|
|
|
|
const { topTraders, topTraderScores, topCreators, topCreatorScores } = props
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
|
|
|
title="🏅 Top traders"
|
|
|
|
users={topTraders}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Profit',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(topTraderScores[topTraders.indexOf(user)]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
<Leaderboard
|
|
|
|
className="max-w-xl"
|
|
|
|
title="🏅 Top creators"
|
|
|
|
users={topCreators}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Market pool',
|
|
|
|
renderCell: (user) =>
|
|
|
|
formatMoney(topCreatorScores[topCreators.indexOf(user)]),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|