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'
|
|
|
|
import { useProbChanges } from 'web/hooks/use-prob-changes'
|
2022-09-08 06:36:34 +00:00
|
|
|
import { linkClass, SiteLink } from '../site-link'
|
2022-09-07 06:18:11 +00:00
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { Row } from '../layout/row'
|
2022-09-08 06:36:34 +00:00
|
|
|
import { useState } from 'react'
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
export function ProbChangeTable(props: { userId: string | undefined }) {
|
|
|
|
const { userId } = props
|
|
|
|
|
|
|
|
const changes = useProbChanges(userId ?? '')
|
2022-09-08 06:36:34 +00:00
|
|
|
const [expanded, setExpanded] = useState(false)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
if (!changes) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-09-08 06:36:34 +00:00
|
|
|
const count = expanded ? 16 : 4
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-08 06:36:34 +00:00
|
|
|
const { positiveChanges, negativeChanges } = changes
|
|
|
|
const filteredPositiveChanges = positiveChanges.slice(0, count / 2)
|
|
|
|
const filteredNegativeChanges = negativeChanges.slice(0, count / 2)
|
|
|
|
const filteredChanges = [
|
|
|
|
...filteredPositiveChanges,
|
|
|
|
...filteredNegativeChanges,
|
|
|
|
]
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-08 06:36:34 +00:00
|
|
|
<Col>
|
|
|
|
<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">
|
|
|
|
{filteredChanges.slice(0, count / 2).map((contract) => (
|
|
|
|
<Row className="items-center hover:bg-gray-100">
|
|
|
|
<ProbChange
|
|
|
|
className="p-4 text-right text-xl"
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
<SiteLink
|
|
|
|
className="p-4 pl-2 font-semibold text-indigo-700"
|
|
|
|
href={contractPath(contract)}
|
|
|
|
>
|
|
|
|
<span className="line-clamp-2">{contract.question}</span>
|
|
|
|
</SiteLink>
|
|
|
|
</Row>
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
<Col className="flex-1 divide-y">
|
|
|
|
{filteredChanges.slice(count / 2).map((contract) => (
|
|
|
|
<Row className="items-center hover:bg-gray-100">
|
|
|
|
<ProbChange
|
|
|
|
className="p-4 text-right text-xl"
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
<SiteLink
|
|
|
|
className="p-4 pl-2 font-semibold text-indigo-700"
|
|
|
|
href={contractPath(contract)}
|
|
|
|
>
|
|
|
|
<span className="line-clamp-2">{contract.question}</span>
|
|
|
|
</SiteLink>
|
|
|
|
</Row>
|
|
|
|
))}
|
|
|
|
</Col>
|
2022-09-07 06:18:11 +00:00
|
|
|
</Col>
|
2022-09-08 06:36:34 +00:00
|
|
|
<div
|
|
|
|
className={clsx(linkClass, 'cursor-pointer self-end')}
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
>
|
|
|
|
{expanded ? 'Show less' : 'Show more'}
|
|
|
|
</div>
|
|
|
|
</Col>
|
2022-09-05 23:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ProbChange(props: {
|
|
|
|
contract: CPMMContract
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, className } = props
|
|
|
|
const {
|
|
|
|
probChanges: { day: change },
|
|
|
|
} = contract
|
|
|
|
|
|
|
|
const color =
|
|
|
|
change > 0
|
2022-09-08 06:36:34 +00:00
|
|
|
? 'text-green-500'
|
2022-09-05 23:09:01 +00:00
|
|
|
: change < 0
|
2022-09-08 06:36:34 +00:00
|
|
|
? 'text-red-500'
|
2022-09-07 06:18:11 +00:00
|
|
|
: 'text-gray-600'
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
const str =
|
|
|
|
change === 0
|
|
|
|
? '+0%'
|
|
|
|
: `${change > 0 ? '+' : '-'}${formatPercent(Math.abs(change))}`
|
|
|
|
return <div className={clsx(className, color)}>{str}</div>
|
|
|
|
}
|