diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index d02c23ef..1525d84f 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -37,7 +37,7 @@ import { getPseudoProbability, } from 'common/pseudo-numeric' import { SellRow } from './sell-row' -import { useSaveShares } from './use-save-shares' +import { useSaveBinaryShares } from './use-save-binary-shares' import { SignUpPrompt } from './sign-up-prompt' import { isIOS } from 'web/lib/util/device' import { ProbabilityInput } from './probability-input' @@ -56,12 +56,7 @@ export function BetPanel(props: { const userBets = useUserContractBets(user?.id, contract.id) const unfilledBets = useUnfilledBets(contract.id) ?? [] const yourUnfilledBets = unfilledBets.filter((bet) => bet.userId === user?.id) - const { yesFloorShares, noFloorShares } = useSaveShares(contract, userBets) - const sharesOutcome = yesFloorShares - ? 'YES' - : noFloorShares - ? 'NO' - : undefined + const { sharesOutcome } = useSaveBinaryShares(contract, userBets) const [isLimitOrder, setIsLimitOrder] = useState(false) diff --git a/web/components/bet-row.tsx b/web/components/bet-row.tsx index 23f458ea..712d4a2c 100644 --- a/web/components/bet-row.tsx +++ b/web/components/bet-row.tsx @@ -8,7 +8,7 @@ import { Modal } from './layout/modal' import { SellButton } from './sell-button' import { useUser } from 'web/hooks/use-user' import { useUserContractBets } from 'web/hooks/use-user-bets' -import { useSaveShares } from './use-save-shares' +import { useSaveBinaryShares } from './use-save-binary-shares' // Inline version of a bet panel. Opens BetPanel in a new modal. export default function BetRow(props: { @@ -24,10 +24,8 @@ export default function BetRow(props: { ) const user = useUser() const userBets = useUserContractBets(user?.id, contract.id) - const { yesFloorShares, noFloorShares, yesShares, noShares } = useSaveShares( - contract, - userBets - ) + const { yesShares, noShares, hasYesShares, hasNoShares } = + useSaveBinaryShares(contract, userBets) return ( <> @@ -40,7 +38,7 @@ export default function BetRow(props: { setBetChoice(choice) }} replaceNoButton={ - yesFloorShares > 0 ? ( + hasYesShares ? ( 0 ? ( + hasNoShares ? ( - - - ) - return ( - {hasUpShares > 0 ? ( + {hasUpShares ? ( setDownHover(false)} onClick={() => placeQuickBet('DOWN')} > - {hasDownShares > 0 ? ( + {hasDownShares ? (
- You have {formatWithCommas(floorShares)}{' '} + You have {formatWithCommas(shares)}{' '} diff --git a/web/components/use-save-binary-shares.ts b/web/components/use-save-binary-shares.ts new file mode 100644 index 00000000..fefa8a55 --- /dev/null +++ b/web/components/use-save-binary-shares.ts @@ -0,0 +1,56 @@ +import { BinaryContract, PseudoNumericContract } from 'common/contract' +import { Bet } from 'common/bet' +import { useEffect, useState } from 'react' +import { partition, sumBy } from 'lodash' +import { safeLocalStorage } from 'web/lib/util/local' + +export const useSaveBinaryShares = ( + contract: BinaryContract | PseudoNumericContract, + userBets: Bet[] | undefined +) => { + const [savedShares, setSavedShares] = useState({ yesShares: 0, noShares: 0 }) + + const [yesBets, noBets] = partition( + userBets ?? [], + (bet) => bet.outcome === 'YES' + ) + const [yesShares, noShares] = userBets + ? [sumBy(yesBets, (bet) => bet.shares), sumBy(noBets, (bet) => bet.shares)] + : [savedShares.yesShares, savedShares.noShares] + + useEffect(() => { + const local = safeLocalStorage() + + // Read shares from local storage. + const savedShares = local?.getItem(`${contract.id}-shares`) + if (savedShares) { + setSavedShares(JSON.parse(savedShares)) + } + + if (userBets) { + // Save shares to local storage. + const sharesData = JSON.stringify({ yesShares, noShares }) + local?.setItem(`${contract.id}-shares`, sharesData) + } + }, [contract.id, userBets, noShares, yesShares]) + + const hasYesShares = yesShares >= 1 + const hasNoShares = noShares >= 1 + + const sharesOutcome = hasYesShares + ? ('YES' as const) + : hasNoShares + ? ('NO' as const) + : undefined + const shares = + sharesOutcome === 'YES' ? yesShares : sharesOutcome === 'NO' ? noShares : 0 + + return { + yesShares, + noShares, + shares, + sharesOutcome, + hasYesShares, + hasNoShares, + } +} diff --git a/web/components/use-save-shares.ts b/web/components/use-save-shares.ts deleted file mode 100644 index 494c1f29..00000000 --- a/web/components/use-save-shares.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { Contract } from 'common/contract' -import { Bet } from 'common/bet' -import { useEffect, useState } from 'react' -import { partition, sumBy } from 'lodash' -import { safeLocalStorage } from 'web/lib/util/local' - -export const useSaveShares = ( - contract: Contract, - userBets: Bet[] | undefined, - freeResponseAnswerOutcome?: string -) => { - const [savedShares, setSavedShares] = useState< - | { - yesShares: number - noShares: number - yesFloorShares: number - noFloorShares: number - } - | undefined - >() - - // TODO: How do we handle numeric yes / no bets? - maybe bet amounts above vs below the highest peak - const [yesBets, noBets] = partition(userBets ?? [], (bet) => - freeResponseAnswerOutcome - ? bet.outcome === freeResponseAnswerOutcome - : bet.outcome === 'YES' - ) - const [yesShares, noShares] = [ - sumBy(yesBets, (bet) => bet.shares), - sumBy(noBets, (bet) => bet.shares), - ] - - const yesFloorShares = Math.round(yesShares) === 0 ? 0 : Math.floor(yesShares) - const noFloorShares = Math.round(noShares) === 0 ? 0 : Math.floor(noShares) - - useEffect(() => { - const local = safeLocalStorage() - // Save yes and no shares to local storage. - const savedShares = local?.getItem(`${contract.id}-shares`) - if (!userBets && savedShares) { - setSavedShares(JSON.parse(savedShares)) - } - - if (userBets) { - const updatedShares = { yesShares, noShares } - local?.setItem(`${contract.id}-shares`, JSON.stringify(updatedShares)) - } - }, [contract.id, userBets, noShares, yesShares]) - - if (userBets) return { yesShares, noShares, yesFloorShares, noFloorShares } - return ( - savedShares ?? { - yesShares: 0, - noShares: 0, - yesFloorShares: 0, - noFloorShares: 0, - } - ) -}