manifold/web/components/bets-list.tsx

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-12-15 07:41:50 +00:00
import Link from 'next/link'
import _ from 'lodash'
import dayjs from 'dayjs'
import { useContract } from '../hooks/use-contract'
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 { ContractDetails } from './contracts-list'
2021-12-15 18:41:18 +00:00
import { Spacer } from './layout/spacer'
2021-12-15 07:41:50 +00:00
export function BetsList(props: { user: User }) {
const { user } = props
const bets = useUserBets(user?.id ?? '')
if (bets === 'loading') {
return <></>
}
if (bets.length === 0) return <div>You have not made any bets yet!</div>
const contractBets = _.groupBy(bets, 'contractId')
return (
2021-12-15 18:41:18 +00:00
<Col className="mt-6 gap-10">
2021-12-15 07:41:50 +00:00
{Object.keys(contractBets).map((contractId) => (
2021-12-15 18:41:18 +00:00
<ContractBetsTable
2021-12-15 07:41:50 +00:00
key={contractId}
contractId={contractId}
bets={contractBets[contractId]}
/>
))}
</Col>
)
}
2021-12-15 18:41:18 +00:00
function ContractBetsTable(props: { contractId: string; bets: Bet[] }) {
2021-12-15 07:41:50 +00:00
const { contractId, bets } = props
const contract = useContract(contractId)
if (contract === 'loading' || contract === null) return <></>
return (
2021-12-15 18:41:18 +00:00
<div className="px-4">
<Link href={`/contract/${contractId}`}>
<a>
<p className="font-medium text-indigo-700 mb-2">
{contract.question}
</p>
<ContractDetails contract={contract} />
</a>
</Link>
<Spacer h={4} />
<div className="overflow-x-auto">
<table className="table table-zebra table-compact text-gray-500 w-full">
<thead>
<tr className="p-2">
<th>Outcome</th>
<th>Amount</th>
<th>Probability</th>
<th>Estimated payoff</th>
<th>Date</th>
</tr>
</thead>
<tbody>
{bets.map((bet) => (
<BetRow key={bet.id} bet={bet} />
))}
</tbody>
</table>
</div>
2021-12-15 07:41:50 +00:00
</div>
)
}
2021-12-15 18:41:18 +00:00
function BetRow(props: { bet: Bet }) {
2021-12-15 07:41:50 +00:00
const { bet } = props
2021-12-15 18:41:18 +00:00
const { amount, outcome, createdTime, probBefore, probAfter, dpmWeight } = bet
2021-12-15 07:41:50 +00:00
return (
2021-12-15 18:41:18 +00:00
<tr>
<td>{outcome}</td>
<td>{formatMoney(amount)}</td>
<td>
{formatPercent(probBefore)} {formatPercent(probAfter)}
</td>
<td>{formatMoney(amount + dpmWeight)}</td>
<td>{dayjs(createdTime).format('MMM D, H:mma')}</td>
</tr>
2021-12-15 07:41:50 +00:00
)
}