2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-08-19 17:07:48 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { DOMAIN } from 'common/envs/constants'
|
2022-10-02 18:39:19 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-08-19 17:07:48 +00:00
|
|
|
import { BetInline } from 'web/components/bet-inline'
|
|
|
|
import { Button } from 'web/components/button'
|
2022-03-20 21:05:16 +00:00
|
|
|
import {
|
2022-04-18 23:02:40 +00:00
|
|
|
BinaryResolutionOrChance,
|
2022-10-02 18:55:47 +00:00
|
|
|
ContractCard,
|
2022-04-19 02:44:31 +00:00
|
|
|
FreeResponseResolutionOrChance,
|
2022-05-19 17:42:03 +00:00
|
|
|
NumericResolutionOrExpectation,
|
2022-07-02 19:37:59 +00:00
|
|
|
PseudoNumericResolutionOrExpectation,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/components/contract/contract-card'
|
2022-09-16 00:41:25 +00:00
|
|
|
import { MarketSubheader } from 'web/components/contract/contract-details'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { ContractChart } from 'web/components/charts/contract'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import { useContractWithPreload } from 'web/hooks/use-contract'
|
|
|
|
import { useMeasureSize } from 'web/hooks/use-measure-size'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
|
|
|
import { listAllBets } from 'web/lib/firebase/bets'
|
2022-08-04 21:27:02 +00:00
|
|
|
import {
|
|
|
|
contractPath,
|
|
|
|
getContractFromSlug,
|
|
|
|
tradingAllowed,
|
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-03-20 17:45:17 +00:00
|
|
|
import Custom404 from '../../404'
|
2022-10-02 18:39:19 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-03-20 17:45:17 +00:00
|
|
|
|
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz(props: {
|
|
|
|
params: { username: string; contractSlug: string }
|
|
|
|
}) {
|
2022-09-21 07:02:10 +00:00
|
|
|
const { contractSlug } = props.params
|
2022-03-20 17:45:17 +00:00
|
|
|
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 {
|
2022-09-21 07:02:10 +00:00
|
|
|
props: { contract, bets },
|
2022-03-20 17:45:17 +00:00
|
|
|
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
|
|
|
|
bets: Bet[]
|
|
|
|
}) {
|
2022-09-21 07:02:10 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? { contract: null, bets: [] }
|
2022-03-20 17:45:17 +00:00
|
|
|
|
2022-04-06 17:32:57 +00:00
|
|
|
const contract = useContractWithPreload(props.contract)
|
2022-03-20 21:05:16 +00:00
|
|
|
const { bets } = props
|
2022-03-20 17:45:17 +00:00
|
|
|
|
|
|
|
if (!contract) {
|
|
|
|
return <Custom404 />
|
|
|
|
}
|
|
|
|
|
2022-03-20 21:05:16 +00:00
|
|
|
return <ContractEmbed contract={contract} bets={bets} />
|
|
|
|
}
|
|
|
|
|
2022-10-02 18:55:47 +00:00
|
|
|
interface EmbedProps {
|
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
|
|
|
}
|
2022-03-20 21:05:16 +00:00
|
|
|
|
2022-10-02 18:55:47 +00:00
|
|
|
export function ContractEmbed(props: EmbedProps) {
|
|
|
|
const { contract } = props
|
2022-10-02 18:39:19 +00:00
|
|
|
useEffect(() => {
|
|
|
|
track('view market embed', {
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
|
|
|
creatorId: contract.creatorId,
|
|
|
|
hostname: window.location.hostname,
|
|
|
|
})
|
|
|
|
}, [contract.creatorId, contract.id, contract.slug])
|
2022-09-07 03:43:28 +00:00
|
|
|
|
2022-10-02 18:55:47 +00:00
|
|
|
// return (height < 250px) ? Card : SmolView
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="contents [@media(min-height:250px)]:hidden">
|
|
|
|
<ContractCard
|
|
|
|
contract={contract}
|
|
|
|
className="h-screen"
|
|
|
|
noLinkAvatar
|
|
|
|
newTab
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="hidden [@media(min-height:250px)]:contents">
|
|
|
|
<ContractSmolView {...props} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function ContractSmolView({ contract, bets }: EmbedProps) {
|
|
|
|
const { question, outcomeType } = contract
|
|
|
|
|
2022-03-20 21:05:16 +00:00
|
|
|
const isBinary = outcomeType === 'BINARY'
|
2022-07-02 19:37:59 +00:00
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
2022-03-20 21:05:16 +00:00
|
|
|
|
2022-03-20 23:12:33 +00:00
|
|
|
const href = `https://${DOMAIN}${contractPath(contract)}`
|
|
|
|
|
2022-09-30 23:57:48 +00:00
|
|
|
const { setElem, width: graphWidth, height: graphHeight } = useMeasureSize()
|
2022-03-24 17:03:08 +00:00
|
|
|
|
2022-08-19 17:07:48 +00:00
|
|
|
const [betPanelOpen, setBetPanelOpen] = useState(false)
|
|
|
|
|
|
|
|
const [probAfter, setProbAfter] = useState<number>()
|
2022-03-24 17:03:08 +00:00
|
|
|
|
2022-03-20 17:45:17 +00:00
|
|
|
return (
|
2022-10-02 22:04:20 +00:00
|
|
|
<Col className="h-[100vh] w-full bg-white p-4">
|
2022-09-16 00:41:25 +00:00
|
|
|
<Row className="justify-between gap-4 px-2">
|
|
|
|
<div className="text-xl text-indigo-700 md:text-2xl">
|
2022-05-17 17:31:19 +00:00
|
|
|
<SiteLink href={href}>{question}</SiteLink>
|
2022-03-20 23:17:37 +00:00
|
|
|
</div>
|
2022-09-16 00:41:25 +00:00
|
|
|
{isBinary && (
|
|
|
|
<BinaryResolutionOrChance contract={contract} probAfter={probAfter} />
|
|
|
|
)}
|
2022-03-20 21:05:16 +00:00
|
|
|
|
2022-09-16 00:41:25 +00:00
|
|
|
{isPseudoNumeric && (
|
|
|
|
<PseudoNumericResolutionOrExpectation contract={contract} />
|
|
|
|
)}
|
2022-07-02 19:37:59 +00:00
|
|
|
|
2022-09-16 00:41:25 +00:00
|
|
|
{outcomeType === 'FREE_RESPONSE' && (
|
|
|
|
<FreeResponseResolutionOrChance contract={contract} truncate="long" />
|
|
|
|
)}
|
2022-05-19 17:42:03 +00:00
|
|
|
|
2022-09-16 00:41:25 +00:00
|
|
|
{outcomeType === 'NUMERIC' && (
|
|
|
|
<NumericResolutionOrExpectation contract={contract} />
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
<Spacer h={3} />
|
|
|
|
<Row className="items-center justify-between gap-4 px-2">
|
|
|
|
<MarketSubheader contract={contract} disabled />
|
|
|
|
|
|
|
|
{(isBinary || isPseudoNumeric) &&
|
|
|
|
tradingAllowed(contract) &&
|
|
|
|
!betPanelOpen && (
|
|
|
|
<Button color="gradient" onClick={() => setBetPanelOpen(true)}>
|
|
|
|
Predict
|
|
|
|
</Button>
|
2022-05-19 17:42:03 +00:00
|
|
|
)}
|
2022-09-16 00:41:25 +00:00
|
|
|
</Row>
|
2022-03-20 21:05:16 +00:00
|
|
|
|
2022-09-16 00:41:25 +00:00
|
|
|
<Spacer h={2} />
|
2022-03-20 21:05:16 +00:00
|
|
|
|
2022-08-19 17:07:48 +00:00
|
|
|
{(isBinary || isPseudoNumeric) && betPanelOpen && (
|
|
|
|
<BetInline
|
|
|
|
contract={contract as any}
|
|
|
|
setProbAfter={setProbAfter}
|
|
|
|
onClose={() => setBetPanelOpen(false)}
|
|
|
|
className="self-center"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="mx-1 mb-2 min-h-0 flex-1" ref={setElem}>
|
2022-09-30 23:57:48 +00:00
|
|
|
{graphWidth != null && graphHeight != null && (
|
|
|
|
<ContractChart
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
width={graphWidth}
|
|
|
|
height={graphHeight}
|
|
|
|
/>
|
|
|
|
)}
|
2022-03-20 21:05:16 +00:00
|
|
|
</div>
|
2022-03-20 17:45:17 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|