import clsx from 'clsx' import dayjs from 'dayjs' import React, { useEffect, useState } from 'react' import { SwitchVerticalIcon } from '@heroicons/react/outline' import { Col } from '../layout/col' import { Row } from '../layout/row' import { Title } from '../title' import { User } from 'common/user' import { Modal } from 'web/components/layout/modal' import { Button } from '../button' import { createChallenge, getChallengeUrl } from 'web/lib/firebase/challenges' import { BinaryContract, MAX_QUESTION_LENGTH } from 'common/contract' import { SiteLink } from 'web/components/site-link' import { formatMoney } from 'common/util/format' import { NoLabel, YesLabel } from '../outcome-label' import { QRCode } from '../qr-code' import { AmountInput } from '../amount-input' import { getProbability } from 'common/calculate' import { createMarket } from 'web/lib/firebase/api' import { removeUndefinedProps } from 'common/util/object' import { FIXED_ANTE } from 'common/economy' import Textarea from 'react-expanding-textarea' import { useTextEditor } from 'web/components/editor' import { LoadingIndicator } from 'web/components/loading-indicator' import { track } from 'web/lib/service/analytics' import { CopyLinkButton } from '../copy-link-button' type challengeInfo = { amount: number expiresTime: number | null outcome: 'YES' | 'NO' | number acceptorAmount: number question: string } export function CreateChallengeModal(props: { user: User | null | undefined isOpen: boolean setOpen: (open: boolean) => void contract?: BinaryContract }) { const { user, contract, isOpen, setOpen } = props const [challengeSlug, setChallengeSlug] = useState('') const [loading, setLoading] = useState(false) const { editor } = useTextEditor({ placeholder: '' }) return ( {/*// add a sign up to challenge button?*/} {user && ( { setLoading(true) try { const challengeContract = contract ? contract : await createMarket( removeUndefinedProps({ question: newChallenge.question, outcomeType: 'BINARY', initialProb: 50, description: editor?.getJSON(), ante: FIXED_ANTE, closeTime: dayjs().add(30, 'day').valueOf(), }) ) const challenge = await createChallenge({ creator: user, creatorAmount: newChallenge.amount, expiresTime: newChallenge.expiresTime, acceptorAmount: newChallenge.acceptorAmount, outcome: newChallenge.outcome, contract: challengeContract as BinaryContract, }) if (challenge) { setChallengeSlug(getChallengeUrl(challenge)) track('challenge created', { creator: user.username, amount: newChallenge.amount, contractId: challengeContract.id, }) } } catch (e) { console.error("couldn't create market/challenge:", e) } setLoading(false) }} challengeSlug={challengeSlug} /> )} ) } function CreateChallengeForm(props: { user: User onCreate: (m: challengeInfo) => Promise challengeSlug: string loading: boolean contract?: BinaryContract }) { const { user, onCreate, contract, challengeSlug, loading } = props const [isCreating, setIsCreating] = useState(false) const [finishedCreating, setFinishedCreating] = useState(false) const [error, setError] = useState('') const [editingAcceptorAmount, setEditingAcceptorAmount] = useState(false) const defaultExpire = 'week' const [challengeInfo, setChallengeInfo] = useState({ expiresTime: dayjs().add(2, defaultExpire).valueOf(), outcome: 'YES', amount: 100, acceptorAmount: 100, question: contract ? contract.question : '', }) useEffect(() => { setError('') }, [challengeInfo]) return ( <> {!finishedCreating && ( { e.preventDefault() if (user.balance < challengeInfo.amount) { setError("You don't have enough mana to create this challenge") return } if (!contract && user.balance < FIXED_ANTE + challengeInfo.amount) { setError( `You don't have enough mana to create this challenge and market. You need ${formatMoney( FIXED_ANTE + challengeInfo.amount )}` ) return } setIsCreating(true) onCreate(challengeInfo).finally(() => setIsCreating(false)) setFinishedCreating(true) }} > Challenge a friend to bet on{' '} {contract ? ( {contract.question} ) : ( setChallengeInfo({ ...challengeInfo, question: e.target.value, }) } /> )} You'll bet: setChallengeInfo((m: challengeInfo) => { return { ...m, amount: newAmount ?? 0, acceptorAmount: editingAcceptorAmount ? m.acceptorAmount : newAmount ?? 0, } }) } error={undefined} label={'M$'} inputClassName="w-24" /> on {challengeInfo.outcome === 'YES' ? : } setChallengeInfo((m: challengeInfo) => { return { ...m, outcome: m.outcome === 'YES' ? 'NO' : 'YES', } }) } > If they bet: { setEditingAcceptorAmount(true) setChallengeInfo((m: challengeInfo) => { return { ...m, acceptorAmount: newAmount ?? 0, } }) }} error={undefined} label={'M$'} inputClassName="w-24" /> on {challengeInfo.outcome === 'YES' ? : } {contract && ( { setEditingAcceptorAmount(true) const p = getProbability(contract) const prob = challengeInfo.outcome === 'YES' ? p : 1 - p const { amount } = challengeInfo const acceptorAmount = Math.round(amount / prob - amount) setChallengeInfo({ ...challengeInfo, acceptorAmount }) }} > Use market odds )} If the challenge is accepted, whoever is right will earn{' '} {formatMoney( challengeInfo.acceptorAmount + challengeInfo.amount || 0 )} {' '} in total.{' '} {!contract && ( Because there's no market yet, you'll be charged {formatMoney(FIXED_ANTE)} to create it. )} Create challenge bet {error} )} {loading && ( )} {finishedCreating && !loading && ( <> Share the challenge using the link. See your other challenges > )} > ) }