manifold/web/pages/[username]/[slug].tsx

117 lines
3.1 KiB
TypeScript
Raw Normal View History

import React from 'react'
2021-12-16 03:14:00 +00:00
import clsx from 'clsx'
2021-12-16 18:21:16 +00:00
import { useContractWithPreload } from '../../hooks/use-contract'
import { Header } from '../../components/header'
import { ContractOverview } from '../../components/contract-overview'
import { BetPanel } from '../../components/bet-panel'
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'
2021-12-17 23:16:42 +00:00
import { Contract, getContractFromSlug } from '../../lib/firebase/contracts'
2021-12-16 18:40:23 +00:00
import { SEO } from '../../components/SEO'
2021-12-09 21:31:02 +00:00
export async function getStaticProps(props: { params: any }) {
2021-12-17 23:16:42 +00:00
const { username, slug } = props.params
const contract = (await getContractFromSlug(slug)) || null
2021-12-09 22:05:55 +00:00
2021-12-16 03:24:11 +00:00
return {
props: {
2021-12-16 18:40:23 +00:00
username,
2021-12-17 23:16:42 +00:00
slug,
contract,
2021-12-16 03:24:11 +00:00
},
2021-12-09 22:05:55 +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
export async function getStaticPaths() {
return { paths: [], fallback: 'blocking' }
}
2021-12-16 03:14:00 +00:00
export default function ContractPage(props: {
2021-12-16 18:21:16 +00:00
contract: Contract | null
2021-12-17 23:16:42 +00:00
slug: string
2021-12-16 18:40:23 +00:00
username: string
}) {
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)
2021-12-09 22:05:55 +00:00
if (!contract) {
2021-12-09 22:05:55 +00:00
return <div>Contract not found...</div>
}
2021-12-14 06:12:25 +00:00
const { creatorId, isResolved } = contract
const isCreator = user?.id === creatorId
2021-12-14 00:00:02 +00:00
2021-12-09 22:05:55 +00:00
return (
2021-12-18 23:40:39 +00:00
<Col
className={clsx(
'px-4 pb-8 mx-auto',
isResolved ? 'max-w-4xl' : 'max-w-7xl'
)}
>
2021-12-16 18:40:23 +00:00
<SEO
title={contract.question}
description={contract.description}
2021-12-17 23:16:42 +00:00
url={`/${props.username}/${props.slug}`}
2021-12-16 18:40:23 +00:00
/>
2021-12-16 03:14:00 +00:00
<Header />
2021-12-18 23:40:39 +00:00
<Col className="w-full md:flex-row justify-between mt-6">
<div className="flex-[3]">
<ContractOverview contract={contract} />
2021-12-16 09:32:52 +00:00
<BetsSection contract={contract} user={user ?? null} />
</div>
{!isResolved && (
<>
2021-12-18 23:40:39 +00:00
<div className="md:ml-8" />
2021-12-18 23:40:39 +00:00
<Col className="flex-1">
<BetPanel contract={contract} />
2021-12-16 04:52:07 +00:00
{isCreator && user && (
<ResolutionPanel creator={user} contract={contract} />
)}
</Col>
</>
2021-12-14 00:00:02 +00:00
)}
2021-12-12 22:14:52 +00:00
</Col>
</Col>
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)
if (bets === 'loading' || bets.length === 0) return <></>
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)
if (!userBets || userBets.length === 0) return <></>
2021-12-16 04:30:24 +00:00
return (
2021-12-18 23:40:39 +00:00
<div>
<Spacer h={6} />
<Title text="Your bets" />
<MyBetsSummary contract={contract} bets={userBets} />
<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>
)
}