import React, { memo, useEffect } from 'react' import dayjs from 'dayjs' import { Contract } from 'common/contract' import { Bet } from 'common/bet' import { useUser } from 'web/hooks/use-user' import { Row } from 'web/components/layout/row' import { Avatar, EmptyAvatar } from 'web/components/avatar' import clsx from 'clsx' import { formatMoney, formatPercent } from 'common/util/format' import { OutcomeLabel } from 'web/components/outcome-label' import { RelativeTimestamp } from 'web/components/relative-timestamp' import { formatNumericProbability } from 'common/pseudo-numeric' import { SiteLink } from 'web/components/site-link' import { getChallenge, getChallengeUrl } from 'web/lib/firebase/challenges' import { Challenge } from 'common/challenge' import { UserLink } from 'web/components/user-link' import { BETTOR } from 'common/user' export const FeedBet = memo(function FeedBet(props: { contract: Contract bet: Bet }) { const { contract, bet } = props const { userAvatarUrl, userUsername, createdTime } = bet const showUser = dayjs(createdTime).isAfter('2022-06-01') return ( {showUser ? ( ) : ( )} ) }) export function BetStatusText(props: { contract: Contract bet: Bet hideUser?: boolean hideOutcome?: boolean className?: string }) { const { bet, contract, hideUser, hideOutcome, className } = props const { outcomeType } = contract const self = useUser() const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC' const isFreeResponse = outcomeType === 'FREE_RESPONSE' const { amount, outcome, createdTime, challengeSlug } = bet const [challenge, setChallenge] = React.useState() useEffect(() => { if (challengeSlug) { getChallenge(challengeSlug, contract.id).then((c) => { setChallenge(c) }) } }, [challengeSlug, contract.id]) const bought = amount >= 0 ? 'bought' : 'sold' const outOfTotalAmount = bet.limitProb !== undefined && bet.orderAmount !== undefined ? ` / ${formatMoney(bet.orderAmount)}` : '' const money = formatMoney(Math.abs(amount)) const hadPoolMatch = (bet.limitProb === undefined || bet.fills?.some((fill) => fill.matchedBetId === null)) ?? false 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) return (
{!hideUser ? ( ) : ( {self?.id === bet.userId ? 'You' : `A ${BETTOR}`} )}{' '} {bought} {money} {outOfTotalAmount} {!hideOutcome && ( <> {' '} of{' '} {' '} {fromProb === toProb ? `at ${fromProb}` : `from ${fromProb} to ${toProb}`} {challengeSlug && ( [challenge] )} )}
) }