2022-10-12 18:05:58 +00:00
|
|
|
import React, { memo, useEffect, useMemo, useRef, useState } from 'react'
|
2022-04-11 21:13:26 +00:00
|
|
|
import { ArrowLeftIcon } from '@heroicons/react/outline'
|
2022-09-27 17:09:54 +00:00
|
|
|
import dayjs from 'dayjs'
|
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-08-28 03:26:37 +00:00
|
|
|
getRecommendedContracts,
|
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-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-07-21 19:43:10 +00:00
|
|
|
import { useSaveReferral } from 'web/hooks/use-save-referral'
|
2022-08-19 17:43:57 +00:00
|
|
|
import { getOpenGraphProps } from 'common/contract-details'
|
2022-09-01 21:42:50 +00:00
|
|
|
import { ContractDescription } from 'web/components/contract/contract-description'
|
2022-08-24 00:25:57 +00:00
|
|
|
import {
|
|
|
|
ContractLeaderboard,
|
|
|
|
ContractTopTrades,
|
|
|
|
} from 'web/components/contract/contract-leaderboard'
|
2022-08-24 17:59:21 +00:00
|
|
|
import { ContractsGrid } from 'web/components/contract/contracts-grid'
|
2022-08-28 03:26:37 +00:00
|
|
|
import { Title } from 'web/components/title'
|
2022-08-28 23:03:00 +00:00
|
|
|
import { usePrefetch } from 'web/hooks/use-prefetch'
|
2022-09-15 03:28:40 +00:00
|
|
|
import { useAdmin } from 'web/hooks/use-admin'
|
2022-09-27 17:09:54 +00:00
|
|
|
import { BetsSummary } from 'web/components/bet-summary'
|
2022-09-28 17:11:26 +00:00
|
|
|
import { listAllComments } from 'web/lib/firebase/comments'
|
|
|
|
import { ContractComment } from 'common/comment'
|
2022-09-30 20:16:27 +00:00
|
|
|
import { ScrollToTopButton } from 'web/components/scroll-to-top-button'
|
2022-10-12 18:05:58 +00:00
|
|
|
import { Answer } from 'common/answer'
|
|
|
|
import { useEvent } from 'web/hooks/use-event'
|
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 }
|
|
|
|
}) {
|
2022-09-21 07:02:10 +00:00
|
|
|
const { contractSlug } = props.params
|
2021-12-19 05:50:47 +00:00
|
|
|
const contract = (await getContractFromSlug(contractSlug)) || null
|
2022-01-15 00:16:25 +00:00
|
|
|
const contractId = contract?.id
|
2022-09-21 07:02:10 +00:00
|
|
|
const bets = contractId ? await listAllBets(contractId) : []
|
2022-09-28 17:11:26 +00:00
|
|
|
const comments = contractId ? await listAllComments(contractId) : []
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-16 03:24:11 +00:00
|
|
|
return {
|
2022-09-28 17:11:26 +00:00
|
|
|
props: {
|
|
|
|
contract,
|
|
|
|
// Limit the data sent to the client. Client will still load all bets/comments directly.
|
|
|
|
bets: bets.slice(0, 5000),
|
|
|
|
comments: comments.slice(0, 1000),
|
|
|
|
},
|
2022-09-07 03:12:18 +00:00
|
|
|
revalidate: 5, // regenerate after five seconds
|
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
|
2022-02-01 18:06:42 +00:00
|
|
|
bets: Bet[]
|
2022-09-28 17:11:26 +00:00
|
|
|
comments: ContractComment[]
|
2022-04-11 21:13:26 +00:00
|
|
|
backToHome?: () => void
|
2021-12-16 06:36:51 +00:00
|
|
|
}) {
|
2022-09-28 17:11:26 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
contract: null,
|
|
|
|
bets: [],
|
|
|
|
comments: [],
|
|
|
|
}
|
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-09-21 07:02:10 +00:00
|
|
|
return <ContractPageContent key={contract.id} {...{ ...props, contract }} />
|
2022-04-11 21:13:26 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 03:28:40 +00:00
|
|
|
// requires an admin to resolve a week after market closes
|
|
|
|
export function needsAdminToResolve(contract: Contract) {
|
|
|
|
return !contract.isResolved && dayjs().diff(contract.closeTime, 'day') > 7
|
|
|
|
}
|
|
|
|
|
2022-09-21 07:02:10 +00:00
|
|
|
export function ContractPageSidebar(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
2022-08-09 20:25:42 +00:00
|
|
|
const { creatorId, isResolved, outcomeType } = contract
|
2022-09-21 07:02:10 +00:00
|
|
|
const user = useUser()
|
2022-08-09 20:25:42 +00:00
|
|
|
const isCreator = user?.id === creatorId
|
|
|
|
const isBinary = outcomeType === 'BINARY'
|
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
const isNumeric = outcomeType === 'NUMERIC'
|
|
|
|
const allowTrade = tradingAllowed(contract)
|
2022-09-15 03:28:40 +00:00
|
|
|
const isAdmin = useAdmin()
|
|
|
|
const allowResolve =
|
|
|
|
!isResolved &&
|
|
|
|
(isCreator || (needsAdminToResolve(contract) && isAdmin)) &&
|
|
|
|
!!user
|
|
|
|
|
2022-08-09 20:25:42 +00:00
|
|
|
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 ? (
|
2022-09-15 03:28:40 +00:00
|
|
|
<NumericResolutionPanel
|
|
|
|
isAdmin={isAdmin}
|
|
|
|
creator={user}
|
|
|
|
isCreator={isCreator}
|
|
|
|
contract={contract}
|
|
|
|
/>
|
2022-08-09 20:25:42 +00:00
|
|
|
) : (
|
2022-09-15 03:28:40 +00:00
|
|
|
<ResolutionPanel
|
|
|
|
isAdmin={isAdmin}
|
|
|
|
creator={user}
|
|
|
|
isCreator={isCreator}
|
|
|
|
contract={contract}
|
|
|
|
/>
|
2022-08-09 20:25:42 +00:00
|
|
|
))}
|
|
|
|
</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
|
|
|
|
}
|
2022-06-10 17:23:27 +00:00
|
|
|
) {
|
2022-09-28 17:11:26 +00:00
|
|
|
const { backToHome, comments } = props
|
2022-06-13 21:05:46 +00:00
|
|
|
const contract = useContractWithPreload(props.contract) ?? props.contract
|
2022-09-21 07:02:10 +00:00
|
|
|
const user = useUser()
|
2022-10-03 16:11:24 +00:00
|
|
|
const isCreator = user?.id === contract.creatorId
|
2022-08-28 23:03:00 +00:00
|
|
|
usePrefetch(user?.id)
|
2022-09-07 03:43:28 +00:00
|
|
|
useTracking(
|
|
|
|
'view market',
|
|
|
|
{
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
|
|
|
creatorId: contract.creatorId,
|
|
|
|
},
|
|
|
|
true
|
|
|
|
)
|
2022-06-15 21:34:34 +00:00
|
|
|
|
2022-06-10 17:23:27 +00:00
|
|
|
const bets = useBets(contract.id) ?? props.bets
|
2022-08-27 08:09:17 +00:00
|
|
|
const nonChallengeBets = useMemo(
|
|
|
|
() => bets.filter((b) => !b.challengeSlug),
|
|
|
|
[bets]
|
|
|
|
)
|
2022-08-11 19:53:54 +00:00
|
|
|
|
2022-09-27 17:09:54 +00:00
|
|
|
const userBets = user
|
|
|
|
? bets.filter((bet) => !bet.isAnte && bet.userId === user.id)
|
|
|
|
: []
|
|
|
|
|
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-10-12 18:05:58 +00:00
|
|
|
const [answerResponse, setAnswerResponse] = useState<Answer | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
const tabsContainerRef = useRef<null | HTMLDivElement>(null)
|
|
|
|
const onAnswerCommentClick = useEvent((answer: Answer) => {
|
|
|
|
setAnswerResponse(answer)
|
|
|
|
if (tabsContainerRef.current) {
|
|
|
|
tabsContainerRef.current.scrollIntoView({ behavior: 'smooth' })
|
|
|
|
} else {
|
|
|
|
console.error('no ref to scroll to')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const onCancelAnswerResponse = useEvent(() => setAnswerResponse(undefined))
|
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
return (
|
2022-10-03 16:11:24 +00:00
|
|
|
<Page
|
|
|
|
rightSidebar={
|
2022-10-05 21:52:16 +00:00
|
|
|
user || user === null ? (
|
|
|
|
<>
|
|
|
|
<ContractPageSidebar contract={contract} />
|
|
|
|
{isCreator && (
|
|
|
|
<Col className={'xl:hidden'}>
|
|
|
|
<RecommendedContractsWidget contract={contract} />
|
|
|
|
</Col>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
)
|
2022-10-03 16:11:24 +00:00
|
|
|
}
|
|
|
|
>
|
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}
|
2022-09-21 07:02:10 +00:00
|
|
|
url={`/${contract.creatorUsername}/${contract.slug}`}
|
2022-02-17 23:00:19 +00:00
|
|
|
ogCardProps={ogCardProps}
|
|
|
|
/>
|
|
|
|
)}
|
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-27 08:09:17 +00:00
|
|
|
<ContractOverview contract={contract} bets={nonChallengeBets} />
|
2022-09-01 21:42:50 +00:00
|
|
|
<ContractDescription className="mb-6 px-2" contract={contract} />
|
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-10-12 18:05:58 +00:00
|
|
|
<AnswersPanel
|
|
|
|
contract={contract}
|
|
|
|
onAnswerCommentClick={onAnswerCommentClick}
|
|
|
|
/>
|
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-10-10 20:32:29 +00:00
|
|
|
<ContractTopTrades
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
comments={comments}
|
|
|
|
/>
|
2022-04-03 21:57:38 +00:00
|
|
|
</div>
|
|
|
|
<Spacer h={12} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-08 21:13:10 +00:00
|
|
|
|
2022-09-27 17:09:54 +00:00
|
|
|
<BetsSummary
|
|
|
|
className="mb-4 px-2"
|
|
|
|
contract={contract}
|
|
|
|
userBets={userBets}
|
|
|
|
/>
|
|
|
|
|
2022-10-12 18:05:58 +00:00
|
|
|
<div ref={tabsContainerRef}>
|
|
|
|
<ContractTabs
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
userBets={userBets}
|
|
|
|
comments={comments}
|
|
|
|
answerResponse={answerResponse}
|
|
|
|
onCancelAnswerResponse={onCancelAnswerResponse}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-12-12 22:14:52 +00:00
|
|
|
</Col>
|
2022-10-03 16:11:24 +00:00
|
|
|
{!isCreator && <RecommendedContractsWidget contract={contract} />}
|
2022-09-30 20:16:27 +00:00
|
|
|
<ScrollToTopButton className="fixed bottom-16 right-2 z-20 lg:bottom-2 xl:hidden" />
|
2021-12-20 04:06:30 +00:00
|
|
|
</Page>
|
2021-12-09 22:05:55 +00:00
|
|
|
)
|
|
|
|
}
|
2022-09-20 21:03:52 +00:00
|
|
|
|
2022-09-21 07:02:10 +00:00
|
|
|
const RecommendedContractsWidget = memo(
|
|
|
|
function RecommendedContractsWidget(props: { contract: Contract }) {
|
|
|
|
const { contract } = props
|
|
|
|
const user = useUser()
|
|
|
|
const [recommendations, setRecommendations] = useState<Contract[]>([])
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) {
|
|
|
|
getRecommendedContracts(contract, user.id, 6).then(setRecommendations)
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [contract.id, user?.id])
|
|
|
|
if (recommendations.length === 0) {
|
|
|
|
return null
|
2022-09-20 21:03:52 +00:00
|
|
|
}
|
2022-09-21 07:02:10 +00:00
|
|
|
return (
|
2022-10-03 16:11:24 +00:00
|
|
|
<Col className="mt-2 gap-2 px-2 sm:px-1">
|
2022-09-21 07:02:10 +00:00
|
|
|
<Title className="text-gray-700" text="Recommended" />
|
|
|
|
<ContractsGrid
|
|
|
|
contracts={recommendations}
|
|
|
|
trackingPostfix=" recommended"
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
2022-09-20 21:03:52 +00:00
|
|
|
}
|
2022-09-21 07:02:10 +00:00
|
|
|
)
|