2022-08-24 00:25:57 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-04-11 21:13:26 +00:00
|
|
|
import { ArrowLeftIcon } from '@heroicons/react/outline'
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useContractWithPreload } from 'web/hooks/use-contract'
|
|
|
|
import { ContractOverview } from 'web/components/contract/contract-overview'
|
|
|
|
import { BetPanel } from 'web/components/bet-panel'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
2022-08-24 00:25:57 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { ResolutionPanel } from 'web/components/resolution-panel'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
2022-01-02 04:52:55 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
getContractFromSlug,
|
2022-01-26 20:08:03 +00:00
|
|
|
tradingAllowed,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
|
|
|
import { SEO } from 'web/components/SEO'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { listAllComments } from 'web/lib/firebase/comments'
|
2022-01-16 03:09:15 +00:00
|
|
|
import Custom404 from '../404'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { AnswersPanel } from 'web/components/answers/answers-panel'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
|
|
|
import { ContractTabs } from 'web/components/contract/contract-tabs'
|
2022-08-11 19:55:25 +00:00
|
|
|
import { FullscreenConfetti } from 'web/components/fullscreen-confetti'
|
2022-08-04 21:27:02 +00:00
|
|
|
import { NumericBetPanel } from 'web/components/numeric-bet-panel'
|
|
|
|
import { NumericResolutionPanel } from 'web/components/numeric-resolution-panel'
|
2022-05-20 00:34:08 +00:00
|
|
|
import { useIsIframe } from 'web/hooks/use-is-iframe'
|
|
|
|
import ContractEmbedPage from '../embed/[username]/[contractSlug]'
|
2022-06-10 17:23:27 +00:00
|
|
|
import { useBets } from 'web/hooks/use-bets'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { CPMMBinaryContract } from 'common/contract'
|
2022-06-14 02:09:09 +00:00
|
|
|
import { AlertBox } from 'web/components/alert-box'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { useTracking } from 'web/hooks/use-tracking'
|
2022-08-24 00:25:57 +00:00
|
|
|
import { useTipTxns } from 'web/hooks/use-tip-txns'
|
2022-07-21 19:43:10 +00:00
|
|
|
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
2022-08-04 21:27:02 +00:00
|
|
|
import { User } from 'common/user'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from 'common/comment'
|
2022-08-19 17:43:57 +00:00
|
|
|
import { getOpenGraphProps } from 'common/contract-details'
|
2022-08-24 00:25:57 +00:00
|
|
|
import {
|
|
|
|
ContractLeaderboard,
|
|
|
|
ContractTopTrades,
|
|
|
|
} from 'web/components/contract/contract-leaderboard'
|
2021-12-09 21:31:02 +00:00
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz(props: {
|
2022-01-21 23:21:46 +00:00
|
|
|
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-04-11 21:13:26 +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
|
|
|
|
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-07-14 19:57:17 +00:00
|
|
|
// Limit the data sent to the client. Client will still load all bets and comments directly.
|
|
|
|
bets: bets.slice(0, 5000),
|
|
|
|
comments: comments.slice(0, 1000),
|
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[]
|
2022-08-19 08:06:40 +00:00
|
|
|
comments: ContractComment[]
|
2022-02-01 18:06:42 +00:00
|
|
|
slug: string
|
2022-04-11 21:13:26 +00:00
|
|
|
backToHome?: () => void
|
2021-12-16 06:36:51 +00:00
|
|
|
}) {
|
2022-03-09 02:43:30 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
contract: null,
|
|
|
|
username: '',
|
|
|
|
comments: [],
|
|
|
|
bets: [],
|
|
|
|
slug: '',
|
|
|
|
}
|
2022-06-10 17:23:27 +00:00
|
|
|
|
2022-08-09 22:28:52 +00:00
|
|
|
const user = useUser()
|
2022-06-10 17:23:27 +00:00
|
|
|
const inIframe = useIsIframe()
|
|
|
|
if (inIframe) {
|
|
|
|
return <ContractEmbedPage {...props} />
|
|
|
|
}
|
|
|
|
|
2022-06-13 21:05:46 +00:00
|
|
|
const { contract } = props
|
|
|
|
|
2022-06-10 17:23:27 +00:00
|
|
|
if (!contract) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
|
|
|
|
2022-08-09 22:28:52 +00:00
|
|
|
return <ContractPageContent {...{ ...props, contract, user }} />
|
2022-04-11 21:13:26 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
export function ContractPageSidebar(props: {
|
|
|
|
user: User | null | undefined
|
|
|
|
contract: Contract
|
|
|
|
}) {
|
|
|
|
const { contract, user } = props
|
|
|
|
const { creatorId, isResolved, outcomeType } = contract
|
|
|
|
|
|
|
|
const isCreator = user?.id === creatorId
|
|
|
|
const isBinary = outcomeType === 'BINARY'
|
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
const isNumeric = outcomeType === 'NUMERIC'
|
|
|
|
const allowTrade = tradingAllowed(contract)
|
|
|
|
const allowResolve = !isResolved && isCreator && !!user
|
|
|
|
const hasSidePanel =
|
|
|
|
(isBinary || isNumeric || isPseudoNumeric) && (allowTrade || allowResolve)
|
|
|
|
|
|
|
|
return hasSidePanel ? (
|
|
|
|
<Col className="gap-4">
|
|
|
|
{allowTrade &&
|
|
|
|
(isNumeric ? (
|
|
|
|
<NumericBetPanel className="hidden xl:flex" contract={contract} />
|
|
|
|
) : (
|
|
|
|
<BetPanel
|
|
|
|
className="hidden xl:flex"
|
|
|
|
contract={contract as CPMMBinaryContract}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{allowResolve &&
|
|
|
|
(isNumeric || isPseudoNumeric ? (
|
|
|
|
<NumericResolutionPanel creator={user} contract={contract} />
|
|
|
|
) : (
|
|
|
|
<ResolutionPanel creator={user} contract={contract} />
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
2022-06-10 17:23:27 +00:00
|
|
|
export function ContractPageContent(
|
2022-08-09 22:28:52 +00:00
|
|
|
props: Parameters<typeof ContractPage>[0] & {
|
|
|
|
contract: Contract
|
|
|
|
user?: User | null
|
|
|
|
}
|
2022-06-10 17:23:27 +00:00
|
|
|
) {
|
2022-08-09 22:28:52 +00:00
|
|
|
const { backToHome, comments, user } = props
|
2022-06-13 21:05:46 +00:00
|
|
|
|
|
|
|
const contract = useContractWithPreload(props.contract) ?? props.contract
|
2022-06-10 17:23:27 +00:00
|
|
|
|
2022-06-15 21:34:34 +00:00
|
|
|
useTracking('view market', {
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
|
|
|
creatorId: contract.creatorId,
|
|
|
|
})
|
|
|
|
|
2022-06-10 17:23:27 +00:00
|
|
|
const bets = useBets(contract.id) ?? props.bets
|
2022-08-11 19:53:54 +00:00
|
|
|
|
2022-06-10 17:23:27 +00:00
|
|
|
// Sort for now to see if bug is fixed.
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
2022-04-11 21:13:26 +00:00
|
|
|
|
2022-07-07 23:23:13 +00:00
|
|
|
const tips = useTipTxns({ contractId: contract.id })
|
2022-06-18 03:28:16 +00:00
|
|
|
|
2022-04-28 23:01:50 +00:00
|
|
|
const [showConfetti, setShowConfetti] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const shouldSeeConfetti = !!(
|
|
|
|
user &&
|
|
|
|
contract.creatorId === user.id &&
|
|
|
|
Date.now() - contract.createdTime < 10 * 1000
|
|
|
|
)
|
|
|
|
setShowConfetti(shouldSeeConfetti)
|
|
|
|
}, [contract, user])
|
2022-02-06 22:55:14 +00:00
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
const { isResolved, question, outcomeType } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-01-26 20:08:03 +00:00
|
|
|
const allowTrade = tradingAllowed(contract)
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-02-18 00:34:11 +00:00
|
|
|
const ogCardProps = getOpenGraphProps(contract)
|
2022-01-10 07:05:24 +00:00
|
|
|
|
2022-07-21 19:43:10 +00:00
|
|
|
useSaveReferral(user, {
|
2022-08-04 21:27:02 +00:00
|
|
|
defaultReferrerUsername: contract.creatorUsername,
|
2022-07-21 19:43:10 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
})
|
2022-07-01 13:47:19 +00:00
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
const rightSidebar = <ContractPageSidebar user={user} contract={contract} />
|
2021-12-09 22:05:55 +00:00
|
|
|
return (
|
2022-03-31 05:35:20 +00:00
|
|
|
<Page rightSidebar={rightSidebar}>
|
2022-04-28 23:01:50 +00:00
|
|
|
{showConfetti && (
|
2022-08-11 19:55:25 +00:00
|
|
|
<FullscreenConfetti recycle={false} numberOfPieces={300} />
|
2022-04-28 23:01:50 +00:00
|
|
|
)}
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
{ogCardProps && (
|
|
|
|
<SEO
|
|
|
|
title={question}
|
|
|
|
description={ogCardProps.description}
|
|
|
|
url={`/${props.username}/${props.slug}`}
|
|
|
|
ogCardProps={ogCardProps}
|
|
|
|
/>
|
|
|
|
)}
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2022-05-23 22:09:40 +00:00
|
|
|
<Col className="w-full justify-between rounded border-0 border-gray-100 bg-white py-6 pl-1 pr-2 sm:px-2 md:px-6 md:py-8">
|
2022-04-11 21:13:26 +00:00
|
|
|
{backToHome && (
|
|
|
|
<button
|
|
|
|
className="btn btn-sm mb-4 items-center gap-2 self-start border-0 border-gray-700 bg-white normal-case text-gray-700 hover:bg-white hover:text-gray-700 lg:hidden"
|
|
|
|
onClick={backToHome}
|
|
|
|
>
|
|
|
|
<ArrowLeftIcon className="h-5 w-5 text-gray-700" />
|
|
|
|
Back
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
2022-08-04 21:27:02 +00:00
|
|
|
<ContractOverview
|
|
|
|
contract={contract}
|
|
|
|
bets={bets.filter((b) => !b.challengeSlug)}
|
|
|
|
/>
|
2022-07-02 19:37:59 +00:00
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
{outcomeType === 'NUMERIC' && (
|
2022-06-14 02:09:09 +00:00
|
|
|
<AlertBox
|
|
|
|
title="Warning"
|
2022-07-02 19:37:59 +00:00
|
|
|
text="Distributional numeric markets were introduced as an experimental feature and are now deprecated."
|
2022-06-14 02:09:09 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-04-18 23:02:40 +00:00
|
|
|
|
2022-07-28 02:40:33 +00:00
|
|
|
{(outcomeType === 'FREE_RESPONSE' ||
|
|
|
|
outcomeType === 'MULTIPLE_CHOICE') && (
|
2022-05-03 21:54:00 +00:00
|
|
|
<>
|
|
|
|
<Spacer h={4} />
|
2022-06-01 02:42:35 +00:00
|
|
|
<AnswersPanel contract={contract} />
|
2022-05-03 21:54:00 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-03 21:57:38 +00:00
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
{outcomeType === 'NUMERIC' && allowTrade && (
|
2022-06-01 02:42:35 +00:00
|
|
|
<NumericBetPanel className="xl:hidden" contract={contract} />
|
2022-05-19 17:42:03 +00:00
|
|
|
)}
|
|
|
|
|
2022-05-03 20:57:39 +00:00
|
|
|
{isResolved && (
|
2022-04-03 21:57:38 +00:00
|
|
|
<>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2">
|
2022-05-10 21:22:57 +00:00
|
|
|
<ContractLeaderboard contract={contract} bets={bets} />
|
2022-04-03 21:57:38 +00:00
|
|
|
<ContractTopTrades
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
comments={comments}
|
2022-06-18 03:28:16 +00:00
|
|
|
tips={tips}
|
2022-04-03 21:57:38 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Spacer h={12} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-08 21:13:10 +00:00
|
|
|
|
|
|
|
<ContractTabs
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
bets={bets}
|
2022-06-18 03:28:16 +00:00
|
|
|
tips={tips}
|
2022-04-08 21:13:10 +00:00
|
|
|
comments={comments}
|
|
|
|
/>
|
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
|
|
|
)
|
|
|
|
}
|