2021-12-10 14:56:17 +00:00
|
|
|
import React from 'react'
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2021-12-16 18:21:16 +00:00
|
|
|
import { useContractWithPreload } from '../../hooks/use-contract'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { ContractOverview } from '../../components/contract-overview'
|
|
|
|
import { BetPanel } from '../../components/bet-panel'
|
2021-12-10 18:52:11 +00:00
|
|
|
import { Col } from '../../components/layout/col'
|
2021-12-14 00:00:02 +00:00
|
|
|
import { useUser } from '../../hooks/use-user'
|
|
|
|
import { ResolutionPanel } from '../../components/resolution-panel'
|
2021-12-16 04:30:24 +00:00
|
|
|
import { ContractBetsTable, MyBetsSummary } from '../../components/bets-list'
|
|
|
|
import { useBets } from '../../hooks/use-bets'
|
|
|
|
import { Title } from '../../components/title'
|
|
|
|
import { Spacer } from '../../components/layout/spacer'
|
|
|
|
import { User } from '../../lib/firebase/users'
|
2022-01-02 04:52:55 +00:00
|
|
|
import {
|
2022-01-12 19:01:04 +00:00
|
|
|
contractMetrics,
|
2022-01-02 04:52:55 +00:00
|
|
|
Contract,
|
|
|
|
getContractFromSlug,
|
2022-01-26 20:08:03 +00:00
|
|
|
tradingAllowed,
|
2022-01-02 04:52:55 +00:00
|
|
|
} from '../../lib/firebase/contracts'
|
2021-12-16 18:40:23 +00:00
|
|
|
import { SEO } from '../../components/SEO'
|
2021-12-20 04:06:30 +00:00
|
|
|
import { Page } from '../../components/page'
|
2022-01-10 07:05:24 +00:00
|
|
|
import { contractTextDetails } from '../../components/contract-card'
|
2022-01-15 00:16:25 +00:00
|
|
|
import { Bet, listAllBets } from '../../lib/firebase/bets'
|
|
|
|
import { Comment, listAllComments } from '../../lib/firebase/comments'
|
2022-01-16 03:09:15 +00:00
|
|
|
import Custom404 from '../404'
|
2022-02-01 18:06:42 +00:00
|
|
|
import { getFoldsByTags } from '../../lib/firebase/folds'
|
|
|
|
import { Fold } from '../../../common/fold'
|
2022-02-06 22:55:14 +00:00
|
|
|
import { useFoldsWithTags } from '../../hooks/use-fold'
|
2021-12-09 21:31:02 +00:00
|
|
|
|
2022-01-21 23:21:46 +00:00
|
|
|
export async function getStaticProps(props: {
|
|
|
|
params: { username: string; contractSlug: string }
|
|
|
|
}) {
|
2021-12-19 05:50:47 +00:00
|
|
|
const { username, contractSlug } = props.params
|
|
|
|
const contract = (await getContractFromSlug(contractSlug)) || null
|
2022-01-15 00:16:25 +00:00
|
|
|
const contractId = contract?.id
|
|
|
|
|
2022-02-01 18:06:42 +00:00
|
|
|
const foldsPromise = getFoldsByTags(contract?.tags ?? [])
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
const [bets, comments] = await Promise.all([
|
2022-02-01 18:06:42 +00:00
|
|
|
contractId ? listAllBets(contractId) : [],
|
|
|
|
contractId ? listAllComments(contractId) : [],
|
2022-01-15 00:16:25 +00:00
|
|
|
])
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2022-02-01 18:06:42 +00:00
|
|
|
const folds = await foldsPromise
|
|
|
|
|
2021-12-16 03:24:11 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-02-01 18:06:42 +00:00
|
|
|
contract,
|
2021-12-16 18:40:23 +00:00
|
|
|
username,
|
2021-12-19 05:50:47 +00:00
|
|
|
slug: contractSlug,
|
2022-01-15 00:16:25 +00:00
|
|
|
bets,
|
|
|
|
comments,
|
2022-02-01 18:06:42 +00:00
|
|
|
folds,
|
2021-12-16 03:24:11 +00:00
|
|
|
},
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
revalidate: 60, // regenerate after a minute
|
2021-12-09 22:05:55 +00:00
|
|
|
}
|
2021-12-16 03:24:11 +00:00
|
|
|
}
|
2021-12-09 21:31:02 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
export async function getStaticPaths() {
|
|
|
|
return { paths: [], fallback: 'blocking' }
|
|
|
|
}
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
export default function ContractPage(props: {
|
2021-12-16 18:21:16 +00:00
|
|
|
contract: Contract | null
|
2021-12-16 18:40:23 +00:00
|
|
|
username: string
|
2022-02-01 18:06:42 +00:00
|
|
|
bets: Bet[]
|
|
|
|
comments: Comment[]
|
|
|
|
slug: string
|
|
|
|
folds: Fold[]
|
2021-12-16 06:36:51 +00:00
|
|
|
}) {
|
2021-12-16 05:56:12 +00:00
|
|
|
const user = useUser()
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-17 23:16:42 +00:00
|
|
|
const contract = useContractWithPreload(props.slug, props.contract)
|
2022-02-06 22:55:14 +00:00
|
|
|
const { bets, comments } = props
|
|
|
|
|
|
|
|
const folds = useFoldsWithTags(contract?.tags) ?? props.folds
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
if (!contract) {
|
2022-01-16 03:09:15 +00:00
|
|
|
return <Custom404 />
|
2021-12-09 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-02 04:52:55 +00:00
|
|
|
const { creatorId, isResolved, resolution, question } = contract
|
2021-12-14 06:12:25 +00:00
|
|
|
const isCreator = user?.id === creatorId
|
2022-01-26 20:08:03 +00:00
|
|
|
const allowTrade = tradingAllowed(contract)
|
2022-01-10 16:32:54 +00:00
|
|
|
const allowResolve = !isResolved && isCreator && !!user
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const { probPercent } = contractMetrics(contract)
|
2022-01-02 18:57:52 +00:00
|
|
|
|
|
|
|
const description = resolution
|
|
|
|
? `Resolved ${resolution}. ${contract.description}`
|
|
|
|
: `${probPercent} chance. ${contract.description}`
|
2022-01-02 04:52:55 +00:00
|
|
|
|
2022-01-10 07:05:24 +00:00
|
|
|
const ogCardProps = {
|
|
|
|
question,
|
|
|
|
probability: probPercent,
|
|
|
|
metadata: contractTextDetails(contract),
|
|
|
|
creatorName: contract.creatorName,
|
|
|
|
creatorUsername: contract.creatorUsername,
|
|
|
|
}
|
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
return (
|
2022-01-10 16:32:54 +00:00
|
|
|
<Page wide={allowTrade || allowResolve}>
|
2021-12-16 18:40:23 +00:00
|
|
|
<SEO
|
2022-01-02 18:57:52 +00:00
|
|
|
title={question}
|
|
|
|
description={description}
|
2021-12-17 23:16:42 +00:00
|
|
|
url={`/${props.username}/${props.slug}`}
|
2022-01-10 07:05:24 +00:00
|
|
|
ogCardProps={ogCardProps}
|
2021-12-16 18:40:23 +00:00
|
|
|
/>
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2022-01-27 22:37:43 +00:00
|
|
|
<Col className="w-full md:flex-row justify-between">
|
|
|
|
<div className="flex-[3] bg-white px-2 py-6 md:px-6 md:py-8 rounded border-0 border-gray-100">
|
2022-01-15 00:16:25 +00:00
|
|
|
<ContractOverview
|
|
|
|
contract={contract}
|
|
|
|
bets={bets ?? []}
|
|
|
|
comments={comments ?? []}
|
2022-02-01 18:06:42 +00:00
|
|
|
folds={folds}
|
2022-01-15 00:16:25 +00:00
|
|
|
/>
|
2021-12-16 09:32:52 +00:00
|
|
|
<BetsSection contract={contract} user={user ?? null} />
|
|
|
|
</div>
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2022-01-03 05:21:25 +00:00
|
|
|
{(allowTrade || allowResolve) && (
|
2021-12-14 18:51:30 +00:00
|
|
|
<>
|
2022-01-27 22:37:43 +00:00
|
|
|
<div className="md:ml-6" />
|
2021-12-10 18:52:11 +00:00
|
|
|
|
2021-12-18 23:40:39 +00:00
|
|
|
<Col className="flex-1">
|
2022-01-26 22:28:57 +00:00
|
|
|
{allowTrade && (
|
|
|
|
<BetPanel className="hidden lg:inline" contract={contract} />
|
|
|
|
)}
|
2022-01-03 05:21:25 +00:00
|
|
|
{allowResolve && (
|
2021-12-15 00:29:58 +00:00
|
|
|
<ResolutionPanel creator={user} contract={contract} />
|
|
|
|
)}
|
|
|
|
</Col>
|
2021-12-14 18:51:30 +00:00
|
|
|
</>
|
2021-12-14 00:00:02 +00:00
|
|
|
)}
|
2021-12-12 22:14:52 +00:00
|
|
|
</Col>
|
2021-12-20 04:06:30 +00:00
|
|
|
</Page>
|
2021-12-09 22:05:55 +00:00
|
|
|
)
|
|
|
|
}
|
2021-12-16 04:30:24 +00:00
|
|
|
|
|
|
|
function BetsSection(props: { contract: Contract; user: User | null }) {
|
|
|
|
const { contract, user } = props
|
|
|
|
const bets = useBets(contract.id)
|
|
|
|
|
2022-01-15 00:16:25 +00:00
|
|
|
if (!bets || bets.length === 0) return <></>
|
2021-12-16 04:30:24 +00:00
|
|
|
|
2021-12-16 04:40:48 +00:00
|
|
|
// Decending creation time.
|
|
|
|
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
const userBets = user && bets.filter((bet) => bet.userId === user.id)
|
|
|
|
|
2021-12-16 21:22:00 +00:00
|
|
|
if (!userBets || userBets.length === 0) return <></>
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
return (
|
2021-12-18 23:40:39 +00:00
|
|
|
<div>
|
2022-01-13 18:58:25 +00:00
|
|
|
<Title className="px-2" text="Your trades" />
|
|
|
|
<MyBetsSummary
|
|
|
|
className="px-2"
|
|
|
|
contract={contract}
|
|
|
|
bets={userBets}
|
|
|
|
showMKT
|
|
|
|
/>
|
2021-12-16 21:22:00 +00:00
|
|
|
<Spacer h={6} />
|
|
|
|
<ContractBetsTable contract={contract} bets={userBets} />
|
2021-12-18 23:40:39 +00:00
|
|
|
<Spacer h={12} />
|
2021-12-16 04:30:24 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|