Improve components performance (#76)

This commit is contained in:
Szymon Szlachtowicz 2021-09-16 17:46:31 +02:00 committed by GitHub
parent 2b49035373
commit 4c29f56329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 69 additions and 45 deletions

View File

@ -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 { useTokenBalance } from '@status-waku-voting/react-components'
import { NewVoteModal } from './newVoteModal/NewVoteModal' import { NewVoteModal } from './newVoteModal/NewVoteModal'
import { useEthers } from '@usedapp/core' 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 { useHistory } from 'react-router'
import { useVotingRooms } from '@status-waku-voting/proposal-hooks' import { useVotingRooms } from '@status-waku-voting/proposal-hooks'
type ProposalProps = { type ProposalListHeaderProps = {
votes: VotingRoom[]
theme: Theme
wakuVoting: WakuVoting wakuVoting: WakuVoting
tokenBalance: number
account: string | null | undefined account: string | null | undefined
} }
export function Proposal({ wakuVoting, account }: ProposalProps) { function ProposalListHeader({ votes, theme, wakuVoting, tokenBalance, account }: ProposalListHeaderProps) {
const votes = useVotingRooms(wakuVoting)
const tokenBalance = useTokenBalance(account, wakuVoting)
const [showNewVoteModal, setShowNewVoteModal] = useState(false) const [showNewVoteModal, setShowNewVoteModal] = useState(false)
const [showConnectionModal, setShowConnectionModal] = useState(false) const [showConnectionModal, setShowConnectionModal] = useState(false)
const { activateBrowserWallet } = useEthers() const { activateBrowserWallet } = useEthers()
const history = useHistory() const history = useHistory()
const ref = useRef<HTMLHeadingElement>(null) const ref = useRef<HTMLHeadingElement>(null)
const mobileVersion = useMobileVersion(ref, 600) const mobileVersion = useMobileVersion(ref, 600)
@ -42,37 +41,51 @@ export function Proposal({ wakuVoting, account }: ProposalProps) {
}, []) }, [])
return ( return (
<ProposalWrapper ref={ref}> <div ref={ref}>
<NewVoteModal <NewVoteModal
theme={blueTheme} theme={theme}
availableAmount={tokenBalance} availableAmount={tokenBalance}
setShowModal={setShowNewVoteModal} setShowModal={setShowNewVoteModal}
showModal={showNewVoteModal} showModal={showNewVoteModal}
wakuVoting={wakuVoting} wakuVoting={wakuVoting}
/> />
{showConnectionModal && ( {showConnectionModal && (
<Modal heading="Connect" setShowModal={setShowConnectionModal} theme={blueTheme}> <Modal heading="Connect" setShowModal={setShowConnectionModal} theme={theme}>
<Networks /> <Networks />
</Modal> </Modal>
)} )}
{votes && votes?.length === 0 ? ( {votes?.length === 0 ? (
<VotingEmpty <VotingEmpty account={account} theme={theme} onConnectClick={onConnectClick} onCreateClick={onCreateClick} />
account={account}
theme={blueTheme}
onConnectClick={onConnectClick}
onCreateClick={onCreateClick}
/>
) : ( ) : (
<ProposalVotesWrapper> <ProposalHeader account={account} theme={theme} onConnectClick={onConnectClick} onCreateClick={onCreateClick} />
<ProposalHeader
account={account}
theme={blueTheme}
onConnectClick={onConnectClick}
onCreateClick={onCreateClick}
/>
<ProposalList theme={blueTheme} wakuVoting={wakuVoting} votes={votes} availableAmount={tokenBalance} />
</ProposalVotesWrapper>
)} )}
</div>
)
}
type ProposalProps = {
wakuVoting: WakuVoting
account: string | null | undefined
}
export function Proposal({ wakuVoting, account }: ProposalProps) {
const votes = useVotingRooms(wakuVoting)
const tokenBalance = useTokenBalance(account, wakuVoting)
return (
<ProposalWrapper>
<ProposalVotesWrapper>
<ProposalListHeader
votes={votes}
tokenBalance={tokenBalance}
theme={blueTheme}
account={account}
wakuVoting={wakuVoting}
/>
{votes.length > 0 && (
<ProposalList theme={blueTheme} wakuVoting={wakuVoting} votes={votes} availableAmount={tokenBalance} />
)}
</ProposalVotesWrapper>
<NotificationItem text={'Proposal you finalized will be settled after 10 confirmations.'} address={'#'} /> <NotificationItem text={'Proposal you finalized will be settled after 10 confirmations.'} address={'#'} />
</ProposalWrapper> </ProposalWrapper>

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react' import React, { useEffect, useState } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { useEthers } from '@usedapp/core' import { useEthers } from '@usedapp/core'
import { Modal, Networks, CreateButton } from '@status-waku-voting/react-components' import { Modal, Networks, CreateButton } from '@status-waku-voting/react-components'

View File

@ -1,4 +1,4 @@
import React from 'react' import React, { useEffect } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { CreateButton, Theme } from '@status-waku-voting/react-components' import { CreateButton, Theme } from '@status-waku-voting/react-components'

View File

@ -15,11 +15,12 @@ export function useVotingRooms(wakuVoting: WakuVoting) {
hash.current = newHash hash.current = newHash
} }
}, 10000) }, 10000)
setVotes([])
wakuVoting.getVotingRooms().then((e) => { wakuVoting.getVotingRooms().then((e) => {
setVotes(e) setVotes(e)
hash.current = id(e.map((votingRoom) => votingRoom.id.toString()).join('')) hash.current = id(e.map((votingRoom) => votingRoom.id.toString()).join(''))
}) })
return () => clearInterval(interval) return () => clearInterval(interval)
}, []) }, [wakuVoting])
return votes return votes
} }

View File

@ -1,16 +1,27 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
import { useRefSize } from './useRefSize'
export function useMobileVersion(myRef: React.RefObject<HTMLHeadingElement>, sizeThreshold: number) { export function useMobileVersion(myRef: React.RefObject<HTMLHeadingElement>, sizeThreshold: number) {
const [mobileVersion, setMobileVersion] = useState(false) const [mobileVersion, setMobileVersion] = useState(false)
const { width } = useRefSize(myRef)
useEffect(() => { useEffect(() => {
if (width < sizeThreshold && width > 0) { const checkDimensions = () => {
const width = window.innerWidth
if (width && width < sizeThreshold && width > 0) {
if (mobileVersion === false) {
setMobileVersion(true) setMobileVersion(true)
}
} else { } else {
if (mobileVersion === true) {
setMobileVersion(false) setMobileVersion(false)
} }
}, [width]) }
}
checkDimensions()
window.addEventListener('resize', checkDimensions)
return () => {
window.removeEventListener('resize', checkDimensions)
}
}, [myRef, mobileVersion])
return mobileVersion return mobileVersion
} }

View File

@ -4,6 +4,7 @@ export function useRefSize(myRef: React.RefObject<HTMLHeadingElement>) {
const [width, setWidth] = useState(0) const [width, setWidth] = useState(0)
const [height, setHeight] = useState(0) const [height, setHeight] = useState(0)
useEffect(() => {
const setDimensions = () => { const setDimensions = () => {
if (myRef?.current?.offsetWidth) { if (myRef?.current?.offsetWidth) {
setWidth(myRef?.current?.offsetWidth) setWidth(myRef?.current?.offsetWidth)
@ -12,8 +13,6 @@ export function useRefSize(myRef: React.RefObject<HTMLHeadingElement>) {
setHeight(myRef?.current?.offsetHeight) setHeight(myRef?.current?.offsetHeight)
} }
} }
useEffect(() => {
setDimensions() setDimensions()
window.addEventListener('resize', setDimensions) window.addEventListener('resize', setDimensions)
return () => { return () => {