Do all the chart sizing work in same batch
This commit is contained in:
parent
d4ae1658c2
commit
ed635a0487
|
@ -1,4 +1,4 @@
|
||||||
import React, { useRef } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
|
|
||||||
import { tradingAllowed } from 'web/lib/firebase/contracts'
|
import { tradingAllowed } from 'web/lib/firebase/contracts'
|
||||||
import { Col } from '../layout/col'
|
import { Col } from '../layout/col'
|
||||||
|
@ -24,8 +24,6 @@ import {
|
||||||
BinaryContract,
|
BinaryContract,
|
||||||
} from 'common/contract'
|
} from 'common/contract'
|
||||||
import { ContractDetails } from './contract-details'
|
import { ContractDetails } from './contract-details'
|
||||||
import { useElementWidth } from 'web/hooks/use-element-width'
|
|
||||||
import { useIsMobile } from 'web/hooks/use-is-mobile'
|
|
||||||
|
|
||||||
const OverviewQuestion = (props: { text: string }) => (
|
const OverviewQuestion = (props: { text: string }) => (
|
||||||
<Linkify className="text-lg text-indigo-700 sm:text-2xl" text={props.text} />
|
<Linkify className="text-lg text-indigo-700 sm:text-2xl" text={props.text} />
|
||||||
|
@ -53,16 +51,27 @@ const SizedContractChart = (props: {
|
||||||
}) => {
|
}) => {
|
||||||
const { contract, bets, fullHeight, mobileHeight } = props
|
const { contract, bets, fullHeight, mobileHeight } = props
|
||||||
const containerRef = useRef<HTMLDivElement>(null)
|
const containerRef = useRef<HTMLDivElement>(null)
|
||||||
const isMobile = useIsMobile(800)
|
const [chartWidth, setChartWidth] = useState<number>()
|
||||||
const width = useElementWidth(containerRef)
|
const [chartHeight, setChartHeight] = useState<number>()
|
||||||
|
useEffect(() => {
|
||||||
|
const handleResize = () => {
|
||||||
|
setChartHeight(window.innerWidth < 800 ? mobileHeight : fullHeight)
|
||||||
|
setChartWidth(containerRef.current?.clientWidth)
|
||||||
|
}
|
||||||
|
handleResize()
|
||||||
|
window.addEventListener('resize', handleResize)
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('resize', handleResize)
|
||||||
|
}
|
||||||
|
}, [fullHeight, mobileHeight])
|
||||||
return (
|
return (
|
||||||
<div ref={containerRef}>
|
<div ref={containerRef}>
|
||||||
{width != null && (
|
{chartWidth != null && chartHeight != null && (
|
||||||
<ContractChart
|
<ContractChart
|
||||||
contract={contract}
|
contract={contract}
|
||||||
bets={bets}
|
bets={bets}
|
||||||
width={width}
|
width={chartWidth}
|
||||||
height={isMobile ? mobileHeight : fullHeight}
|
height={chartHeight}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
import { RefObject, useState, useEffect } from 'react'
|
|
||||||
|
|
||||||
// todo: consider consolidation with use-measure-size
|
|
||||||
export const useElementWidth = <T extends Element>(ref: RefObject<T>) => {
|
|
||||||
const [width, setWidth] = useState<number>()
|
|
||||||
useEffect(() => {
|
|
||||||
const handleResize = () => {
|
|
||||||
setWidth(ref.current?.clientWidth)
|
|
||||||
}
|
|
||||||
handleResize()
|
|
||||||
window.addEventListener('resize', handleResize)
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('resize', handleResize)
|
|
||||||
}
|
|
||||||
}, [ref])
|
|
||||||
return width
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user