import clsx from 'clsx' import React, { useState } from 'react' import { findBestMatch } from 'string-similarity' import { FreeResponseContract } from 'common/contract' import { BuyAmountInput } from '../amount-input' import { Col } from '../layout/col' import { APIError, createAnswer } from 'web/lib/firebase/api' import { Row } from '../layout/row' import { formatMoney, formatPercent, formatWithCommas, } from 'common/util/format' import { InfoTooltip } from '../info-tooltip' import { useUser } from 'web/hooks/use-user' import { calculateDpmShares, calculateDpmPayoutAfterCorrectBet, getDpmOutcomeProbabilityAfterBet, } from 'common/calculate-dpm' import { firebaseLogin } from 'web/lib/firebase/users' import { Bet } from 'common/bet' import { MAX_ANSWER_LENGTH } from 'common/answer' import { withTracking } from 'web/lib/service/analytics' import { lowerCase } from 'lodash' import { Button } from '../button' import { ExpandingInput } from '../expanding-input' export function CreateAnswerPanel(props: { contract: FreeResponseContract }) { const { contract } = props const user = useUser() const [text, setText] = useState('') const [betAmount, setBetAmount] = useState(10) const [amountError, setAmountError] = useState() const [answerError, setAnswerError] = useState() const [possibleDuplicateAnswer, setPossibleDuplicateAnswer] = useState< string | undefined >() const [isSubmitting, setIsSubmitting] = useState(false) const { answers } = contract const canSubmit = text && betAmount && !amountError && !isSubmitting && !answerError const submitAnswer = async () => { if (canSubmit) { setIsSubmitting(true) try { await createAnswer({ contractId: contract.id, text, amount: betAmount, }) setText('') setBetAmount(10) setAmountError(undefined) setPossibleDuplicateAnswer(undefined) } catch (e) { if (e instanceof APIError) { setAmountError(e.toString()) } } setIsSubmitting(false) } } const changeAnswer = (text: string) => { setText(text) const existingAnswer = answers.find( (a) => lowerCase(a.text) === lowerCase(text) ) if (existingAnswer) { setAnswerError( existingAnswer ? `"${existingAnswer.text}" already exists as an answer. Can't see it? Hit the 'Show More' button right above this box.` : '' ) return } else { setAnswerError('') } if (answers.length && text) { const matches = findBestMatch( lowerCase(text), answers.map((a) => lowerCase(a.text)) ) setPossibleDuplicateAnswer( matches.bestMatch.rating > 0.8 ? answers[matches.bestMatchIndex].text : '' ) } } const resultProb = getDpmOutcomeProbabilityAfterBet( contract.totalShares, 'new', betAmount ?? 0 ) const shares = calculateDpmShares(contract.totalShares, betAmount ?? 0, 'new') const currentPayout = betAmount ? calculateDpmPayoutAfterCorrectBet(contract, { outcome: 'new', amount: betAmount, shares, } as Bet) : 0 const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0 const currentReturnPercent = (currentReturn * 100).toFixed() + '%' if (user?.isBannedFromPosting) return <> return (
Add your answer
changeAnswer(e.target.value)} className="w-full" placeholder="Type your answer..." rows={1} maxLength={MAX_ANSWER_LENGTH} /> {answerError ? ( ) : possibleDuplicateAnswer ? ( ) : undefined}
{text && ( <> Bet Amount Balance: {formatMoney(user?.balance ?? 0)} {' '}
Probability
{formatPercent(0)}
{formatPercent(resultProb)}
Estimated
payout if chosen
{formatMoney(currentPayout)} (+{currentReturnPercent})
)} {user ? ( ) : ( text && ( ) )} ) } type answerErrorLevel = 'warning' | 'error' const AnswerError = (props: { text: string; level: answerErrorLevel }) => { const { text, level } = props const colorClass = { error: 'text-red-500', warning: 'text-orange-500', }[level] ?? '' return (
{text}
) }