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 { 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<HTMLHeadingElement>(null)
const mobileVersion = useMobileVersion(ref, 600)
@ -42,37 +41,51 @@ export function Proposal({ wakuVoting, account }: ProposalProps) {
}, [])
return (
<ProposalWrapper ref={ref}>
<div ref={ref}>
<NewVoteModal
theme={blueTheme}
theme={theme}
availableAmount={tokenBalance}
setShowModal={setShowNewVoteModal}
showModal={showNewVoteModal}
wakuVoting={wakuVoting}
/>
{showConnectionModal && (
<Modal heading="Connect" setShowModal={setShowConnectionModal} theme={blueTheme}>
<Modal heading="Connect" setShowModal={setShowConnectionModal} theme={theme}>
<Networks />
</Modal>
)}
{votes && votes?.length === 0 ? (
<VotingEmpty
account={account}
theme={blueTheme}
onConnectClick={onConnectClick}
onCreateClick={onCreateClick}
/>
{votes?.length === 0 ? (
<VotingEmpty account={account} theme={theme} onConnectClick={onConnectClick} onCreateClick={onCreateClick} />
) : (
<ProposalVotesWrapper>
<ProposalHeader
account={account}
theme={blueTheme}
onConnectClick={onConnectClick}
onCreateClick={onCreateClick}
/>
<ProposalList theme={blueTheme} wakuVoting={wakuVoting} votes={votes} availableAmount={tokenBalance} />
</ProposalVotesWrapper>
<ProposalHeader account={account} theme={theme} onConnectClick={onConnectClick} onCreateClick={onCreateClick} />
)}
</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={'#'} />
</ProposalWrapper>

View File

@ -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'

View File

@ -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'

View File

@ -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
}

View File

@ -1,16 +1,27 @@
import React, { useEffect, useState } from 'react'
import { useRefSize } from './useRefSize'
export function useMobileVersion(myRef: React.RefObject<HTMLHeadingElement>, sizeThreshold: number) {
const [mobileVersion, setMobileVersion] = useState(false)
const { width } = useRefSize(myRef)
useEffect(() => {
if (width < sizeThreshold && width > 0) {
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
}

View File

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