Show total of active bets and their current value. Arrange unresolved markets first.

This commit is contained in:
jahooma 2021-12-15 21:04:38 -06:00
parent 09dba6ef1e
commit fe9def453b

View File

@ -1,14 +1,14 @@
import Link from 'next/link' import Link from 'next/link'
import _ from 'lodash' import _ from 'lodash'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { useContract } from '../hooks/use-contract' import { useEffect, useState } from 'react'
import { useUserBets } from '../hooks/use-user-bets' import { useUserBets } from '../hooks/use-user-bets'
import { Bet } from '../lib/firebase/bets' import { Bet } from '../lib/firebase/bets'
import { User } from '../lib/firebase/users' import { User } from '../lib/firebase/users'
import { formatMoney, formatPercent } from '../lib/util/format' import { formatMoney, formatPercent } from '../lib/util/format'
import { Col } from './layout/col' import { Col } from './layout/col'
import { Spacer } from './layout/spacer' 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 { Row } from './layout/row'
import { UserLink } from './user-page' import { UserLink } from './user-page'
import { import {
@ -21,6 +21,22 @@ export function BetsList(props: { user: User }) {
const { user } = props const { user } = props
const bets = useUserBets(user?.id ?? '') const bets = useUserBets(user?.id ?? '')
const [contracts, setContracts] = useState<Contract[]>([])
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') { if (bets === 'loading') {
return <></> return <></>
} }
@ -29,25 +45,44 @@ export function BetsList(props: { user: User }) {
const contractBets = _.groupBy(bets, 'contractId') 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 ( return (
<Col className="mt-6 gap-10"> <Col className="mt-6 gap-10">
{Object.keys(contractBets).map((contractId) => ( <Row className="px-4 gap-8">
<Col>
<div className="text-sm text-gray-500">Active bets</div>
<div>{formatMoney(currentBets)}</div>
</Col>
<Col>
<div className="text-sm text-gray-500">Current value</div>
<div>{formatMoney(currentBetsValue)}</div>
</Col>
</Row>
{[...unresolved, ...resolved].map((contract) => (
<MyContractBets <MyContractBets
key={contractId} key={contract.id}
contractId={contractId} contract={contract}
bets={contractBets[contractId]} bets={contractBets[contract.id]}
/> />
))} ))}
</Col> </Col>
) )
} }
function MyContractBets(props: { contractId: string; bets: Bet[] }) { function MyContractBets(props: { contract: Contract; bets: Bet[] }) {
const { contractId, bets } = props const { bets, contract } = props
const contract = useContract(contractId)
if (contract === 'loading' || contract === null) return <></>
const { resolution } = contract const { resolution } = contract
const betsTotal = _.sumBy(bets, (bet) => bet.amount) const betsTotal = _.sumBy(bets, (bet) => bet.amount)
@ -93,7 +128,7 @@ function MyContractBets(props: { contractId: string; bets: Bet[] }) {
<Row className="gap-8"> <Row className="gap-8">
<Col> <Col>
<div className="text-sm text-gray-500">Total bets</div> <div className="text-sm text-gray-500">Total bet</div>
<div>{formatMoney(betsTotal)}</div> <div>{formatMoney(betsTotal)}</div>
</Col> </Col>
{resolution ? ( {resolution ? (