diff --git a/web/components/bet-row.tsx b/web/components/bet-row.tsx index 60e9e11a..8bd8f3b8 100644 --- a/web/components/bet-row.tsx +++ b/web/components/bet-row.tsx @@ -1,14 +1,15 @@ import clsx from 'clsx' import { useState } from 'react' -import { BetPanelSwitcher, useSaveShares } from './bet-panel' +import { BetPanelSwitcher } from './bet-panel' import { Row } from './layout/row' import { YesNoSelector } from './yes-no-selector' import { Binary, CPMM, DPM, FullContract } from '../../common/contract' import { Modal } from './layout/modal' -import { SellRow } from './sell-row' +import { SellButton, SellRow } from './sell-row' import { useUser } from '../hooks/use-user' import { useUserContractBets } from '../hooks/use-user-bets' +import { useSaveShares } from './use-save-shares' // Inline version of a bet panel. Opens BetPanel in a new modal. export default function BetRow(props: { @@ -16,17 +17,14 @@ export default function BetRow(props: { className?: string labelClassName?: string }) { - const { className, labelClassName } = props + const { className, labelClassName, contract } = props const [open, setOpen] = useState(false) const [betChoice, setBetChoice] = useState<'YES' | 'NO' | undefined>( undefined ) const user = useUser() - const userBets = useUserContractBets(user?.id, props.contract.id) - const { yesFloorShares, noFloorShares } = useSaveShares( - props.contract, - userBets - ) + const userBets = useUserContractBets(user?.id, contract.id) + const { yesFloorShares, noFloorShares } = useSaveShares(contract, userBets) return ( <> @@ -43,28 +41,20 @@ export default function BetRow(props: { }} replaceNoButton={ yesFloorShares > noFloorShares && yesFloorShares > 0 ? ( - + ) : undefined } replaceYesButton={ noFloorShares > yesFloorShares && noFloorShares > 0 ? ( - + ) : undefined } /> setOpen(false)} /> diff --git a/web/components/sell-row.tsx b/web/components/sell-row.tsx index bceffa72..ebeb0ef6 100644 --- a/web/components/sell-row.tsx +++ b/web/components/sell-row.tsx @@ -8,88 +8,67 @@ import { formatWithCommas } from '../../common/util/format' import { OutcomeLabel } from './outcome-label' import { Modal } from './layout/modal' import { Title } from './title' -import { SellPanel, useSaveShares } from './bet-panel' +import { SellPanel } from './bet-panel' import { useUserContractBets } from '../hooks/use-user-bets' import clsx from 'clsx' +import { useSaveShares } from './use-save-shares' export function SellRow(props: { contract: FullContract user: User | null | undefined className?: string - buttonOnly?: boolean }) { - const { className } = props + const { className, contract, user } = props - const userBets = useUserContractBets(props.user?.id, props.contract.id) + const userBets = useUserContractBets(user?.id, contract.id) const [showSellModal, setShowSellModal] = useState(false) - const { mechanism } = props.contract - const { yesFloorShares, noFloorShares } = useSaveShares( - props.contract, + const { mechanism } = contract + const { yesFloorShares, noFloorShares, yesShares, noShares } = useSaveShares( + contract, userBets ) - const shares = yesFloorShares || noFloorShares + const floorShares = yesFloorShares || noFloorShares const sharesOutcome = yesFloorShares ? 'YES' : noFloorShares ? 'NO' : undefined - if (sharesOutcome && props.user && mechanism === 'cpmm-1') { + if (sharesOutcome && user && mechanism === 'cpmm-1') { return (
- {props.buttonOnly ? ( - + + +
+ You have {formatWithCommas(floorShares)}{' '} + {' '} + shares +
+ -
- {'(' + shares + ' shares)'} -
- - ) : ( - - -
- You have {formatWithCommas(shares)}{' '} - {' '} - shares -
- - -
- - )} +
+ {showSellModal && ( } - user={props.user} + contract={contract as FullContract} + user={user} userBets={userBets ?? []} - shares={shares} + shares={yesShares || noShares} sharesOutcome={sharesOutcome} setOpen={setShowSellModal} /> @@ -98,7 +77,63 @@ export function SellRow(props: { ) } - return
+ return
+} + +export function SellButton(props: { + contract: FullContract + user: User | null | undefined +}) { + const { contract, user } = props + + const userBets = useUserContractBets(user?.id, contract.id) + const [showSellModal, setShowSellModal] = useState(false) + + const { mechanism } = contract + const { yesFloorShares, noFloorShares, yesShares, noShares } = useSaveShares( + contract, + userBets + ) + const floorShares = yesFloorShares || noFloorShares + const sharesOutcome = yesFloorShares + ? 'YES' + : noFloorShares + ? 'NO' + : undefined + + if (sharesOutcome && user && mechanism === 'cpmm-1') { + return ( + + +
+ {'(' + floorShares + ' shares)'} +
+ {showSellModal && ( + } + user={user} + userBets={userBets ?? []} + shares={yesShares || noShares} + sharesOutcome={sharesOutcome} + setOpen={setShowSellModal} + /> + )} + + ) + } + return
} function SellSharesModal(props: { @@ -120,7 +155,7 @@ function SellSharesModal(props: { You have {formatWithCommas(Math.floor(shares))}{' '} {' '} shares