From ed635a0487075c051d9eefe09bfdebf8d3bcfa92 Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Fri, 30 Sep 2022 16:53:28 -0700 Subject: [PATCH] Do all the chart sizing work in same batch --- web/components/contract/contract-overview.tsx | 25 +++++++++++++------ web/hooks/use-element-width.tsx | 17 ------------- 2 files changed, 17 insertions(+), 25 deletions(-) delete mode 100644 web/hooks/use-element-width.tsx diff --git a/web/components/contract/contract-overview.tsx b/web/components/contract/contract-overview.tsx index d287c760..3e5f22b2 100644 --- a/web/components/contract/contract-overview.tsx +++ b/web/components/contract/contract-overview.tsx @@ -1,4 +1,4 @@ -import React, { useRef } from 'react' +import React, { useEffect, useRef, useState } from 'react' import { tradingAllowed } from 'web/lib/firebase/contracts' import { Col } from '../layout/col' @@ -24,8 +24,6 @@ import { BinaryContract, } from 'common/contract' 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 }) => ( @@ -53,16 +51,27 @@ const SizedContractChart = (props: { }) => { const { contract, bets, fullHeight, mobileHeight } = props const containerRef = useRef(null) - const isMobile = useIsMobile(800) - const width = useElementWidth(containerRef) + const [chartWidth, setChartWidth] = useState() + const [chartHeight, setChartHeight] = useState() + 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 (
- {width != null && ( + {chartWidth != null && chartHeight != null && ( )}
diff --git a/web/hooks/use-element-width.tsx b/web/hooks/use-element-width.tsx deleted file mode 100644 index 1c373839..00000000 --- a/web/hooks/use-element-width.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import { RefObject, useState, useEffect } from 'react' - -// todo: consider consolidation with use-measure-size -export const useElementWidth = (ref: RefObject) => { - const [width, setWidth] = useState() - useEffect(() => { - const handleResize = () => { - setWidth(ref.current?.clientWidth) - } - handleResize() - window.addEventListener('resize', handleResize) - return () => { - window.removeEventListener('resize', handleResize) - } - }, [ref]) - return width -}