import clsx from 'clsx' import _ from 'lodash' import { useState } from 'react' import { Answer } from '../../../common/answer' import { DPM, FreeResponse, FullContract } from '../../../common/contract' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Avatar } from '../avatar' import { SiteLink } from '../site-link' import { BuyButton } from '../yes-no-selector' import { formatPercent } from '../../../common/util/format' import { getDpmOutcomeProbability } from '../../../common/calculate-dpm' import { tradingAllowed } from '../../lib/firebase/contracts' import { AnswerBetPanel } from './answer-bet-panel' import { Linkify } from '../linkify' import { User } from '../../../common/user' import { ContractActivity } from '../feed/contract-activity' export function AnswerItem(props: { answer: Answer contract: FullContract user: User | null | undefined showChoice: 'radio' | 'checkbox' | undefined chosenProb: number | undefined totalChosenProb?: number onChoose: (answerId: string, prob: number) => void onDeselect: (answerId: string) => void }) { const { answer, contract, user, showChoice, chosenProb, totalChosenProb, onChoose, onDeselect, } = props const { resolution, resolutions, totalShares } = contract const { username, avatarUrl, name, number, text } = answer const isChosen = chosenProb !== undefined const prob = getDpmOutcomeProbability(totalShares, answer.id) const roundedProb = Math.round(prob * 100) const probPercent = formatPercent(prob) const wasResolvedTo = resolution === answer.id || (resolutions && resolutions[answer.id]) const [isBetting, setIsBetting] = useState(false) const canBet = !isBetting && !showChoice && tradingAllowed(contract) return (
canBet && setIsBetting(true)} >
{name}
{/* TODO: Show total pool? */}
#{number}
{isBetting && ( )} {isBetting ? ( setIsBetting(false)} className="sm:w-72" /> ) : ( {!wasResolvedTo && (showChoice === 'checkbox' ? ( { const { value } = e.target const numberValue = value ? parseInt(value.replace(/[^\d]/, '')) : 0 if (!isNaN(numberValue)) onChoose(answer.id, numberValue) }} /> ) : (
{probPercent}
))} {showChoice ? (
{showChoice === 'checkbox' && (
{chosenProb && totalChosenProb ? Math.round((100 * chosenProb) / totalChosenProb) : 0} % share
)}
) : ( <> {tradingAllowed(contract) && ( { setIsBetting(true) }} /> )} {wasResolvedTo && (
Chosen{' '} {resolutions ? `${Math.round(resolutions[answer.id])}%` : ''}
{probPercent}
)} )}
)}
) }