2022-05-02 14:35:46 +00:00
|
|
|
import clsx from 'clsx'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Answer } from 'common/answer'
|
|
|
|
import { getProbability } from 'common/calculate'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { getValueFromBucket } from 'common/calculate-dpm'
|
2022-04-18 23:02:40 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FreeResponseContract,
|
|
|
|
FullContract,
|
2022-05-19 17:42:03 +00:00
|
|
|
NumericContract,
|
2022-05-25 18:27:26 +00:00
|
|
|
resolution,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'common/contract'
|
|
|
|
import { formatPercent } from 'common/util/format'
|
2022-04-29 21:11:04 +00:00
|
|
|
import { ClientRender } from './client-render'
|
2022-04-18 23:02:40 +00:00
|
|
|
|
2022-01-03 18:56:02 +00:00
|
|
|
export function OutcomeLabel(props: {
|
2022-04-18 23:02:40 +00:00
|
|
|
contract: Contract
|
2022-05-25 18:27:26 +00:00
|
|
|
outcome: resolution | string
|
2022-04-18 23:02:40 +00:00
|
|
|
truncate: 'short' | 'long' | 'none'
|
2022-05-19 17:42:03 +00:00
|
|
|
value?: number
|
2022-04-18 23:02:40 +00:00
|
|
|
}) {
|
2022-05-19 17:42:03 +00:00
|
|
|
const { outcome, contract, truncate, value } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
|
|
|
|
if (contract.outcomeType === 'BINARY')
|
|
|
|
return <BinaryOutcomeLabel outcome={outcome as any} />
|
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
if (contract.outcomeType === 'NUMERIC')
|
|
|
|
return (
|
|
|
|
<span className="text-blue-500">
|
|
|
|
{value ?? getValueFromBucket(outcome, contract as NumericContract)}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
return (
|
|
|
|
<FreeResponseOutcomeLabel
|
|
|
|
contract={contract as FullContract<DPM, FreeResponse>}
|
|
|
|
resolution={outcome}
|
|
|
|
truncate={truncate}
|
2022-04-26 13:24:57 +00:00
|
|
|
answerClassName={'font-bold text-base-400'}
|
2022-04-18 23:02:40 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BinaryOutcomeLabel(props: {
|
2022-05-25 18:27:26 +00:00
|
|
|
outcome: resolution
|
2022-01-03 18:56:02 +00:00
|
|
|
}) {
|
|
|
|
const { outcome } = props
|
|
|
|
|
|
|
|
if (outcome === 'YES') return <YesLabel />
|
|
|
|
if (outcome === 'NO') return <NoLabel />
|
2022-01-30 21:51:30 +00:00
|
|
|
if (outcome === 'MKT') return <ProbLabel />
|
2022-04-18 23:02:40 +00:00
|
|
|
return <CancelLabel />
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BinaryContractOutcomeLabel(props: {
|
|
|
|
contract: FullContract<DPM | CPMM, Binary>
|
2022-05-25 18:27:26 +00:00
|
|
|
resolution: resolution
|
2022-04-18 23:02:40 +00:00
|
|
|
}) {
|
|
|
|
const { contract, resolution } = props
|
|
|
|
|
|
|
|
if (resolution === 'MKT') {
|
|
|
|
const prob = contract.resolutionProbability ?? getProbability(contract)
|
|
|
|
return <ProbPercentLabel prob={prob} />
|
|
|
|
}
|
|
|
|
|
|
|
|
return <BinaryOutcomeLabel outcome={resolution} />
|
|
|
|
}
|
|
|
|
|
|
|
|
export function FreeResponseOutcomeLabel(props: {
|
|
|
|
contract: FreeResponseContract
|
|
|
|
resolution: string | 'CANCEL' | 'MKT'
|
|
|
|
truncate: 'short' | 'long' | 'none'
|
2022-04-20 03:31:41 +00:00
|
|
|
answerClassName?: string
|
2022-04-18 23:02:40 +00:00
|
|
|
}) {
|
2022-04-20 03:31:41 +00:00
|
|
|
const { contract, resolution, truncate, answerClassName } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
|
|
|
|
if (resolution === 'CANCEL') return <CancelLabel />
|
|
|
|
if (resolution === 'MKT') return <MultiLabel />
|
|
|
|
|
|
|
|
const { answers } = contract
|
|
|
|
const chosen = answers?.find((answer) => answer.id === resolution)
|
|
|
|
if (!chosen) return <AnswerNumberLabel number={resolution} />
|
2022-04-20 03:31:41 +00:00
|
|
|
return (
|
2022-04-29 21:11:04 +00:00
|
|
|
<FreeResponseAnswerToolTip text={chosen.text}>
|
2022-05-24 14:24:01 +00:00
|
|
|
<AnswerLabel
|
|
|
|
answer={chosen}
|
|
|
|
truncate={truncate}
|
|
|
|
className={answerClassName}
|
|
|
|
/>
|
2022-04-29 21:11:04 +00:00
|
|
|
</FreeResponseAnswerToolTip>
|
2022-04-20 03:31:41 +00:00
|
|
|
)
|
2022-01-03 18:56:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 23:15:22 +00:00
|
|
|
export const OUTCOME_TO_COLOR = {
|
|
|
|
YES: 'primary',
|
|
|
|
NO: 'red-400',
|
|
|
|
CANCEL: 'yellow-400',
|
|
|
|
MKT: 'blue-400',
|
|
|
|
}
|
|
|
|
|
2022-01-03 18:56:02 +00:00
|
|
|
export function YesLabel() {
|
|
|
|
return <span className="text-primary">YES</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function NoLabel() {
|
|
|
|
return <span className="text-red-400">NO</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
export function CancelLabel() {
|
|
|
|
return <span className="text-yellow-400">N/A</span>
|
|
|
|
}
|
|
|
|
|
2022-01-30 21:51:30 +00:00
|
|
|
export function ProbLabel() {
|
|
|
|
return <span className="text-blue-400">PROB</span>
|
|
|
|
}
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
export function MultiLabel() {
|
2022-05-24 06:44:16 +00:00
|
|
|
return <span className="text-blue-400">MANY</span>
|
2022-04-18 23:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function ProbPercentLabel(props: { prob: number }) {
|
|
|
|
const { prob } = props
|
|
|
|
return <span className="text-blue-400">{formatPercent(prob)}</span>
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export function AnswerNumberLabel(props: { number: string }) {
|
|
|
|
return <span className="text-primary">#{props.number}</span>
|
|
|
|
}
|
2022-04-18 23:02:40 +00:00
|
|
|
|
|
|
|
export function AnswerLabel(props: {
|
|
|
|
answer: Answer
|
|
|
|
truncate: 'short' | 'long' | 'none'
|
2022-04-20 03:31:41 +00:00
|
|
|
className?: string
|
2022-04-18 23:02:40 +00:00
|
|
|
}) {
|
2022-04-20 03:31:41 +00:00
|
|
|
const { answer, truncate, className } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
const { text } = answer
|
|
|
|
|
|
|
|
let truncated = text
|
|
|
|
if (truncate === 'short' && text.length > 20) {
|
|
|
|
truncated = text.slice(0, 10) + '...' + text.slice(-10)
|
|
|
|
} else if (truncate === 'long' && text.length > 75) {
|
|
|
|
truncated = text.slice(0, 75) + '...'
|
|
|
|
}
|
|
|
|
|
2022-05-02 14:35:46 +00:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
style={{ wordBreak: 'break-word' }}
|
|
|
|
className={clsx('whitespace-pre-line break-words', className)}
|
|
|
|
>
|
|
|
|
{truncated}
|
|
|
|
</span>
|
|
|
|
)
|
2022-04-18 23:02:40 +00:00
|
|
|
}
|
2022-04-29 21:11:04 +00:00
|
|
|
|
|
|
|
function FreeResponseAnswerToolTip(props: {
|
|
|
|
text: string
|
|
|
|
children?: React.ReactNode
|
|
|
|
}) {
|
|
|
|
const { text } = props
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<ClientRender>
|
|
|
|
<span
|
|
|
|
className="tooltip hidden cursor-default sm:inline-block"
|
|
|
|
data-tip={text}
|
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</span>
|
|
|
|
</ClientRender>
|
|
|
|
<span className="whitespace-nowrap sm:hidden">{props.children}</span>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|