From 4c29f563290e849010206b34f85c106486de5aa2 Mon Sep 17 00:00:00 2001 From: Szymon Szlachtowicz <38212223+Szymx95@users.noreply.github.com> Date: Thu, 16 Sep 2021 17:46:31 +0200 Subject: [PATCH] Improve components performance (#76) --- .../src/components/Proposal.tsx | 65 +++++++++++-------- .../src/components/ProposalHeader.tsx | 2 +- .../src/components/VotingEmpty.tsx | 2 +- .../src/hooks/useVotingRooms.ts | 3 +- .../src/hooks/useMobileVersion.ts | 25 +++++-- .../react-components/src/hooks/useRefSize.ts | 17 +++-- 6 files changed, 69 insertions(+), 45 deletions(-) diff --git a/packages/proposal-components/src/components/Proposal.tsx b/packages/proposal-components/src/components/Proposal.tsx index ddf166c..7284f03 100644 --- a/packages/proposal-components/src/components/Proposal.tsx +++ b/packages/proposal-components/src/components/Proposal.tsx @@ -10,24 +10,23 @@ import { VotingRoom } from '@status-waku-voting/core/dist/esm/src/types/PollType import { useTokenBalance } from '@status-waku-voting/react-components' import { NewVoteModal } from './newVoteModal/NewVoteModal' import { useEthers } from '@usedapp/core' -import { Modal, Networks, useMobileVersion } from '@status-waku-voting/react-components' +import { Modal, Networks, useMobileVersion, Theme } from '@status-waku-voting/react-components' import { useHistory } from 'react-router' import { useVotingRooms } from '@status-waku-voting/proposal-hooks' -type ProposalProps = { +type ProposalListHeaderProps = { + votes: VotingRoom[] + theme: Theme wakuVoting: WakuVoting + tokenBalance: number account: string | null | undefined } -export function Proposal({ wakuVoting, account }: ProposalProps) { - const votes = useVotingRooms(wakuVoting) - const tokenBalance = useTokenBalance(account, wakuVoting) +function ProposalListHeader({ votes, theme, wakuVoting, tokenBalance, account }: ProposalListHeaderProps) { const [showNewVoteModal, setShowNewVoteModal] = useState(false) const [showConnectionModal, setShowConnectionModal] = useState(false) - const { activateBrowserWallet } = useEthers() const history = useHistory() - const ref = useRef(null) const mobileVersion = useMobileVersion(ref, 600) @@ -42,37 +41,51 @@ export function Proposal({ wakuVoting, account }: ProposalProps) { }, []) return ( - +
{showConnectionModal && ( - + )} - {votes && votes?.length === 0 ? ( - + {votes?.length === 0 ? ( + ) : ( - - - - + )} +
+ ) +} + +type ProposalProps = { + wakuVoting: WakuVoting + account: string | null | undefined +} + +export function Proposal({ wakuVoting, account }: ProposalProps) { + const votes = useVotingRooms(wakuVoting) + const tokenBalance = useTokenBalance(account, wakuVoting) + + return ( + + + + {votes.length > 0 && ( + + )} + diff --git a/packages/proposal-components/src/components/ProposalHeader.tsx b/packages/proposal-components/src/components/ProposalHeader.tsx index 07fc22a..ba23a82 100644 --- a/packages/proposal-components/src/components/ProposalHeader.tsx +++ b/packages/proposal-components/src/components/ProposalHeader.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react' +import React, { useEffect, useState } from 'react' import styled from 'styled-components' import { useEthers } from '@usedapp/core' import { Modal, Networks, CreateButton } from '@status-waku-voting/react-components' diff --git a/packages/proposal-components/src/components/VotingEmpty.tsx b/packages/proposal-components/src/components/VotingEmpty.tsx index ec4f601..e86338f 100644 --- a/packages/proposal-components/src/components/VotingEmpty.tsx +++ b/packages/proposal-components/src/components/VotingEmpty.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useEffect } from 'react' import styled from 'styled-components' import { CreateButton, Theme } from '@status-waku-voting/react-components' diff --git a/packages/proposal-hooks/src/hooks/useVotingRooms.ts b/packages/proposal-hooks/src/hooks/useVotingRooms.ts index 6a9ef96..867ba68 100644 --- a/packages/proposal-hooks/src/hooks/useVotingRooms.ts +++ b/packages/proposal-hooks/src/hooks/useVotingRooms.ts @@ -15,11 +15,12 @@ export function useVotingRooms(wakuVoting: WakuVoting) { hash.current = newHash } }, 10000) + setVotes([]) wakuVoting.getVotingRooms().then((e) => { setVotes(e) hash.current = id(e.map((votingRoom) => votingRoom.id.toString()).join('')) }) return () => clearInterval(interval) - }, []) + }, [wakuVoting]) return votes } diff --git a/packages/react-components/src/hooks/useMobileVersion.ts b/packages/react-components/src/hooks/useMobileVersion.ts index 72e32a9..52b1179 100644 --- a/packages/react-components/src/hooks/useMobileVersion.ts +++ b/packages/react-components/src/hooks/useMobileVersion.ts @@ -1,16 +1,27 @@ import React, { useEffect, useState } from 'react' -import { useRefSize } from './useRefSize' export function useMobileVersion(myRef: React.RefObject, sizeThreshold: number) { const [mobileVersion, setMobileVersion] = useState(false) - const { width } = useRefSize(myRef) + useEffect(() => { - if (width < sizeThreshold && width > 0) { - setMobileVersion(true) - } else { - setMobileVersion(false) + const checkDimensions = () => { + const width = window.innerWidth + if (width && width < sizeThreshold && width > 0) { + if (mobileVersion === false) { + setMobileVersion(true) + } + } else { + if (mobileVersion === true) { + setMobileVersion(false) + } + } } - }, [width]) + checkDimensions() + window.addEventListener('resize', checkDimensions) + return () => { + window.removeEventListener('resize', checkDimensions) + } + }, [myRef, mobileVersion]) return mobileVersion } diff --git a/packages/react-components/src/hooks/useRefSize.ts b/packages/react-components/src/hooks/useRefSize.ts index be656d4..ac647ff 100644 --- a/packages/react-components/src/hooks/useRefSize.ts +++ b/packages/react-components/src/hooks/useRefSize.ts @@ -4,16 +4,15 @@ export function useRefSize(myRef: React.RefObject) { const [width, setWidth] = useState(0) const [height, setHeight] = useState(0) - const setDimensions = () => { - if (myRef?.current?.offsetWidth) { - setWidth(myRef?.current?.offsetWidth) - } - if (myRef?.current?.offsetHeight) { - setHeight(myRef?.current?.offsetHeight) - } - } - useEffect(() => { + const setDimensions = () => { + if (myRef?.current?.offsetWidth) { + setWidth(myRef?.current?.offsetWidth) + } + if (myRef?.current?.offsetHeight) { + setHeight(myRef?.current?.offsetHeight) + } + } setDimensions() window.addEventListener('resize', setDimensions) return () => {