import clsx from 'clsx' import { useEffect, useState } from 'react' 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 dayjs from 'dayjs' import { Button } from '../button' import { DuplicateIcon } from '@heroicons/react/outline' import { createChallenge, getChallengeUrl } from 'web/lib/firebase/challenges' import { Contract } from 'common/contract' import { track } from 'web/lib/service/analytics' import { CopyLinkButton } from 'web/components/copy-link-button' import { getOutcomeProbability } from 'common/lib/calculate' import { SiteLink } from 'web/components/site-link' type challengeInfo = { amount: number expiresTime: number | null message: string outcome: 'YES' | 'NO' | number prob: number } export function CreateChallengeButton(props: { user: User | null | undefined contract: Contract }) { const { user, contract } = props const [open, setOpen] = useState(false) const [highlightedSlug, setHighlightedSlug] = useState('') return ( <> setOpen(newOpen)}> {/*// add a sign up to challenge button?*/} {user && ( { const challenge = await createChallenge({ creator: user, amount: newChallenge.amount, expiresTime: newChallenge.expiresTime, message: newChallenge.message, prob: newChallenge.prob / 100, outcome: newChallenge.outcome, contract: contract, }) challenge && setHighlightedSlug(getChallengeUrl(challenge)) }} highlightedSlug={highlightedSlug} /> )} setOpen(true)} className={clsx('whitespace-nowrap')} > Challenge > ) } function CreateChallengeForm(props: { user: User contract: Contract onCreate: (m: challengeInfo) => Promise highlightedSlug: string }) { const { user, onCreate, contract, highlightedSlug } = props const [isCreating, setIsCreating] = useState(false) const [finishedCreating, setFinishedCreating] = useState(false) const [copyPressed, setCopyPressed] = useState(false) const [error, setError] = useState('') setTimeout(() => setCopyPressed(false), 300) const defaultExpire = 'week' const isBinary = contract.outcomeType === 'BINARY' const isNumeric = contract.outcomeType === 'PSEUDO_NUMERIC' const defaultMessage = `${user.name} is challenging you to a bet! Do you think ${contract.question}` const [challengeInfo, setChallengeInfo] = useState({ expiresTime: dayjs().add(2, defaultExpire).valueOf(), outcome: 'YES', amount: 100, prob: Math.round(getOutcomeProbability(contract, 'YES') * 100), message: defaultMessage, }) useEffect(() => { setError('') }, [challengeInfo]) return ( <> {!finishedCreating && ( { e.preventDefault() if (user.balance < challengeInfo.amount) { setError('You do not have enough mana to create this challenge') return } setIsCreating(true) onCreate(challengeInfo).finally(() => setIsCreating(false)) setFinishedCreating(true) }} > You're betting M$ setChallengeInfo((m: challengeInfo) => { return { ...m, amount: parseInt(e.target.value) } }) } /> on {/*Outcome*/} {isBinary && ( setChallengeInfo((m: challengeInfo) => { return { ...m, outcome: e.target.value as 'YES' | 'NO', } }) } > Yes No )} {isNumeric && ( setChallengeInfo((m: challengeInfo) => { return { ...m, outcome: parseFloat(e.target.value) as number, } }) } /> )} {/*at*/} At setChallengeInfo((m: challengeInfo) => { return { ...m, prob: parseFloat(e.target.value), } }) } /> % {/**/} {/* Message*/} {/* */} {/* setChallengeInfo((m: challengeInfo) => {*/} {/* return { ...m, message: e.target.value }*/} {/* })*/} {/* }*/} {/* />*/} {/**/} Create {error} )} {finishedCreating && ( <> {highlightedSlug} { setCopyPressed(true) track('copy share challenge') }} buttonClassName="btn-sm rounded-l-none" toastClassName={'-left-40 -top-20 mt-1'} icon={DuplicateIcon} label={''} /> See your other challenges > )} > ) }