From e5d02dde062314c89d742b40f520c55110b9af9b Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Sun, 13 Mar 2022 13:55:42 -0700 Subject: [PATCH] Allow filtering your trades --- web/components/bets-list.tsx | 77 +++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index 8136042f..6c8dac64 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -38,6 +38,7 @@ import { LoadingIndicator } from './loading-indicator' import { SiteLink } from './site-link' type BetSort = 'newest' | 'profit' | 'settled' | 'value' +type BetFilter = 'open' | 'closed' | 'resolved' | 'all' export function BetsList(props: { user: User }) { const { user } = props @@ -46,6 +47,7 @@ export function BetsList(props: { user: User }) { const [contracts, setContracts] = useState() const [sort, setSort] = useState('value') + const [filter, setFilter] = useState('open') useEffect(() => { if (bets) { @@ -69,11 +71,10 @@ export function BetsList(props: { user: User }) { } if (bets.length === 0) return - // Decending creation time. bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime) - const contractBets = _.groupBy(bets, 'contractId') + const contractsById = _.fromPairs(contracts.map((c) => [c.id, c])) const contractsCurrentValue = _.mapValues( contractBets, @@ -81,7 +82,7 @@ export function BetsList(props: { user: User }) { return _.sumBy(bets, (bet) => { if (bet.isSold || bet.sale) return 0 - const contract = contracts.find((c) => c.id === contractId) + const contract = contractsById[contractId] const payout = contract ? calculatePayout(contract, bet, 'MKT') : 0 return payout - (bet.loanAmount ?? 0) }) @@ -94,29 +95,30 @@ export function BetsList(props: { user: User }) { }) }) - let sortedContracts = contracts - if (sort === 'profit') { - sortedContracts = _.sortBy( - contracts, - (c) => -1 * (contractsCurrentValue[c.id] - contractsInvestment[c.id]) - ) - } else if (sort === 'value') { - sortedContracts = _.sortBy(contracts, (c) => -contractsCurrentValue[c.id]) - } else if (sort === 'newest') - sortedContracts = _.sortBy( - contracts, - (c) => -1 * Math.max(...contractBets[c.id].map((bet) => bet.createdTime)) - ) - else if (sort === 'settled') - sortedContracts = _.sortBy(contracts, (c) => -1 * (c.resolutionTime ?? 0)) + const FILTERS: Record boolean> = { + resolved: (c) => !!c.resolutionTime, + closed: (c) => + !FILTERS.resolved(c) && (c.closeTime ?? Infinity) < Date.now(), + open: (c) => !(FILTERS.closed(c) || FILTERS.resolved(c)), + all: () => true, + // Pepe notes: most users want "settled", to see when their bets or sold; or "realized profit" + } + const SORTS: Record number> = { + profit: (c) => contractsCurrentValue[c.id] - contractsInvestment[c.id], + value: (c) => contractsCurrentValue[c.id], + newest: (c) => + Math.max(...contractBets[c.id].map((bet) => bet.createdTime)), + settled: (c) => c.resolutionTime ?? 0, + } + const displayedContracts = _.sortBy(contracts, SORTS[sort]) + .reverse() + .filter(FILTERS[filter]) const [settled, unsettled] = _.partition( - sortedContracts, + contracts, (c) => c.isResolved || contractsInvestment[c.id] === 0 ) - const displayedContracts = sort === 'settled' ? settled : unsettled - const currentInvestment = _.sumBy(unsettled, (c) => contractsInvestment[c.id]) const currentBetsValue = _.sumBy( @@ -151,16 +153,29 @@ export function BetsList(props: { user: User }) { - + + + + + {displayedContracts.length === 0 ? (