2022-09-28 03:24:42 +00:00
|
|
|
import { useMemo, useRef } from 'react'
|
2022-09-28 04:18:22 +00:00
|
|
|
import { last, sortBy } from 'lodash'
|
2022-09-28 08:00:39 +00:00
|
|
|
import { scaleTime, scaleLinear } from 'd3-scale'
|
2022-09-28 03:24:42 +00:00
|
|
|
|
|
|
|
import { Bet } from 'common/bet'
|
2022-09-29 01:03:30 +00:00
|
|
|
import { getProbability, getInitialProbability } from 'common/calculate'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { BinaryContract } from 'common/contract'
|
2022-09-29 06:27:42 +00:00
|
|
|
import { DAY_MS } from 'common/util/time'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { useIsMobile } from 'web/hooks/use-is-mobile'
|
2022-09-28 04:18:22 +00:00
|
|
|
import {
|
|
|
|
MARGIN_X,
|
|
|
|
MARGIN_Y,
|
|
|
|
getDateRange,
|
|
|
|
getRightmostVisibleDate,
|
2022-09-29 04:14:34 +00:00
|
|
|
formatDateInRange,
|
|
|
|
formatPct,
|
2022-09-28 04:18:22 +00:00
|
|
|
} from '../helpers'
|
2022-09-29 04:14:34 +00:00
|
|
|
import {
|
|
|
|
SingleValueHistoryTooltipProps,
|
|
|
|
SingleValueHistoryChart,
|
|
|
|
} from '../generic-charts'
|
2022-09-28 03:24:42 +00:00
|
|
|
import { useElementWidth } from 'web/hooks/use-element-width'
|
2022-09-29 04:14:34 +00:00
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Avatar } from 'web/components/avatar'
|
2022-09-28 03:24:42 +00:00
|
|
|
|
|
|
|
const getBetPoints = (bets: Bet[]) => {
|
2022-09-29 04:14:34 +00:00
|
|
|
return sortBy(bets, (b) => b.createdTime).map((b) => ({
|
|
|
|
x: new Date(b.createdTime),
|
|
|
|
y: b.probAfter,
|
|
|
|
datum: b,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
const BinaryChartTooltip = (props: SingleValueHistoryTooltipProps<Bet>) => {
|
2022-09-29 04:43:04 +00:00
|
|
|
const { p, xScale } = props
|
|
|
|
const { x, y, datum } = p
|
2022-09-29 04:14:34 +00:00
|
|
|
const [start, end] = xScale.domain()
|
|
|
|
return (
|
|
|
|
<Row className="items-center gap-2 text-sm">
|
|
|
|
{datum && <Avatar size="xs" avatarUrl={datum.userAvatarUrl} />}
|
|
|
|
<strong>{formatPct(y)}</strong>
|
|
|
|
<span>{formatDateInRange(x, start, end)}</span>
|
|
|
|
</Row>
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const BinaryContractChart = (props: {
|
|
|
|
contract: BinaryContract
|
|
|
|
bets: Bet[]
|
|
|
|
height?: number
|
|
|
|
}) => {
|
|
|
|
const { contract, bets } = props
|
2022-09-29 01:03:30 +00:00
|
|
|
const [startDate, endDate] = getDateRange(contract)
|
|
|
|
const startP = getInitialProbability(contract)
|
|
|
|
const endP = getProbability(contract)
|
2022-09-28 03:24:42 +00:00
|
|
|
const betPoints = useMemo(() => getBetPoints(bets), [bets])
|
|
|
|
const data = useMemo(
|
|
|
|
() => [
|
2022-09-29 04:14:34 +00:00
|
|
|
{ x: startDate, y: startP },
|
2022-09-28 03:24:42 +00:00
|
|
|
...betPoints,
|
2022-09-29 06:27:42 +00:00
|
|
|
{ x: endDate ?? new Date(Date.now() + DAY_MS), y: endP },
|
2022-09-28 03:24:42 +00:00
|
|
|
],
|
2022-09-29 01:03:30 +00:00
|
|
|
[startDate, startP, endDate, endP, betPoints]
|
2022-09-28 03:24:42 +00:00
|
|
|
)
|
2022-09-29 01:03:30 +00:00
|
|
|
|
2022-09-28 04:18:22 +00:00
|
|
|
const rightmostDate = getRightmostVisibleDate(
|
2022-09-29 01:03:30 +00:00
|
|
|
endDate,
|
2022-09-29 04:14:34 +00:00
|
|
|
last(betPoints)?.x,
|
2022-09-28 04:18:22 +00:00
|
|
|
new Date(Date.now())
|
|
|
|
)
|
2022-09-29 01:03:30 +00:00
|
|
|
const visibleRange = [startDate, rightmostDate]
|
2022-09-28 03:24:42 +00:00
|
|
|
const isMobile = useIsMobile(800)
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null)
|
|
|
|
const width = useElementWidth(containerRef) ?? 0
|
|
|
|
const height = props.height ?? (isMobile ? 250 : 350)
|
2022-09-29 06:27:42 +00:00
|
|
|
const xScale = scaleTime(visibleRange, [0, width - MARGIN_X])
|
2022-09-28 03:24:42 +00:00
|
|
|
const yScale = scaleLinear([0, 1], [height - MARGIN_Y, 0])
|
2022-09-29 04:14:34 +00:00
|
|
|
|
2022-09-28 03:24:42 +00:00
|
|
|
return (
|
|
|
|
<div ref={containerRef}>
|
2022-09-28 21:20:28 +00:00
|
|
|
{width > 0 && (
|
2022-09-28 03:24:42 +00:00
|
|
|
<SingleValueHistoryChart
|
|
|
|
w={width}
|
|
|
|
h={height}
|
|
|
|
xScale={xScale}
|
|
|
|
yScale={yScale}
|
|
|
|
data={data}
|
|
|
|
color="#11b981"
|
2022-09-29 04:14:34 +00:00
|
|
|
Tooltip={BinaryChartTooltip}
|
2022-09-28 03:24:42 +00:00
|
|
|
pct
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|