Fix date memoization in charts (#972)

* Memoize on numbers, not dates

* Use numbers instead of dates to calculate visible range
This commit is contained in:
Marshall Polaris 2022-09-29 21:35:20 -07:00 committed by GitHub
parent 5b5a919ed7
commit 715bae57e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 25 deletions

View File

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

View File

@ -136,10 +136,10 @@ export const ChoiceContractChart = (props: {
const betPoints = useMemo(() => getBetPoints(answers, bets), [answers, bets]) const betPoints = useMemo(() => getBetPoints(answers, bets), [answers, bets])
const data = useMemo( const data = useMemo(
() => [ () => [
{ x: start, y: answers.map((_) => 0) }, { x: new Date(start), y: answers.map((_) => 0) },
...betPoints, ...betPoints,
{ {
x: end ?? new Date(Date.now() + DAY_MS), x: new Date(end ?? Date.now() + DAY_MS),
y: answers.map((a) => getOutcomeProbability(contract, a.id)), y: answers.map((a) => getOutcomeProbability(contract, a.id)),
}, },
], ],
@ -147,8 +147,8 @@ export const ChoiceContractChart = (props: {
) )
const rightmostDate = getRightmostVisibleDate( const rightmostDate = getRightmostVisibleDate(
end, end,
last(betPoints)?.x, last(betPoints)?.x?.getTime(),
new Date(Date.now()) Date.now()
) )
const visibleRange = [start, rightmostDate] const visibleRange = [start, rightmostDate]
const isMobile = useIsMobile(800) const isMobile = useIsMobile(800)

View File

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

View File

@ -259,19 +259,19 @@ export const getDateRange = (contract: Contract) => {
const { createdTime, closeTime, resolutionTime } = contract const { createdTime, closeTime, resolutionTime } = contract
const isClosed = !!closeTime && Date.now() > closeTime const isClosed = !!closeTime && Date.now() > closeTime
const endDate = resolutionTime ?? (isClosed ? closeTime : null) const endDate = resolutionTime ?? (isClosed ? closeTime : null)
return [new Date(createdTime), endDate ? new Date(endDate) : null] as const return [createdTime, endDate ?? null] as const
} }
export const getRightmostVisibleDate = ( export const getRightmostVisibleDate = (
contractEnd: Date | null | undefined, contractEnd: number | null | undefined,
lastActivity: Date | null | undefined, lastActivity: number | null | undefined,
now: Date now: number
) => { ) => {
if (contractEnd != null) { if (contractEnd != null) {
return contractEnd return contractEnd
} else if (lastActivity != null) { } else if (lastActivity != null) {
// client-DB clock divergence may cause last activity to be later than now // 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 { } else {
return now return now
} }