manifold/web/pages/embed/[username]/[contractSlug].tsx

146 lines
3.9 KiB
TypeScript
Raw Normal View History

2022-03-20 17:45:17 +00:00
import { Bet } from '../../../../common/bet'
2022-03-20 21:05:16 +00:00
import {
Contract,
DPM,
FreeResponse,
FullContract,
} from '../../../../common/contract'
import { DOMAIN } from '../../../../common/envs/constants'
2022-03-20 21:05:16 +00:00
import { AnswersGraph } from '../../../components/answers/answers-graph'
import {
ResolutionOrChance,
ContractDetails,
} from '../../../components/contract-card'
import { ContractProbGraph } from '../../../components/contract-prob-graph'
2022-03-20 17:45:17 +00:00
import { Col } from '../../../components/layout/col'
2022-03-20 21:05:16 +00:00
import { Row } from '../../../components/layout/row'
2022-03-20 17:45:17 +00:00
import { Spacer } from '../../../components/layout/spacer'
2022-03-20 21:05:16 +00:00
import { Linkify } from '../../../components/linkify'
import { SiteLink } from '../../../components/site-link'
2022-03-20 17:45:17 +00:00
import { useContractWithPreload } from '../../../hooks/use-contract'
import { useMeasureSize } from '../../../hooks/use-measure-size'
2022-03-20 17:45:17 +00:00
import { fromPropz, usePropz } from '../../../hooks/use-propz'
import { useWindowSize } from '../../../hooks/use-window-size'
2022-03-20 17:45:17 +00:00
import { listAllBets } from '../../../lib/firebase/bets'
import {
contractPath,
getContractFromSlug,
} from '../../../lib/firebase/contracts'
2022-03-20 17:45:17 +00:00
import Custom404 from '../../404'
export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: {
params: { username: string; contractSlug: string }
}) {
const { username, contractSlug } = props.params
const contract = (await getContractFromSlug(contractSlug)) || null
const contractId = contract?.id
2022-03-20 21:05:16 +00:00
const bets = contractId ? await listAllBets(contractId) : []
2022-03-20 17:45:17 +00:00
return {
props: {
contract,
username,
slug: contractSlug,
bets,
},
revalidate: 60, // regenerate after a minute
}
}
export async function getStaticPaths() {
return { paths: [], fallback: 'blocking' }
}
2022-03-20 21:05:16 +00:00
export default function ContractEmbedPage(props: {
2022-03-20 17:45:17 +00:00
contract: Contract | null
username: string
bets: Bet[]
slug: string
}) {
props = usePropz(props, getStaticPropz) ?? {
contract: null,
username: '',
bets: [],
slug: '',
}
const contract = useContractWithPreload(props.slug, props.contract)
2022-03-20 21:05:16 +00:00
const { bets } = props
2022-03-20 17:45:17 +00:00
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
if (!contract) {
return <Custom404 />
}
2022-03-20 21:05:16 +00:00
return <ContractEmbed contract={contract} bets={bets} />
}
function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
const { contract, bets } = props
const { question, resolution, outcomeType } = contract
const isBinary = outcomeType === 'BINARY'
const href = `https://${DOMAIN}${contractPath(contract)}`
const { height: windowHeight } = useWindowSize()
const { setElem, height: topSectionHeight } = useMeasureSize()
const paddingBottom = 8
const graphHeight =
windowHeight && topSectionHeight
? windowHeight - topSectionHeight - paddingBottom
: 0
2022-03-20 17:45:17 +00:00
return (
<Col className="w-full flex-1 bg-white">
<div className="flex flex-col relative pt-2" ref={setElem}>
<SiteLink
className="absolute top-0 left-0 w-full h-full z-20"
href={href}
/>
<div className="px-3 text-xl md:text-2xl text-indigo-700">
<Linkify text={question} />
</div>
2022-03-20 21:05:16 +00:00
<Spacer h={3} />
2022-03-20 21:05:16 +00:00
<Row className="items-center justify-between gap-4 px-2">
2022-03-21 20:23:21 +00:00
<ContractDetails
contract={contract}
isCreator={false}
hideShareButtons
/>
2022-03-20 21:05:16 +00:00
{(isBinary || resolution) && (
<ResolutionOrChance contract={contract} />
)}
</Row>
2022-03-20 21:05:16 +00:00
<Spacer h={2} />
</div>
2022-03-20 21:05:16 +00:00
<div className="mx-1" style={{ paddingBottom }}>
2022-03-20 21:05:16 +00:00
{isBinary ? (
<ContractProbGraph
contract={contract}
bets={bets}
height={graphHeight}
/>
2022-03-20 21:05:16 +00:00
) : (
<AnswersGraph
contract={contract as FullContract<DPM, FreeResponse>}
bets={bets}
height={graphHeight}
2022-03-20 21:05:16 +00:00
/>
2022-03-20 17:45:17 +00:00
)}
2022-03-20 21:05:16 +00:00
</div>
2022-03-20 17:45:17 +00:00
</Col>
)
}