From 0c3fa9f06584bc2813699fb5305c829ceb4e4ec9 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 25 May 2022 16:51:33 -0600 Subject: [PATCH] Handle free response outcomes for yes/no shares (#338) --- web/components/contract/quick-bet.tsx | 18 +++++++++++------- web/components/use-save-shares.ts | 21 +++++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/web/components/contract/quick-bet.tsx b/web/components/contract/quick-bet.tsx index daca33a7..a9415c5c 100644 --- a/web/components/contract/quick-bet.tsx +++ b/web/components/contract/quick-bet.tsx @@ -38,16 +38,20 @@ export function QuickBet(props: { contract: Contract }) { const user = useUser() const userBets = useUserContractBets(user?.id, contract.id) + const topAnswer = + contract.outcomeType === 'FREE_RESPONSE' + ? getTopAnswer(contract as FreeResponseContract) + : undefined + + // TODO: yes/no from useSaveShares doesn't work on numeric contracts const { yesFloorShares, noFloorShares } = useSaveShares( - contract as FullContract, - userBets + contract as FullContract, + userBets, + topAnswer?.number.toString() || undefined ) - // TODO: This relies on a hack in useSaveShares, where noFloorShares includes - // all non-YES shares. Ideally, useSaveShares should group by all outcomes - const hasUpShares = - contract.outcomeType === 'BINARY' ? yesFloorShares : noFloorShares + const hasUpShares = yesFloorShares || contract.outcomeType === 'NUMERIC' const hasDownShares = - contract.outcomeType === 'BINARY' ? noFloorShares : yesFloorShares + noFloorShares && yesFloorShares <= 0 && contract.outcomeType !== 'NUMERIC' const [upHover, setUpHover] = useState(false) const [downHover, setDownHover] = useState(false) diff --git a/web/components/use-save-shares.ts b/web/components/use-save-shares.ts index 977c8a97..bd1ac5a1 100644 --- a/web/components/use-save-shares.ts +++ b/web/components/use-save-shares.ts @@ -1,11 +1,18 @@ -import { Binary, CPMM, DPM, FullContract } from 'common/contract' +import { + Binary, + CPMM, + DPM, + FreeResponseContract, + FullContract, +} from 'common/contract' import { Bet } from 'common/bet' import { useEffect, useState } from 'react' import { partition, sumBy } from 'lodash' export const useSaveShares = ( - contract: FullContract, - userBets: Bet[] | undefined + contract: FullContract, + userBets: Bet[] | undefined, + freeResponseAnswerOutcome?: string ) => { const [savedShares, setSavedShares] = useState< | { @@ -17,9 +24,11 @@ export const useSaveShares = ( | undefined >() - const [yesBets, noBets] = partition( - userBets ?? [], - (bet) => bet.outcome === 'YES' + // 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),