Fix some rendering issues on contract page (#933)

* Memoize calculating sale amount on your bets list

* Don't re-render more than necessary with `useIsMobile` hook

* Use `useIsMobile` hook in `AmountInput`
This commit is contained in:
Marshall Polaris 2022-09-25 16:43:53 -07:00 committed by GitHub
parent c1c3a360fd
commit be2c60d3f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 16 deletions

View File

@ -5,7 +5,7 @@ import { formatMoney } from 'common/util/format'
import { Col } from './layout/col' import { Col } from './layout/col'
import { SiteLink } from './site-link' import { SiteLink } from './site-link'
import { ENV_CONFIG } from 'common/envs/constants' import { ENV_CONFIG } from 'common/envs/constants'
import { useWindowSize } from 'web/hooks/use-window-size' import { useIsMobile } from 'web/hooks/use-is-mobile'
export function AmountInput(props: { export function AmountInput(props: {
amount: number | undefined amount: number | undefined
@ -34,8 +34,7 @@ export function AmountInput(props: {
const isInvalid = !str || isNaN(amount) const isInvalid = !str || isNaN(amount)
onChange(isInvalid ? undefined : amount) onChange(isInvalid ? undefined : amount)
} }
const { width } = useWindowSize() const isMobile = useIsMobile(768)
const isMobile = (width ?? 0) < 768
return ( return (
<Col className={className}> <Col className={className}>
<label className="input-group mb-4"> <label className="input-group mb-4">

View File

@ -610,18 +610,24 @@ function BetRow(props: {
const isNumeric = outcomeType === 'NUMERIC' const isNumeric = outcomeType === 'NUMERIC'
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC' const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
const saleAmount = saleBet?.sale?.amount // calculateSaleAmount is very slow right now so that's why we memoized this
const payout = useMemo(() => {
const saleBetAmount = saleBet?.sale?.amount
if (saleBetAmount) {
return saleBetAmount
} else if (contract.isResolved) {
return resolvedPayout(contract, bet)
} else {
return calculateSaleAmount(contract, bet, unfilledBets)
}
}, [contract, bet, saleBet, unfilledBets])
const saleDisplay = isAnte ? ( const saleDisplay = isAnte ? (
'ANTE' 'ANTE'
) : saleAmount !== undefined ? ( ) : saleBet ? (
<>{formatMoney(saleAmount)} (sold)</> <>{formatMoney(payout)} (sold)</>
) : ( ) : (
formatMoney( formatMoney(payout)
isResolved
? resolvedPayout(contract, bet)
: calculateSaleAmount(contract, bet, unfilledBets)
)
) )
const payoutIfChosenDisplay = const payoutIfChosenDisplay =

View File

@ -1,7 +1,13 @@
import { useWindowSize } from 'web/hooks/use-window-size' import { useEffect, useState } from 'react'
// matches talwind sm breakpoint export function useIsMobile(threshold?: number) {
export function useIsMobile() { const [isMobile, setIsMobile] = useState<boolean>()
const { width } = useWindowSize() useEffect(() => {
return (width ?? 0) < 640 // 640 matches tailwind sm breakpoint
const onResize = () => setIsMobile(window.innerWidth < (threshold ?? 640))
onResize()
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [threshold])
return isMobile
} }