2022-09-05 23:09:01 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import { contractPath } from 'web/lib/firebase/contracts'
|
|
|
|
import { CPMMContract } from 'common/contract'
|
|
|
|
import { formatPercent } from 'common/util/format'
|
2022-09-12 05:39:04 +00:00
|
|
|
import { SiteLink } from '../site-link'
|
2022-09-07 06:18:11 +00:00
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { Row } from '../layout/row'
|
2022-09-12 05:39:04 +00:00
|
|
|
import { LoadingIndicator } from '../loading-indicator'
|
2022-09-17 22:58:08 +00:00
|
|
|
import { ProfitBadge } from '../bets-list'
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
export function ProbChangeTable(props: {
|
|
|
|
changes:
|
|
|
|
| { positiveChanges: CPMMContract[]; negativeChanges: CPMMContract[] }
|
|
|
|
| undefined
|
2022-09-16 21:12:24 +00:00
|
|
|
full?: boolean
|
2022-09-12 05:39:04 +00:00
|
|
|
}) {
|
2022-09-16 21:12:24 +00:00
|
|
|
const { changes, full } = props
|
2022-09-12 05:39:04 +00:00
|
|
|
|
|
|
|
if (!changes) return <LoadingIndicator />
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
const { positiveChanges, negativeChanges } = changes
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-18 00:47:04 +00:00
|
|
|
const threshold = 0.01
|
|
|
|
const positiveAboveThreshold = positiveChanges.filter(
|
|
|
|
(c) => c.probChanges.day > threshold
|
2022-09-12 05:39:04 +00:00
|
|
|
)
|
2022-09-18 00:47:04 +00:00
|
|
|
const negativeAboveThreshold = negativeChanges.filter(
|
|
|
|
(c) => c.probChanges.day < threshold
|
|
|
|
)
|
|
|
|
const maxRows = Math.min(
|
|
|
|
positiveAboveThreshold.length,
|
|
|
|
negativeAboveThreshold.length
|
|
|
|
)
|
|
|
|
const rows = full ? maxRows : Math.min(3, maxRows)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-18 00:47:04 +00:00
|
|
|
const filteredPositiveChanges = positiveAboveThreshold.slice(0, rows)
|
|
|
|
const filteredNegativeChanges = negativeAboveThreshold.slice(0, rows)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
if (rows === 0) return <div className="px-4 text-gray-500">None</div>
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-12 05:39:04 +00:00
|
|
|
<Col className="mb-4 w-full divide-x-2 divide-y rounded-lg bg-white shadow-md md:flex-row md:divide-y-0">
|
|
|
|
<Col className="flex-1 divide-y">
|
|
|
|
{filteredPositiveChanges.map((contract) => (
|
2022-09-16 21:12:24 +00:00
|
|
|
<ProbChangeRow key={contract.id} contract={contract} />
|
2022-09-12 05:39:04 +00:00
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
<Col className="flex-1 divide-y">
|
|
|
|
{filteredNegativeChanges.map((contract) => (
|
2022-09-16 21:12:24 +00:00
|
|
|
<ProbChangeRow key={contract.id} contract={contract} />
|
2022-09-12 05:39:04 +00:00
|
|
|
))}
|
2022-09-07 06:18:11 +00:00
|
|
|
</Col>
|
2022-09-08 06:36:34 +00:00
|
|
|
</Col>
|
2022-09-05 23:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
function ProbChangeRow(props: { contract: CPMMContract }) {
|
|
|
|
const { contract } = props
|
|
|
|
return (
|
2022-09-17 23:00:24 +00:00
|
|
|
<Row className="items-center justify-between gap-4 hover:bg-gray-100">
|
2022-09-16 21:12:24 +00:00
|
|
|
<SiteLink
|
2022-09-17 22:58:08 +00:00
|
|
|
className="p-4 pr-0 font-semibold text-indigo-700"
|
2022-09-16 21:12:24 +00:00
|
|
|
href={contractPath(contract)}
|
|
|
|
>
|
|
|
|
<span className="line-clamp-2">{contract.question}</span>
|
|
|
|
</SiteLink>
|
2022-09-17 22:58:08 +00:00
|
|
|
<ProbChange className="py-2 pr-4 text-xl" contract={contract} />
|
2022-09-16 21:12:24 +00:00
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-05 23:09:01 +00:00
|
|
|
export function ProbChange(props: {
|
|
|
|
contract: CPMMContract
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, className } = props
|
|
|
|
const {
|
2022-09-17 22:58:08 +00:00
|
|
|
prob,
|
2022-09-05 23:09:01 +00:00
|
|
|
probChanges: { day: change },
|
|
|
|
} = contract
|
2022-09-19 21:24:36 +00:00
|
|
|
|
|
|
|
const color = change >= 0 ? 'text-green-500' : 'text-red-500'
|
|
|
|
|
2022-09-17 22:58:08 +00:00
|
|
|
return (
|
|
|
|
<Col className={clsx('flex flex-col items-end', className)}>
|
2022-09-19 21:24:36 +00:00
|
|
|
<div className="mb-0.5 mr-0.5 text-2xl">
|
2022-09-17 22:58:08 +00:00
|
|
|
{formatPercent(Math.round(100 * prob) / 100)}
|
2022-09-19 21:24:36 +00:00
|
|
|
</div>
|
|
|
|
<div className={clsx('text-base', color)}>
|
|
|
|
{(change > 0 ? '+' : '') + (change * 100).toFixed(0) + '%'}
|
|
|
|
</div>
|
2022-09-17 22:58:08 +00:00
|
|
|
</Col>
|
|
|
|
)
|
2022-09-05 23:09:01 +00:00
|
|
|
}
|