Use numbers instead of dates to calculate visible range

This commit is contained in:
Marshall Polaris 2022-09-29 21:29:05 -07:00
parent de7cf62918
commit 5ade0bbd3e
4 changed files with 23 additions and 23 deletions

View File

@ -49,24 +49,24 @@ export const BinaryContractChart = (props: {
onMouseOver?: (p: HistoryPoint<Bet> | undefined) => void
}) => {
const { contract, bets, onMouseOver } = props
const [startDate, endDate] = getDateRange(contract)
const [start, end] = getDateRange(contract)
const startP = getInitialProbability(contract)
const endP = getProbability(contract)
const betPoints = useMemo(() => getBetPoints(bets), [bets])
const data = useMemo(() => {
return [
{ x: new Date(startDate), y: startP },
{ x: new Date(start), y: startP },
...betPoints,
{ x: new Date(endDate ?? Date.now() + DAY_MS), y: endP },
{ x: new Date(end ?? Date.now() + DAY_MS), y: endP },
]
}, [startDate, startP, endDate, endP, betPoints])
}, [start, startP, end, endP, betPoints])
const rightmostDate = getRightmostVisibleDate(
endDate ? new Date(endDate) : null,
last(betPoints)?.x,
new Date(Date.now())
end,
last(betPoints)?.x?.getTime(),
Date.now()
)
const visibleRange = [startDate, rightmostDate]
const visibleRange = [start, rightmostDate]
const isMobile = useIsMobile(800)
const containerRef = useRef<HTMLDivElement>(null)
const width = useElementWidth(containerRef) ?? 0

View File

@ -146,9 +146,9 @@ export const ChoiceContractChart = (props: {
[answers, contract, betPoints, start, end]
)
const rightmostDate = getRightmostVisibleDate(
end ? new Date(end) : null,
last(betPoints)?.x,
new Date(Date.now())
end,
last(betPoints)?.x?.getTime(),
Date.now()
)
const visibleRange = [start, rightmostDate]
const isMobile = useIsMobile(800)

View File

@ -62,7 +62,7 @@ export const PseudoNumericContractChart = (props: {
}) => {
const { contract, bets, onMouseOver } = props
const { min, max, isLogScale } = contract
const [startDate, endDate] = getDateRange(contract)
const [start, end] = getDateRange(contract)
const scaleP = useMemo(
() => getScaleP(min, max, isLogScale),
[min, max, isLogScale]
@ -72,18 +72,18 @@ export const PseudoNumericContractChart = (props: {
const betPoints = useMemo(() => getBetPoints(bets, scaleP), [bets, scaleP])
const data = useMemo(
() => [
{ x: new Date(startDate), y: startP },
{ x: new Date(start), y: startP },
...betPoints,
{ x: new Date(endDate ?? Date.now() + DAY_MS), y: endP },
{ x: new Date(end ?? Date.now() + DAY_MS), y: endP },
],
[betPoints, startDate, startP, endDate, endP]
[betPoints, start, startP, end, endP]
)
const rightmostDate = getRightmostVisibleDate(
endDate ? new Date(endDate) : null,
last(betPoints)?.x,
new Date(Date.now())
end,
last(betPoints)?.x?.getTime(),
Date.now()
)
const visibleRange = [startDate, rightmostDate]
const visibleRange = [start, rightmostDate]
const isMobile = useIsMobile(800)
const containerRef = useRef<HTMLDivElement>(null)
const width = useElementWidth(containerRef) ?? 0

View File

@ -263,15 +263,15 @@ export const getDateRange = (contract: Contract) => {
}
export const getRightmostVisibleDate = (
contractEnd: Date | null | undefined,
lastActivity: Date | null | undefined,
now: Date
contractEnd: number | null | undefined,
lastActivity: number | null | undefined,
now: number
) => {
if (contractEnd != null) {
return contractEnd
} else if (lastActivity != null) {
// client-DB clock divergence may cause last activity to be later than now
return new Date(Math.max(lastActivity.getTime(), now.getTime()))
return Math.max(lastActivity, now)
} else {
return now
}