2022-01-02 19:09:01 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import Link from 'next/link'
|
2022-04-19 02:44:31 +00:00
|
|
|
import _ from 'lodash'
|
2022-02-21 04:04:00 +00:00
|
|
|
import { ClockIcon, DatabaseIcon, PencilIcon } from '@heroicons/react/outline'
|
|
|
|
import { TrendingUpIcon } from '@heroicons/react/solid'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { Row } from '../layout/row'
|
2022-04-19 02:44:31 +00:00
|
|
|
import { formatMoney, formatPercent } from '../../../common/util/format'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { UserLink } from '../user-page'
|
2022-01-12 19:01:04 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
contractMetrics,
|
|
|
|
contractPath,
|
2022-02-17 23:00:19 +00:00
|
|
|
getBinaryProbPercent,
|
2022-02-21 04:04:00 +00:00
|
|
|
updateContract,
|
2022-04-07 21:15:51 +00:00
|
|
|
} from '../../lib/firebase/contracts'
|
|
|
|
import { Col } from '../layout/col'
|
2022-01-05 17:49:20 +00:00
|
|
|
import dayjs from 'dayjs'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { DateTimeTooltip } from '../datetime-tooltip'
|
|
|
|
import { fromNow } from '../../lib/util/time'
|
|
|
|
import { Avatar } from '../avatar'
|
|
|
|
import { Spacer } from '../layout/spacer'
|
2022-02-21 04:04:00 +00:00
|
|
|
import { useState } from 'react'
|
2022-04-07 20:52:54 +00:00
|
|
|
import { ContractInfoDialog } from './contract-info-dialog'
|
2022-04-13 19:11:49 +00:00
|
|
|
import { Bet } from '../../../common/bet'
|
2022-04-18 23:02:40 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FreeResponseContract,
|
|
|
|
FullContract,
|
|
|
|
} from '../../../common/contract'
|
|
|
|
import {
|
2022-04-19 02:44:31 +00:00
|
|
|
AnswerLabel,
|
2022-04-18 23:02:40 +00:00
|
|
|
BinaryContractOutcomeLabel,
|
|
|
|
FreeResponseOutcomeLabel,
|
|
|
|
} from '../outcome-label'
|
2022-04-19 02:44:31 +00:00
|
|
|
import { getOutcomeProbability } from '../../../common/calculate'
|
2022-01-02 19:09:01 +00:00
|
|
|
|
2022-01-09 21:21:30 +00:00
|
|
|
export function ContractCard(props: {
|
|
|
|
contract: Contract
|
|
|
|
showHotVolume?: boolean
|
2022-01-17 22:54:00 +00:00
|
|
|
showCloseTime?: boolean
|
2022-01-14 06:55:35 +00:00
|
|
|
className?: string
|
2022-01-09 21:21:30 +00:00
|
|
|
}) {
|
2022-01-17 22:54:00 +00:00
|
|
|
const { contract, showHotVolume, showCloseTime, className } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
const { question, outcomeType, resolution } = contract
|
2022-01-02 19:09:01 +00:00
|
|
|
|
|
|
|
return (
|
2022-01-18 22:55:39 +00:00
|
|
|
<div>
|
|
|
|
<div
|
|
|
|
className={clsx(
|
2022-02-11 18:40:22 +00:00
|
|
|
'relative rounded-lg bg-white p-6 shadow-md hover:bg-gray-100',
|
2022-01-18 22:55:39 +00:00
|
|
|
className
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Link href={contractPath(contract)}>
|
|
|
|
<a className="absolute left-0 right-0 top-0 bottom-0" />
|
|
|
|
</Link>
|
2022-02-04 18:30:56 +00:00
|
|
|
|
|
|
|
<AbbrContractDetails
|
|
|
|
contract={contract}
|
|
|
|
showHotVolume={showHotVolume}
|
|
|
|
showCloseTime={showCloseTime}
|
|
|
|
/>
|
|
|
|
<Spacer h={3} />
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
<Row
|
|
|
|
className={clsx(
|
|
|
|
'justify-between gap-4',
|
|
|
|
outcomeType === 'FREE_RESPONSE' && 'flex-col items-start'
|
|
|
|
)}
|
|
|
|
>
|
2022-02-15 02:27:43 +00:00
|
|
|
<p
|
2022-02-18 02:24:10 +00:00
|
|
|
className="break-words font-medium text-indigo-700"
|
2022-02-15 02:27:43 +00:00
|
|
|
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
|
|
|
|
>
|
|
|
|
{question}
|
|
|
|
</p>
|
2022-04-18 23:02:40 +00:00
|
|
|
{outcomeType === 'BINARY' && (
|
|
|
|
<BinaryResolutionOrChance
|
|
|
|
className="items-center"
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
)}
|
2022-04-19 02:44:31 +00:00
|
|
|
{outcomeType === 'FREE_RESPONSE' && (
|
|
|
|
<FreeResponseResolutionOrChance
|
2022-04-18 23:02:40 +00:00
|
|
|
contract={contract as FullContract<DPM, FreeResponse>}
|
|
|
|
truncate="long"
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-18 22:55:39 +00:00
|
|
|
</Row>
|
|
|
|
</div>
|
2022-01-14 22:59:14 +00:00
|
|
|
</div>
|
2022-01-02 19:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
export function BinaryResolutionOrChance(props: {
|
|
|
|
contract: FullContract<DPM | CPMM, Binary>
|
2022-01-02 20:53:42 +00:00
|
|
|
large?: boolean
|
|
|
|
className?: string
|
|
|
|
}) {
|
2022-02-14 21:33:47 +00:00
|
|
|
const { contract, large, className } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
const { resolution } = contract
|
2022-01-02 20:53:42 +00:00
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const marketClosed = (contract.closeTime || Infinity) < Date.now()
|
2022-02-14 21:33:47 +00:00
|
|
|
const probColor = marketClosed ? 'text-gray-400' : 'text-primary'
|
|
|
|
|
2022-01-02 20:53:42 +00:00
|
|
|
return (
|
|
|
|
<Col className={clsx(large ? 'text-4xl' : 'text-3xl', className)}>
|
|
|
|
{resolution ? (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={clsx('text-gray-500', large ? 'text-xl' : 'text-base')}
|
|
|
|
>
|
|
|
|
Resolved
|
|
|
|
</div>
|
2022-04-18 23:02:40 +00:00
|
|
|
<BinaryContractOutcomeLabel
|
|
|
|
contract={contract}
|
|
|
|
resolution={resolution}
|
|
|
|
/>
|
2022-01-02 20:53:42 +00:00
|
|
|
</>
|
|
|
|
) : (
|
2022-04-18 23:02:40 +00:00
|
|
|
<>
|
|
|
|
<div className={probColor}>{getBinaryProbPercent(contract)}</div>
|
|
|
|
<div className={clsx(probColor, large ? 'text-xl' : 'text-base')}>
|
|
|
|
chance
|
|
|
|
</div>
|
|
|
|
</>
|
2022-01-02 20:53:42 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-19 02:44:31 +00:00
|
|
|
function getTopAnswer(contract: FreeResponseContract) {
|
|
|
|
const { answers } = contract
|
|
|
|
const top = _.maxBy(
|
|
|
|
answers.map((answer) => ({
|
|
|
|
answer,
|
|
|
|
prob: getOutcomeProbability(contract, answer.id),
|
|
|
|
})),
|
|
|
|
({ prob }) => prob
|
|
|
|
)
|
|
|
|
return top?.answer
|
|
|
|
}
|
|
|
|
|
|
|
|
export function FreeResponseResolutionOrChance(props: {
|
2022-04-18 23:02:40 +00:00
|
|
|
contract: FreeResponseContract
|
|
|
|
truncate: 'short' | 'long' | 'none'
|
|
|
|
}) {
|
2022-04-19 02:44:31 +00:00
|
|
|
const { contract, truncate } = props
|
|
|
|
const { resolution } = contract
|
|
|
|
|
|
|
|
const topAnswer = getTopAnswer(contract)
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
return (
|
|
|
|
<Col className="text-xl">
|
2022-04-19 02:44:31 +00:00
|
|
|
{resolution ? (
|
|
|
|
<>
|
|
|
|
<div className={clsx('text-base text-gray-500')}>Resolved</div>
|
|
|
|
<FreeResponseOutcomeLabel
|
|
|
|
contract={contract}
|
|
|
|
resolution={resolution}
|
|
|
|
truncate={truncate}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
topAnswer && (
|
|
|
|
<Row className="flex-1 items-center justify-between gap-6">
|
|
|
|
<AnswerLabel answer={topAnswer} truncate={truncate} />
|
|
|
|
<Col className="text-primary">
|
|
|
|
<div>
|
|
|
|
{formatPercent(getOutcomeProbability(contract, topAnswer.id))}
|
|
|
|
</div>
|
|
|
|
<div className="text-base">chance</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
)}
|
2022-04-18 23:02:40 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-04 18:30:56 +00:00
|
|
|
function AbbrContractDetails(props: {
|
2022-01-09 21:21:30 +00:00
|
|
|
contract: Contract
|
|
|
|
showHotVolume?: boolean
|
2022-01-17 22:54:00 +00:00
|
|
|
showCloseTime?: boolean
|
2022-01-09 21:21:30 +00:00
|
|
|
}) {
|
2022-01-17 22:54:00 +00:00
|
|
|
const { contract, showHotVolume, showCloseTime } = props
|
|
|
|
const { volume24Hours, creatorName, creatorUsername, closeTime } = contract
|
2022-03-23 05:02:47 +00:00
|
|
|
const { volumeLabel } = contractMetrics(contract)
|
2022-01-05 07:06:30 +00:00
|
|
|
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className={clsx('gap-2 text-sm text-gray-500')}>
|
|
|
|
<Row className="items-center justify-between">
|
|
|
|
<Row className="items-center gap-2">
|
2022-02-04 18:30:56 +00:00
|
|
|
<Avatar
|
|
|
|
username={creatorUsername}
|
|
|
|
avatarUrl={contract.creatorAvatarUrl}
|
|
|
|
size={6}
|
|
|
|
/>
|
|
|
|
<UserLink
|
|
|
|
className="whitespace-nowrap"
|
|
|
|
name={creatorName}
|
|
|
|
username={creatorUsername}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
|
2022-01-09 21:21:30 +00:00
|
|
|
{showHotVolume ? (
|
2022-02-18 02:24:10 +00:00
|
|
|
<Row className="gap-1">
|
|
|
|
<TrendingUpIcon className="h-5 w-5" /> {formatMoney(volume24Hours)}
|
|
|
|
</Row>
|
2022-01-17 22:54:00 +00:00
|
|
|
) : showCloseTime ? (
|
2022-02-18 02:24:10 +00:00
|
|
|
<Row className="gap-1">
|
|
|
|
<ClockIcon className="h-5 w-5" />
|
2022-03-02 02:00:14 +00:00
|
|
|
{(closeTime || 0) < Date.now() ? 'Closed' : 'Closes'}{' '}
|
|
|
|
{fromNow(closeTime || 0)}
|
2022-02-18 02:24:10 +00:00
|
|
|
</Row>
|
2022-01-09 21:21:30 +00:00
|
|
|
) : (
|
2022-02-18 02:24:10 +00:00
|
|
|
<Row className="gap-1">
|
|
|
|
{/* <DatabaseIcon className="h-5 w-5" /> */}
|
2022-03-23 05:02:47 +00:00
|
|
|
{volumeLabel}
|
2022-02-18 02:24:10 +00:00
|
|
|
</Row>
|
2022-01-09 21:21:30 +00:00
|
|
|
)}
|
2022-01-05 07:06:30 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-21 04:04:00 +00:00
|
|
|
export function ContractDetails(props: {
|
|
|
|
contract: Contract
|
2022-04-13 19:11:49 +00:00
|
|
|
bets: Bet[]
|
2022-02-21 04:04:00 +00:00
|
|
|
isCreator?: boolean
|
2022-03-21 20:23:21 +00:00
|
|
|
hideShareButtons?: boolean
|
2022-02-21 04:04:00 +00:00
|
|
|
}) {
|
2022-04-13 19:11:49 +00:00
|
|
|
const { contract, bets, isCreator, hideShareButtons } = props
|
2022-01-31 03:25:50 +00:00
|
|
|
const { closeTime, creatorName, creatorUsername } = contract
|
2022-03-23 05:02:47 +00:00
|
|
|
const { volumeLabel, createdDate, resolvedDate } = contractMetrics(contract)
|
2022-01-02 19:09:01 +00:00
|
|
|
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="gap-2 text-sm text-gray-500 sm:flex-row sm:flex-wrap">
|
2022-04-07 20:52:54 +00:00
|
|
|
<Row className="flex-1 flex-wrap items-center gap-x-4 gap-y-3">
|
2022-02-18 02:24:10 +00:00
|
|
|
<Row className="items-center gap-2">
|
|
|
|
<Avatar
|
|
|
|
username={creatorUsername}
|
|
|
|
avatarUrl={contract.creatorAvatarUrl}
|
|
|
|
size={6}
|
|
|
|
/>
|
|
|
|
<UserLink
|
|
|
|
className="whitespace-nowrap"
|
|
|
|
name={creatorName}
|
|
|
|
username={creatorUsername}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
|
2022-04-08 19:27:17 +00:00
|
|
|
{(!!closeTime || !!resolvedDate) && (
|
|
|
|
<Row className="items-center gap-1">
|
|
|
|
<ClockIcon className="h-5 w-5" />
|
2022-01-19 22:01:54 +00:00
|
|
|
|
2022-04-08 19:27:17 +00:00
|
|
|
{/* <DateTimeTooltip text="Market created:" time={contract.createdTime}>
|
2022-01-16 09:16:15 +00:00
|
|
|
{createdDate}
|
2022-04-08 19:27:17 +00:00
|
|
|
</DateTimeTooltip> */}
|
2022-01-19 22:01:54 +00:00
|
|
|
|
2022-04-08 19:27:17 +00:00
|
|
|
{resolvedDate && contract.resolutionTime ? (
|
|
|
|
<>
|
|
|
|
{/* {' - '} */}
|
|
|
|
<DateTimeTooltip
|
|
|
|
text="Market resolved:"
|
|
|
|
time={contract.resolutionTime}
|
|
|
|
>
|
|
|
|
{resolvedDate}
|
|
|
|
</DateTimeTooltip>
|
|
|
|
</>
|
|
|
|
) : null}
|
2022-01-19 22:01:54 +00:00
|
|
|
|
2022-04-08 19:27:17 +00:00
|
|
|
{!resolvedDate && closeTime && (
|
|
|
|
<>
|
|
|
|
{/* {' - '}{' '} */}
|
|
|
|
<EditableCloseDate
|
|
|
|
closeTime={closeTime}
|
|
|
|
contract={contract}
|
|
|
|
isCreator={isCreator ?? false}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
)}
|
2022-02-18 02:24:10 +00:00
|
|
|
|
|
|
|
<Row className="items-center gap-1">
|
|
|
|
<DatabaseIcon className="h-5 w-5" />
|
2022-01-19 22:01:54 +00:00
|
|
|
|
2022-03-23 05:02:47 +00:00
|
|
|
<div className="whitespace-nowrap">{volumeLabel}</div>
|
2022-02-18 02:24:10 +00:00
|
|
|
</Row>
|
2022-02-23 03:22:24 +00:00
|
|
|
|
2022-04-13 19:11:49 +00:00
|
|
|
{!hideShareButtons && (
|
|
|
|
<ContractInfoDialog contract={contract} bets={bets} />
|
|
|
|
)}
|
2022-01-04 22:09:03 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
2022-01-02 19:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-10 07:05:24 +00:00
|
|
|
|
|
|
|
// String version of the above, to send to the OpenGraph image generator
|
|
|
|
export function contractTextDetails(contract: Contract) {
|
2022-01-31 03:25:50 +00:00
|
|
|
const { closeTime, tags } = contract
|
2022-03-23 05:02:47 +00:00
|
|
|
const { createdDate, resolvedDate, volumeLabel } = contractMetrics(contract)
|
2022-01-10 07:05:24 +00:00
|
|
|
|
2022-01-31 03:25:50 +00:00
|
|
|
const hashtags = tags.map((tag) => `#${tag}`)
|
2022-01-10 07:05:24 +00:00
|
|
|
|
|
|
|
return (
|
2022-01-10 07:09:39 +00:00
|
|
|
`${resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}` +
|
2022-01-10 07:05:24 +00:00
|
|
|
(closeTime
|
|
|
|
? ` • ${closeTime > Date.now() ? 'Closes' : 'Closed'} ${dayjs(
|
|
|
|
closeTime
|
|
|
|
).format('MMM D, h:mma')}`
|
|
|
|
: '') +
|
2022-03-23 05:02:47 +00:00
|
|
|
` • ${volumeLabel}` +
|
2022-01-31 03:25:50 +00:00
|
|
|
(hashtags.length > 0 ? ` • ${hashtags.join(' ')}` : '')
|
2022-01-10 07:05:24 +00:00
|
|
|
)
|
|
|
|
}
|
2022-02-21 04:04:00 +00:00
|
|
|
|
|
|
|
function EditableCloseDate(props: {
|
|
|
|
closeTime: number
|
|
|
|
contract: Contract
|
|
|
|
isCreator: boolean
|
|
|
|
}) {
|
|
|
|
const { closeTime, contract, isCreator } = props
|
|
|
|
|
|
|
|
const [isEditingCloseTime, setIsEditingCloseTime] = useState(false)
|
|
|
|
const [closeDate, setCloseDate] = useState(
|
|
|
|
closeTime && dayjs(closeTime).format('YYYY-MM-DDT23:59')
|
|
|
|
)
|
|
|
|
|
2022-04-19 17:55:22 +00:00
|
|
|
const isSameYear = dayjs(closeTime).isSame(dayjs(), 'year')
|
|
|
|
const isSameDay = dayjs(closeTime).isSame(dayjs(), 'day')
|
|
|
|
|
2022-02-21 04:04:00 +00:00
|
|
|
const onSave = () => {
|
|
|
|
const newCloseTime = dayjs(closeDate).valueOf()
|
|
|
|
if (newCloseTime === closeTime) setIsEditingCloseTime(false)
|
|
|
|
else if (newCloseTime > Date.now()) {
|
|
|
|
const { description } = contract
|
|
|
|
const formattedCloseDate = dayjs(newCloseTime).format('YYYY-MM-DD h:mm a')
|
|
|
|
const newDescription = `${description}\n\nClose date updated to ${formattedCloseDate}`
|
|
|
|
|
|
|
|
updateContract(contract.id, {
|
|
|
|
closeTime: newCloseTime,
|
|
|
|
description: newDescription,
|
|
|
|
})
|
|
|
|
|
|
|
|
setIsEditingCloseTime(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{isEditingCloseTime ? (
|
|
|
|
<div className="form-control mr-1 items-start">
|
|
|
|
<input
|
|
|
|
type="datetime-local"
|
|
|
|
className="input input-bordered"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setCloseDate(e.target.value || '')}
|
|
|
|
min={Date.now()}
|
|
|
|
value={closeDate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<DateTimeTooltip
|
|
|
|
text={closeTime > Date.now() ? 'Trading ends:' : 'Trading ended:'}
|
|
|
|
time={closeTime}
|
|
|
|
>
|
2022-04-19 17:55:22 +00:00
|
|
|
{isSameYear
|
|
|
|
? dayjs(closeTime).format('MMM D')
|
|
|
|
: dayjs(closeTime).format('MMM D, YYYY')}
|
|
|
|
{isSameDay && <> ({fromNow(closeTime)})</>}
|
2022-02-21 04:04:00 +00:00
|
|
|
</DateTimeTooltip>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isCreator &&
|
|
|
|
(isEditingCloseTime ? (
|
|
|
|
<button className="btn btn-xs" onClick={onSave}>
|
|
|
|
Done
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
className="btn btn-xs btn-ghost"
|
|
|
|
onClick={() => setIsEditingCloseTime(true)}
|
|
|
|
>
|
2022-03-02 02:00:14 +00:00
|
|
|
<PencilIcon className="mr-2 inline h-4 w-4" /> Edit
|
2022-02-21 04:04:00 +00:00
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|