import { DatumValue } from '@nivo/core' import { ResponsiveLine } from '@nivo/line' import dayjs from 'dayjs' import { useBets } from '../hooks/use-bets' import { useWindowSize } from '../hooks/use-window-size' import { Contract } from '../lib/firebase/contracts' export function ContractProbGraph(props: { contract: Contract }) { const { contract } = props const { id, seedAmounts } = contract let bets = useBets(id) if (bets === 'loading') bets = [] const seedProb = seedAmounts.YES ** 2 / (seedAmounts.YES ** 2 + seedAmounts.NO ** 2) const times = [ contract.createdTime, ...bets.map((bet) => bet.createdTime), ].map((time) => new Date(time)) const probs = [seedProb, ...bets.map((bet) => bet.probAfter)] // Add a fake datapoint in future so the line continues horizontally // to the right. times.push(dayjs().add(1, 'day').toDate()) probs.push(probs[probs.length - 1]) const points = probs.map((prob, i) => ({ x: times[i], y: prob * 100 })) const data = [{ id: 'Yes', data: points, color: '#11b981' }] const lessThanAWeek = times[times.length - 1].getTime() - times[0].getTime() < 1000 * 60 * 60 * 24 * 7 const yTickValues = [0, 25, 50, 75, 100] const { width } = useWindowSize() const numXTickValues = !width || width < 800 ? 4 : 8 const now = dayjs() const hoursAgo = now.subtract(8, 'hours') const startDate = dayjs(times[0]).isBefore(hoursAgo) ? times[0] : hoursAgo.toDate() return (