2022-06-09 04:03:03 +00:00
|
|
|
import dayjs from 'dayjs'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { Contract } from 'common/contract'
|
|
|
|
import { Bet } from 'common/bet'
|
|
|
|
import { User } from 'common/user'
|
2022-06-09 04:03:03 +00:00
|
|
|
import { useUser, useUserById } from 'web/hooks/use-user'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { Row } from 'web/components/layout/row'
|
2022-06-23 17:55:41 +00:00
|
|
|
import { Avatar, EmptyAvatar } from 'web/components/avatar'
|
2022-05-18 14:42:13 +00:00
|
|
|
import clsx from 'clsx'
|
2022-07-02 19:37:59 +00:00
|
|
|
import { formatMoney, formatPercent } from 'common/util/format'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { OutcomeLabel } from 'web/components/outcome-label'
|
|
|
|
import { RelativeTimestamp } from 'web/components/relative-timestamp'
|
2022-08-27 20:46:35 +00:00
|
|
|
import React, { useEffect } from 'react'
|
2022-06-09 20:00:31 +00:00
|
|
|
import { UserLink } from '../user-page'
|
2022-07-02 19:37:59 +00:00
|
|
|
import { formatNumericProbability } from 'common/pseudo-numeric'
|
2022-08-04 21:27:02 +00:00
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import { getChallenge, getChallengeUrl } from 'web/lib/firebase/challenges'
|
|
|
|
import { Challenge } from 'common/challenge'
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
export function FeedBet(props: {
|
|
|
|
contract: Contract
|
|
|
|
bet: Bet
|
|
|
|
hideOutcome: boolean
|
|
|
|
smallAvatar: boolean
|
|
|
|
}) {
|
2022-06-09 04:03:03 +00:00
|
|
|
const { contract, bet, hideOutcome, smallAvatar } = props
|
|
|
|
const { userId, createdTime } = bet
|
|
|
|
|
|
|
|
const isBeforeJune2022 = dayjs(createdTime).isBefore('2022-06-01')
|
|
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
|
|
const bettor = isBeforeJune2022 ? undefined : useUserById(userId)
|
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
const user = useUser()
|
|
|
|
const isSelf = user?.id === userId
|
|
|
|
|
|
|
|
return (
|
2022-08-09 20:25:42 +00:00
|
|
|
<Row className={'flex w-full items-center gap-2 pt-3'}>
|
|
|
|
{isSelf ? (
|
|
|
|
<Avatar
|
|
|
|
className={clsx(smallAvatar && 'ml-1')}
|
|
|
|
size={smallAvatar ? 'sm' : undefined}
|
|
|
|
avatarUrl={user.avatarUrl}
|
|
|
|
username={user.username}
|
|
|
|
/>
|
|
|
|
) : bettor ? (
|
|
|
|
<Avatar
|
|
|
|
className={clsx(smallAvatar && 'ml-1')}
|
|
|
|
size={smallAvatar ? 'sm' : undefined}
|
|
|
|
avatarUrl={bettor.avatarUrl}
|
|
|
|
username={bettor.username}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<EmptyAvatar className="mx-1" />
|
|
|
|
)}
|
|
|
|
<BetStatusText
|
|
|
|
bet={bet}
|
|
|
|
contract={contract}
|
|
|
|
isSelf={isSelf}
|
|
|
|
bettor={bettor}
|
|
|
|
hideOutcome={hideOutcome}
|
|
|
|
className="flex-1"
|
|
|
|
/>
|
|
|
|
</Row>
|
2022-05-18 14:42:13 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BetStatusText(props: {
|
|
|
|
contract: Contract
|
|
|
|
bet: Bet
|
|
|
|
isSelf: boolean
|
|
|
|
bettor?: User
|
|
|
|
hideOutcome?: boolean
|
2022-08-09 20:25:42 +00:00
|
|
|
className?: string
|
2022-05-18 14:42:13 +00:00
|
|
|
}) {
|
2022-08-09 20:25:42 +00:00
|
|
|
const { bet, contract, bettor, isSelf, hideOutcome, className } = props
|
2022-07-02 19:37:59 +00:00
|
|
|
const { outcomeType } = contract
|
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
2022-07-11 16:46:07 +00:00
|
|
|
const isFreeResponse = outcomeType === 'FREE_RESPONSE'
|
2022-08-04 21:27:02 +00:00
|
|
|
const { amount, outcome, createdTime, challengeSlug } = bet
|
|
|
|
const [challenge, setChallenge] = React.useState<Challenge>()
|
|
|
|
useEffect(() => {
|
|
|
|
if (challengeSlug) {
|
|
|
|
getChallenge(challengeSlug, contract.id).then((c) => {
|
|
|
|
setChallenge(c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [challengeSlug, contract.id])
|
2022-05-18 14:42:13 +00:00
|
|
|
|
|
|
|
const bought = amount >= 0 ? 'bought' : 'sold'
|
2022-07-11 16:46:07 +00:00
|
|
|
const outOfTotalAmount =
|
|
|
|
bet.limitProb !== undefined && bet.orderAmount !== undefined
|
|
|
|
? ` / ${formatMoney(bet.orderAmount)}`
|
|
|
|
: ''
|
2022-05-18 14:42:13 +00:00
|
|
|
const money = formatMoney(Math.abs(amount))
|
|
|
|
|
2022-07-11 16:46:07 +00:00
|
|
|
const hadPoolMatch =
|
2022-07-11 20:53:11 +00:00
|
|
|
(bet.limitProb === undefined ||
|
|
|
|
bet.fills?.some((fill) => fill.matchedBetId === null)) ??
|
|
|
|
false
|
2022-07-11 16:46:07 +00:00
|
|
|
|
2022-07-20 22:04:11 +00:00
|
|
|
const fromProb =
|
|
|
|
hadPoolMatch || isFreeResponse
|
|
|
|
? isPseudoNumeric
|
|
|
|
? formatNumericProbability(bet.probBefore, contract)
|
|
|
|
: formatPercent(bet.probBefore)
|
|
|
|
: isPseudoNumeric
|
|
|
|
? formatNumericProbability(bet.limitProb ?? bet.probBefore, contract)
|
|
|
|
: formatPercent(bet.limitProb ?? bet.probBefore)
|
|
|
|
|
|
|
|
const toProb =
|
|
|
|
hadPoolMatch || isFreeResponse
|
|
|
|
? isPseudoNumeric
|
|
|
|
? formatNumericProbability(bet.probAfter, contract)
|
|
|
|
: formatPercent(bet.probAfter)
|
|
|
|
: isPseudoNumeric
|
|
|
|
? formatNumericProbability(bet.limitProb ?? bet.probAfter, contract)
|
|
|
|
: formatPercent(bet.limitProb ?? bet.probAfter)
|
|
|
|
|
2022-05-18 14:42:13 +00:00
|
|
|
return (
|
2022-08-09 20:25:42 +00:00
|
|
|
<div className={clsx('text-sm text-gray-500', className)}>
|
2022-06-09 20:00:31 +00:00
|
|
|
{bettor ? (
|
|
|
|
<UserLink name={bettor.name} username={bettor.username} />
|
|
|
|
) : (
|
|
|
|
<span>{isSelf ? 'You' : 'A trader'}</span>
|
|
|
|
)}{' '}
|
|
|
|
{bought} {money}
|
2022-07-11 16:46:07 +00:00
|
|
|
{outOfTotalAmount}
|
2022-05-18 14:42:13 +00:00
|
|
|
{!hideOutcome && (
|
|
|
|
<>
|
|
|
|
{' '}
|
|
|
|
of{' '}
|
|
|
|
<OutcomeLabel
|
|
|
|
outcome={outcome}
|
2022-05-19 17:42:03 +00:00
|
|
|
value={(bet as any).value}
|
2022-05-18 14:42:13 +00:00
|
|
|
contract={contract}
|
|
|
|
truncate="short"
|
2022-07-02 19:37:59 +00:00
|
|
|
/>{' '}
|
2022-07-20 22:04:11 +00:00
|
|
|
{fromProb === toProb
|
|
|
|
? `at ${fromProb}`
|
|
|
|
: `from ${fromProb} to ${toProb}`}
|
2022-08-04 21:27:02 +00:00
|
|
|
{challengeSlug && (
|
|
|
|
<SiteLink
|
|
|
|
href={challenge ? getChallengeUrl(challenge) : ''}
|
|
|
|
className={'mx-1'}
|
|
|
|
>
|
|
|
|
[challenge]
|
|
|
|
</SiteLink>
|
|
|
|
)}
|
2022-05-18 14:42:13 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<RelativeTimestamp time={createdTime} />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|