From fe9def453ba70db4e00edbd7bc1a78e8aa391365 Mon Sep 17 00:00:00 2001 From: jahooma Date: Wed, 15 Dec 2021 21:04:38 -0600 Subject: [PATCH] Show total of active bets and their current value. Arrange unresolved markets first. --- web/components/bets-list.tsx | 61 ++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index e9d8664b..1c1f7341 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -1,14 +1,14 @@ import Link from 'next/link' import _ from 'lodash' import dayjs from 'dayjs' -import { useContract } from '../hooks/use-contract' +import { useEffect, useState } from 'react' import { useUserBets } from '../hooks/use-user-bets' import { Bet } from '../lib/firebase/bets' import { User } from '../lib/firebase/users' import { formatMoney, formatPercent } from '../lib/util/format' import { Col } from './layout/col' import { Spacer } from './layout/spacer' -import { Contract, path } from '../lib/firebase/contracts' +import { Contract, getContract, path } from '../lib/firebase/contracts' import { Row } from './layout/row' import { UserLink } from './user-page' import { @@ -21,6 +21,22 @@ export function BetsList(props: { user: User }) { const { user } = props const bets = useUserBets(user?.id ?? '') + const [contracts, setContracts] = useState([]) + + useEffect(() => { + const loadedBets = bets === 'loading' ? [] : bets + const contractIds = _.uniq(loadedBets.map((bet) => bet.contractId)) + + let disposed = false + Promise.all(contractIds.map((id) => getContract(id))).then((contracts) => { + if (!disposed) setContracts(contracts.filter(Boolean) as Contract[]) + }) + + return () => { + disposed = true + } + }, [bets]) + if (bets === 'loading') { return <> } @@ -29,25 +45,44 @@ export function BetsList(props: { user: User }) { const contractBets = _.groupBy(bets, 'contractId') + const [resolved, unresolved] = _.partition( + contracts, + (contract) => contract.isResolved + ) + const currentBets = _.sumBy(unresolved, (contract) => + _.sumBy(contractBets[contract.id], (bet) => bet.amount) + ) + + const currentBetsValue = _.sumBy(unresolved, (contract) => + _.sumBy(contractBets[contract.id], (bet) => currentValue(contract, bet)) + ) + return ( - {Object.keys(contractBets).map((contractId) => ( + + +
Active bets
+
{formatMoney(currentBets)}
+ + +
Current value
+
{formatMoney(currentBetsValue)}
+ +
+ + {[...unresolved, ...resolved].map((contract) => ( ))} ) } -function MyContractBets(props: { contractId: string; bets: Bet[] }) { - const { contractId, bets } = props - - const contract = useContract(contractId) - if (contract === 'loading' || contract === null) return <> - +function MyContractBets(props: { contract: Contract; bets: Bet[] }) { + const { bets, contract } = props const { resolution } = contract const betsTotal = _.sumBy(bets, (bet) => bet.amount) @@ -93,7 +128,7 @@ function MyContractBets(props: { contractId: string; bets: Bet[] }) { -
Total bets
+
Total bet
{formatMoney(betsTotal)}
{resolution ? (