Show all limit orders in a tab

This commit is contained in:
James Grugett 2022-07-13 12:14:58 -05:00
parent 737d803903
commit f1eea66588
2 changed files with 89 additions and 37 deletions

View File

@ -59,6 +59,9 @@ export function BetPanel(props: {
track('toggle limit order') track('toggle limit order')
} }
const showLimitOrders =
(isLimitOrder && unfilledBets.length > 0) || yourUnfilledBets.length > 0
return ( return (
<Col className={className}> <Col className={className}>
<SellRow <SellRow
@ -94,12 +97,8 @@ export function BetPanel(props: {
<SignUpPrompt /> <SignUpPrompt />
</Col> </Col>
{yourUnfilledBets.length > 0 && ( {showLimitOrders && (
<LimitBets <LimitBets className="mt-4" contract={contract} bets={unfilledBets} />
className="mt-4"
contract={contract}
bets={yourUnfilledBets}
/>
)} )}
</Col> </Col>
) )
@ -119,6 +118,8 @@ export function SimpleBetPanel(props: {
const unfilledBets = useUnfilledBets(contract.id) ?? [] const unfilledBets = useUnfilledBets(contract.id) ?? []
const yourUnfilledBets = unfilledBets.filter((bet) => bet.userId === user?.id) const yourUnfilledBets = unfilledBets.filter((bet) => bet.userId === user?.id)
const showLimitOrders =
(isLimitOrder && unfilledBets.length > 0) || yourUnfilledBets.length > 0
return ( return (
<Col className={className}> <Col className={className}>
@ -158,12 +159,8 @@ export function SimpleBetPanel(props: {
<SignUpPrompt /> <SignUpPrompt />
</Col> </Col>
{yourUnfilledBets.length > 0 && ( {showLimitOrders && (
<LimitBets <LimitBets className="mt-4" contract={contract} bets={unfilledBets} />
className="mt-4"
contract={contract}
bets={yourUnfilledBets}
/>
)} )}
</Col> </Col>
) )

View File

@ -5,8 +5,11 @@ import { getFormattedMappedValue } from 'common/pseudo-numeric'
import { formatMoney, formatPercent } from 'common/util/format' import { formatMoney, formatPercent } from 'common/util/format'
import { sortBy } from 'lodash' import { sortBy } from 'lodash'
import { useState } from 'react' import { useState } from 'react'
import { useUser, useUserById } from 'web/hooks/use-user'
import { cancelBet } from 'web/lib/firebase/api' import { cancelBet } from 'web/lib/firebase/api'
import { Avatar } from './avatar'
import { Col } from './layout/col' import { Col } from './layout/col'
import { Tabs } from './layout/tabs'
import { LoadingIndicator } from './loading-indicator' import { LoadingIndicator } from './loading-indicator'
import { BinaryOutcomeLabel, PseudoNumericOutcomeLabel } from './outcome-label' import { BinaryOutcomeLabel, PseudoNumericOutcomeLabel } from './outcome-label'
@ -16,12 +19,14 @@ export function LimitBets(props: {
hideLabel?: boolean hideLabel?: boolean
className?: string className?: string
}) { }) {
const { contract, bets, hideLabel, className } = props const { contract, bets, className } = props
const recentBets = sortBy( const sortedBets = sortBy(
bets, bets,
(bet) => -1 * bet.limitProb, (bet) => -1 * bet.limitProb,
(bet) => -1 * bet.createdTime (bet) => -1 * bet.createdTime
) )
const user = useUser()
const yourBets = sortedBets.filter((bet) => bet.userId === user?.id)
return ( return (
<Col <Col
@ -30,25 +35,62 @@ export function LimitBets(props: {
'gap-2 overflow-hidden rounded bg-white px-4 py-3' 'gap-2 overflow-hidden rounded bg-white px-4 py-3'
)} )}
> >
{!hideLabel && ( <Tabs
<div className="px-2 py-3 text-2xl">Your limit orders</div> tabs={[
)} ...(yourBets.length > 0
? [
{
title: 'Your limit orders',
content: (
<LimitOrderTable
limitBets={yourBets}
contract={contract}
isYou={true}
/>
),
},
]
: []),
{
title: 'All limit orders',
content: (
<LimitOrderTable
limitBets={sortedBets}
contract={contract}
isYou={false}
/>
),
},
]}
/>
</Col>
)
}
function LimitOrderTable(props: {
limitBets: LimitBet[]
contract: CPMMBinaryContract | PseudoNumericContract
isYou: boolean
}) {
const { limitBets, contract, isYou } = props
return (
<table className="table-compact table w-full rounded text-gray-500"> <table className="table-compact table w-full rounded text-gray-500">
<tbody> <tbody>
{recentBets.map((bet) => ( {limitBets.map((bet) => (
<LimitBet key={bet.id} bet={bet} contract={contract} /> <LimitBet key={bet.id} bet={bet} contract={contract} isYou={isYou} />
))} ))}
</tbody> </tbody>
</table> </table>
</Col>
) )
} }
function LimitBet(props: { function LimitBet(props: {
contract: CPMMBinaryContract | PseudoNumericContract contract: CPMMBinaryContract | PseudoNumericContract
bet: LimitBet bet: LimitBet
isYou: boolean
}) { }) {
const { contract, bet } = props const { contract, bet, isYou } = props
const { orderAmount, amount, limitProb, outcome } = bet const { orderAmount, amount, limitProb, outcome } = bet
const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC' const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC'
@ -59,8 +101,19 @@ function LimitBet(props: {
setIsCancelling(true) setIsCancelling(true)
} }
const user = useUserById(bet.userId)
return ( return (
<tr> <tr>
{!isYou && (
<td>
<Avatar
size={'sm'}
avatarUrl={user?.avatarUrl}
username={user?.username}
/>
</td>
)}
<td> <td>
<div className="pl-2"> <div className="pl-2">
{isPseudoNumeric ? ( {isPseudoNumeric ? (
@ -76,6 +129,7 @@ function LimitBet(props: {
? getFormattedMappedValue(contract)(limitProb) ? getFormattedMappedValue(contract)(limitProb)
: formatPercent(limitProb)} : formatPercent(limitProb)}
</td> </td>
{isYou && (
<td> <td>
{isCancelling ? ( {isCancelling ? (
<LoadingIndicator /> <LoadingIndicator />
@ -88,6 +142,7 @@ function LimitBet(props: {
</button> </button>
)} )}
</td> </td>
)}
</tr> </tr>
) )
} }