diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index 684b506a..b440dca0 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -45,8 +45,7 @@ type BetFilter = 'open' | 'closed' | 'resolved' | 'all' export function BetsList(props: { user: User }) { const { user } = props - const bets = useUserBets(user.id) - + const bets = useUserBets(user.id, { includeRedemptions: true }) const [contracts, setContracts] = useState() const [sort, setSort] = useState('newest') diff --git a/web/components/user-page.tsx b/web/components/user-page.tsx index 484fdc3e..04774c29 100644 --- a/web/components/user-page.tsx +++ b/web/components/user-page.tsx @@ -66,7 +66,7 @@ export function UserPage(props: { if (!user) return getUsersComments(user.id).then(setUsersComments) listContracts(user.id).then(setUsersContracts) - getUserBets(user.id).then(setUsersBets) + getUserBets(user.id, { includeRedemptions: false }).then(setUsersBets) }, [user]) useEffect(() => { diff --git a/web/hooks/use-user-bets.ts b/web/hooks/use-user-bets.ts index ead55729..a04a1545 100644 --- a/web/hooks/use-user-bets.ts +++ b/web/hooks/use-user-bets.ts @@ -6,11 +6,14 @@ import { listenForUserContractBets, } from 'web/lib/firebase/bets' -export const useUserBets = (userId: string | undefined) => { +export const useUserBets = ( + userId: string | undefined, + options: { includeRedemptions: boolean } +) => { const [bets, setBets] = useState(undefined) useEffect(() => { - if (userId) return listenForUserBets(userId, setBets) + if (userId) return listenForUserBets(userId, setBets, options) }, [userId]) return bets @@ -30,7 +33,10 @@ export const useUserContractBets = ( return bets } -export const useUserBetContracts = (userId: string | undefined) => { +export const useUserBetContracts = ( + userId: string | undefined, + options: { includeRedemptions: boolean } +) => { const [contractIds, setContractIds] = useState() useEffect(() => { @@ -42,11 +48,15 @@ export const useUserBetContracts = (userId: string | undefined) => { setContractIds(JSON.parse(userBetContractJson)) } - return listenForUserBets(userId, (bets) => { - const contractIds = _.uniq(bets.map((bet) => bet.contractId)) - setContractIds(contractIds) - localStorage.setItem(key, JSON.stringify(contractIds)) - }) + return listenForUserBets( + userId, + (bets) => { + const contractIds = _.uniq(bets.map((bet) => bet.contractId)) + setContractIds(contractIds) + localStorage.setItem(key, JSON.stringify(contractIds)) + }, + options + ) } }, [userId]) diff --git a/web/lib/firebase/bets.ts b/web/lib/firebase/bets.ts index c3ca8508..f948ed4f 100644 --- a/web/lib/firebase/bets.ts +++ b/web/lib/firebase/bets.ts @@ -60,23 +60,39 @@ export function listenForBets( }) } -export async function getUserBets(userId: string) { +export async function getUserBets( + userId: string, + options: { includeRedemptions: boolean } +) { + const { includeRedemptions } = options return getValues( query(collectionGroup(db, 'bets'), where('userId', '==', userId)) ) + .then((bets) => + bets.filter( + (bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte + ) + ) + .catch((reason) => reason) } export function listenForUserBets( userId: string, - setBets: (bets: Bet[]) => void + setBets: (bets: Bet[]) => void, + options: { includeRedemptions: boolean } ) { + const { includeRedemptions } = options const userQuery = query( collectionGroup(db, 'bets'), - where('userId', '==', userId) + where('userId', '==', userId), + orderBy('createdTime', 'desc') ) return listenForValues(userQuery, (bets) => { - bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime) - setBets(bets) + setBets( + bets.filter( + (bet) => (includeRedemptions || !bet.isRedemption) && !bet.isAnte + ) + ) }) }