Memo-ize graph components for better performance

This commit is contained in:
James Grugett 2022-04-06 11:45:34 -05:00
parent e3800612ea
commit 849248376b
2 changed files with 7 additions and 5 deletions

View File

@ -2,6 +2,7 @@ import { DatumValue } from '@nivo/core'
import { ResponsiveLine } from '@nivo/line'
import dayjs from 'dayjs'
import _ from 'lodash'
import { memo } from 'react'
import { Bet } from '../../../common/bet'
import { DPM, FreeResponse, FullContract } from '../../../common/contract'
@ -11,7 +12,7 @@ import { useWindowSize } from '../../hooks/use-window-size'
const NUM_LINES = 6
export function AnswersGraph(props: {
export const AnswersGraph = memo(function AnswersGraph(props: {
contract: FullContract<DPM, FreeResponse>
bets: Bet[]
height?: number
@ -118,7 +119,7 @@ export function AnswersGraph(props: {
/>
</div>
)
}
})
function formatPercent(y: DatumValue) {
return `${Math.round(+y.toString())}%`

View File

@ -1,13 +1,14 @@
import { DatumValue } from '@nivo/core'
import { ResponsiveLine } from '@nivo/line'
import dayjs from 'dayjs'
import { memo } from 'react'
import { Bet } from '../../common/bet'
import { getInitialProbability } from '../../common/calculate'
import { Binary, CPMM, DPM, FullContract } from '../../common/contract'
import { useBetsWithoutAntes } from '../hooks/use-bets'
import { useWindowSize } from '../hooks/use-window-size'
export function ContractProbGraph(props: {
export const ContractProbGraph = memo(function ContractProbGraph(props: {
contract: FullContract<DPM | CPMM, Binary>
bets: Bet[]
height?: number
@ -64,7 +65,7 @@ export function ContractProbGraph(props: {
return (
<div
className="w-full overflow-hidden"
style={{ height: height ?? (!width || width >= 800 ? 400 : 250) }}
style={{ height: height ?? (!width || width >= 800 ? 350 : 250) }}
>
<ResponsiveLine
data={data}
@ -96,7 +97,7 @@ export function ContractProbGraph(props: {
/>
</div>
)
}
})
function formatPercent(y: DatumValue) {
return `${Math.round(+y.toString())}%`