From 8f47873942dba0443ce52e967d0f87c20a085023 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 16 Aug 2020 16:29:10 +0200 Subject: [PATCH] useRef for table headers (no changes) (#3429) * useRef for table headers (no changes) * Cleanup Tabs hidden (constants) --- packages/page-accounts/src/Accounts/index.tsx | 26 ++++++------ packages/page-accounts/src/Contacts/index.tsx | 22 +++++----- packages/page-accounts/src/index.tsx | 17 ++++---- packages/page-contracts/src/index.tsx | 31 +++++++------- packages/page-council/src/Motions/index.tsx | 8 ++-- .../page-council/src/Overview/Candidates.tsx | 14 +++---- .../page-council/src/Overview/Members.tsx | 8 ++-- .../src/Overview/DispatchQueue.tsx | 8 ++-- .../page-democracy/src/Overview/Externals.tsx | 8 ++-- .../page-democracy/src/Overview/Proposals.tsx | 8 ++-- .../src/Overview/Referendums.tsx | 8 ++-- .../page-democracy/src/Overview/Scheduler.tsx | 41 ++++++++++--------- packages/page-democracy/src/index.tsx | 15 +++---- packages/page-explorer/src/BlockHeaders.tsx | 8 ++-- packages/page-explorer/src/BlockInfo/Logs.tsx | 6 +-- packages/page-explorer/src/NodeInfo/Peers.tsx | 8 ++-- packages/page-explorer/src/SummarySession.tsx | 15 +++---- packages/page-explorer/src/index.tsx | 19 ++++----- packages/page-extrinsics/src/index.tsx | 8 ++-- packages/page-generic-asset/src/index.tsx | 9 ++-- .../src/Overview/Parachains.tsx | 8 ++-- packages/page-settings/src/index.tsx | 6 --- packages/page-society/src/Overview/Bids.tsx | 8 ++-- .../src/Overview/CandidateVoting.tsx | 9 ++-- .../page-society/src/Overview/Candidates.tsx | 8 ++-- .../page-society/src/Overview/Defender.tsx | 19 +++++---- .../src/Overview/DefenderVoting.tsx | 9 ++-- .../page-society/src/Overview/Members.tsx | 20 ++++----- packages/page-society/src/index.tsx | 9 ++-- .../src/Actions/Account/InjectKeys.tsx | 10 +++-- packages/page-staking/src/Actions/index.tsx | 18 ++++---- .../page-staking/src/Overview/CurrentList.tsx | 14 +++---- packages/page-staking/src/Payouts/index.tsx | 8 ++-- .../page-staking/src/Query/ChartPoints.tsx | 8 ++-- packages/page-staking/src/Slashes/Era.tsx | 9 ++-- packages/page-staking/src/index.tsx | 20 ++++----- packages/page-storage/src/Selection/index.tsx | 9 ++-- packages/page-sudo/src/index.tsx | 9 ++-- .../page-tech-comm/src/Overview/Members.tsx | 8 ++-- .../page-tech-comm/src/Proposals/index.tsx | 8 ++-- packages/page-toolbox/src/index.tsx | 17 ++++---- .../page-treasury/src/Overview/Submission.tsx | 8 ++-- packages/page-treasury/src/Overview/Tips.tsx | 8 ++-- packages/page-treasury/src/index.tsx | 9 ++-- packages/react-params/src/Param/Vote.tsx | 39 +++++++++--------- 45 files changed, 288 insertions(+), 297 deletions(-) diff --git a/packages/page-accounts/src/Accounts/index.tsx b/packages/page-accounts/src/Accounts/index.tsx index dd6bab5dd2..ef3eb27296 100644 --- a/packages/page-accounts/src/Accounts/index.tsx +++ b/packages/page-accounts/src/Accounts/index.tsx @@ -7,7 +7,7 @@ import { AccountId, ProxyDefinition, ProxyType, Voting } from '@polkadot/types/i import { Delegation, SortedAccount } from '../types'; import BN from 'bn.js'; -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import styled from 'styled-components'; import keyring from '@polkadot/ui-keyring'; import { getLedger, isLedger } from '@polkadot/react-api'; @@ -84,6 +84,17 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen }); const isLoading = useLoadingDelay(); + const headerRef = useRef([ + [t('accounts'), 'start', 3], + [t('parent'), 'address ui--media-1400'], + [t('type')], + [t('tags'), 'start'], + [t('transactions'), 'ui--media-1500'], + [t('balances')], + [], + [undefined, 'mini ui--media-1400'] + ]); + useEffect((): void => { const sortedAccounts = sortAccounts(allAccounts, favorites); const sortedAddresses = sortedAccounts.map((a) => a.account.address); @@ -131,17 +142,6 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen [] ); - const header = useMemo(() => [ - [t('accounts'), 'start', 3], - [t('parent'), 'address ui--media-1400'], - [t('type')], - [t('tags'), 'start'], - [t('transactions'), 'ui--media-1500'], - [t('balances')], - [], - [undefined, 'mini ui--media-1400'] - ], [t]); - const footer = useMemo(() => ( @@ -247,7 +247,7 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen empty={!isLoading && sortedAccountsWithDelegation && t("You don't have any accounts. Some features are currently hidden and will only become available once you have accounts.")} filter={filter} footer={footer} - header={header} + header={headerRef.current} > {isLoading ? undefined : sortedAccountsWithDelegation?.map(({ account, delegation, isFavorite }, index): React.ReactNode => ( (''); const isLoading = useLoadingDelay(); + const headerRef = useRef([ + [t('contacts'), 'start', 2], + [t('tags'), 'start'], + [t('transactions'), 'ui--media-1500'], + [t('balances')], + [], + [undefined, 'mini ui--media-1400'] + ]); + useEffect((): void => { setSortedAddresses( allAddresses @@ -40,15 +49,6 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen ); }, [allAddresses, favorites]); - const header = useMemo(() => [ - [t('contacts'), 'start', 2], - [t('tags'), 'start'], - [t('transactions'), 'ui--media-1500'], - [t('balances')], - [], - [undefined, 'mini ui--media-1400'] - ], [t]); - const filter = useMemo(() => (
('no addresses saved yet, add any existing address')} filter={filter} - header={header} + header={headerRef.current} > {isLoading ? undefined : sortedAddresses?.map(({ address, isFavorite }): React.ReactNode => (
{ const { t } = useTranslation(); const { hasAccounts } = useAccounts(); const { isIpfs } = useIpfs(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'overview', @@ -36,11 +39,7 @@ function AccountsApp ({ basePath, onStatusChange }: Props): React.ReactElement

('Vanity generator') } - ], [t]); - const hidden = useMemo( - () => (hasAccounts && !isIpfs) ? [] : ['vanity'], - [hasAccounts, isIpfs] - ); + ]); return (

@@ -48,8 +47,8 @@ function AccountsApp ({ basePath, onStatusChange }: Props): React.ReactElement

diff --git a/packages/page-council/src/Motions/index.tsx b/packages/page-council/src/Motions/index.tsx index 2b89494e4a..9171e0538f 100644 --- a/packages/page-council/src/Motions/index.tsx +++ b/packages/page-council/src/Motions/index.tsx @@ -5,7 +5,7 @@ import { DeriveCollectiveProposals, DeriveCollectiveProposal } from '@polkadot/api-derive/types'; import { AccountId } from '@polkadot/types/interfaces'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Button, Table } from '@polkadot/react-components'; import { useMembers } from '@polkadot/react-hooks'; @@ -25,7 +25,7 @@ function Proposals ({ className = '', motions, prime }: Props): React.ReactEleme const { t } = useTranslation(); const { isMember, members } = useMembers(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('motions'), 'start', 2], [t('threshold')], [t('voting end')], @@ -34,7 +34,7 @@ function Proposals ({ className = '', motions, prime }: Props): React.ReactEleme [], [undefined, 'badge'], [undefined, 'mini'] - ], [t]); + ]); return (
@@ -54,7 +54,7 @@ function Proposals ({ className = '', motions, prime }: Props): React.ReactEleme ('No council motions')} - header={header} + header={headerRef.current} > {motions?.map((motion: DeriveCollectiveProposal): React.ReactNode => ( { const { t } = useTranslation(); - const headerCandidates = useMemo(() => [ + const headerCandidatesRef = useRef([ [t('candidates'), 'start', 2], [], [] - ], [t]); + ]); - const headerRunners = useMemo(() => [ + const headerRunnersRef = useRef([ [t('runners up'), 'start', 2], [t('backing')], [t('votes')] - ], [t]); + ]); return ( <>
('No runners up found')} - header={headerRunners} + header={headerRunnersRef.current} > {electionsInfo?.runnersUp.map(([accountId, balance]): React.ReactNode => (
('No candidates found')} - header={headerCandidates} + header={headerCandidatesRef.current} > {electionsInfo?.candidates.map((accountId): React.ReactNode => ( { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('members'), 'start', 2], [t('backing')], [t('votes')] - ], [t]); + ]); return (
('No members found')} - header={header} + header={headerRef.current} > {electionsInfo?.members.map(([accountId, balance]): React.ReactNode => ( | null const { api } = useApi(); const queued = useCall(api.derive.democracy.dispatchQueue, []); - const header = useMemo(() => [ + const headerRef = useRef([ [t('dispatch queue'), 'start', 2], [t('enact')], [], [undefined, 'mini ui--media-1000'] - ], [t]); + ]); return (
('Nothing queued for execution')} - header={header} + header={headerRef.current} > {queued?.map((entry): React.ReactNode => ( | null { const { api } = useApi(); const external = useCall(api.derive.democracy.nextExternal, []); - const header = useMemo(() => [ + const headerRef = useRef([ [t('external'), 'start'], [t('proposer'), 'address'], [t('locked')], [] - ], [t]); + ]); return (
('No external proposal')} - header={header} + header={headerRef.current} > {external && }
diff --git a/packages/page-democracy/src/Overview/Proposals.tsx b/packages/page-democracy/src/Overview/Proposals.tsx index 018d5f700f..33de54df63 100644 --- a/packages/page-democracy/src/Overview/Proposals.tsx +++ b/packages/page-democracy/src/Overview/Proposals.tsx @@ -4,7 +4,7 @@ import { DeriveProposal } from '@polkadot/api-derive/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Table } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; @@ -20,19 +20,19 @@ function Proposals ({ className }: Props): React.ReactElement { const { api } = useApi(); const proposals = useCall(api.derive.democracy.proposals, []); - const header = useMemo(() => [ + const headerRef = useRef([ [t('proposals'), 'start', 2], [t('proposer'), 'address'], [t('locked'), 'ui--media-1200'], [undefined, undefined, 2], [undefined, 'mini ui--media-1000'] - ], [t]); + ]); return ( ('No active proposals')} - header={header} + header={headerRef.current} > {proposals?.map((proposal): React.ReactNode => ( { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('referenda'), 'start', 2], [t('remaining'), 'ui--media-1200'], [t('activate'), 'ui--media-1400'], @@ -28,13 +28,13 @@ function Referendums ({ className = '', referendums }: Props): React.ReactElemen [undefined, undefined, 2], [undefined, 'badge'], [undefined, 'mini ui--media-1000'] - ], [t]); + ]); return (
('No active referendums')} - header={header} + header={headerRef.current} > {referendums?.map((referendum): React.ReactNode => ( []][]): ScheduledExt[] { + return entries + .filter(([, vecSchedOpt]) => vecSchedOpt.some((schedOpt) => schedOpt.isSome)) + .reduce((items: ScheduledExt[], [key, vecSchedOpt]): ScheduledExt[] => { + const blockNumber = key.args[0]; + + return vecSchedOpt + .filter((schedOpt) => schedOpt.isSome) + .map((schedOpt) => schedOpt.unwrap()) + .reduce((items: ScheduledExt[], { call, maybeId, maybePeriodic, priority }, index) => { + items.push({ blockNumber, call, key: `${blockNumber.toString()}-${index}`, maybeId, maybePeriodic, priority }); + + return items; + }, items); + }, []); +} + function Schedule ({ className = '' }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const items = useCall(api.query.scheduler.agenda.entries as any, [], { - transform: (entries: [{ args: [BlockNumber] }, Option[]][]): ScheduledExt[] => - entries - .filter(([, vecSchedOpt]) => vecSchedOpt.some((schedOpt) => schedOpt.isSome)) - .reduce((items: ScheduledExt[], [key, vecSchedOpt]): ScheduledExt[] => { - const blockNumber = key.args[0]; - - return vecSchedOpt - .filter((schedOpt) => schedOpt.isSome) - .map((schedOpt) => schedOpt.unwrap()) - .reduce((items: ScheduledExt[], { call, maybeId, maybePeriodic, priority }, index) => { - items.push({ blockNumber, call, key: `${blockNumber.toString()}-${index}`, maybeId, maybePeriodic, priority }); - - return items; - }, items); - }, []) + transform: transformEntries }); - const header = useMemo(() => [ + const headerRef = useRef([ [t('scheduled'), 'start'], [t('remaining')], [t('period')], [t('count')] - ], [t]); + ]); return (
('No active schedules')} - header={header} + header={headerRef.current} > {items?.map((scheduled): React.ReactNode => (
('No blocks available')} - header={header} + header={headerRef.current} > {headers .filter((header) => !!header) diff --git a/packages/page-explorer/src/BlockInfo/Logs.tsx b/packages/page-explorer/src/BlockInfo/Logs.tsx index 402f83ba38..b4cce86fc0 100644 --- a/packages/page-explorer/src/BlockInfo/Logs.tsx +++ b/packages/page-explorer/src/BlockInfo/Logs.tsx @@ -5,7 +5,7 @@ import { DigestItem } from '@polkadot/types/interfaces'; import { Codec, TypeDef } from '@polkadot/types/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Struct, Tuple, Raw, Vec, getTypeDef } from '@polkadot/types'; import { Expander, Table } from '@polkadot/react-components'; import Params from '@polkadot/react-params'; @@ -103,12 +103,12 @@ function Logs (props: Props): React.ReactElement | null { const { value } = props; const { t } = useTranslation(); - const header = useMemo(() => [[t('logs'), 'start']], [t]); + const headerRef = useRef([[t('logs'), 'start']]); return (
('No logs available')} - header={header} + header={headerRef.current} > {value?.map((log, index) => ( diff --git a/packages/page-explorer/src/NodeInfo/Peers.tsx b/packages/page-explorer/src/NodeInfo/Peers.tsx index 934e8fe0de..922282a8c9 100644 --- a/packages/page-explorer/src/NodeInfo/Peers.tsx +++ b/packages/page-explorer/src/NodeInfo/Peers.tsx @@ -4,7 +4,7 @@ import { PeerInfo } from '@polkadot/types/interfaces'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import styled from 'styled-components'; import { formatNumber } from '@polkadot/util'; @@ -19,18 +19,18 @@ interface Props { function Peers ({ className = '', peers }: Props): React.ReactElement { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('connected peers'), 'start'], [t('role'), 'start'], [t('best #'), 'number'], [t('best hash'), 'hash'] - ], [t]); + ]); return (
('no peers connected')} - header={header} + header={headerRef.current} > {peers?.sort((a, b): number => b.bestNumber.cmp(a.bestNumber)).map((peer) => ( diff --git a/packages/page-explorer/src/SummarySession.tsx b/packages/page-explorer/src/SummarySession.tsx index 3a9658da25..f20d4b86dd 100644 --- a/packages/page-explorer/src/SummarySession.tsx +++ b/packages/page-explorer/src/SummarySession.tsx @@ -5,7 +5,7 @@ import { DeriveSessionProgress } from '@polkadot/api-derive/types'; import { Forcing } from '@polkadot/types/interfaces'; -import React, { useMemo } from 'react'; +import React from 'react'; import { CardSummary } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; @@ -22,14 +22,11 @@ function SummarySession ({ withEra = true, withSession = true }: Props): React.R const { api } = useApi(); const sessionInfo = useCall(api.query.staking && api.derive.session?.progress, []); const forcing = useCall(api.query.staking?.forceEra, []); - const eraLabel = useMemo(() => - t('era') - , [t]); - const sessionLabel = useMemo(() => - sessionInfo?.isEpoch - ? t('epoch') - : t('session') - , [sessionInfo?.isEpoch, t]); + + const eraLabel = t('era'); + const sessionLabel = sessionInfo?.isEpoch + ? t('epoch') + : t('session'); return ( <> diff --git a/packages/page-explorer/src/index.tsx b/packages/page-explorer/src/index.tsx index 101459118a..91ef5cd7a2 100644 --- a/packages/page-explorer/src/index.tsx +++ b/packages/page-explorer/src/index.tsx @@ -4,13 +4,11 @@ import { KeyedEvent } from '@polkadot/react-query/types'; -import React, { useContext, useMemo } from 'react'; +import React, { useContext, useRef } from 'react'; import { Route, Switch } from 'react-router'; -// import styled from 'styled-components'; import Tabs from '@polkadot/react-components/Tabs'; import { useApi } from '@polkadot/react-hooks'; import { BlockAuthorsContext, EventsContext } from '@polkadot/react-query'; -import uiSettings from '@polkadot/ui-settings'; import BlockInfo from './BlockInfo'; import Forks from './Forks'; @@ -24,12 +22,15 @@ interface Props { newEvents?: KeyedEvent[]; } +const HIDDESN_NOBABE = ['forks']; + function ExplorerApp ({ basePath, className }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { lastHeaders } = useContext(BlockAuthorsContext); const events = useContext(EventsContext); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'chain', @@ -48,19 +49,15 @@ function ExplorerApp ({ basePath, className }: Props): React.ReactElement name: 'node', text: t('Node info') } - ], [t]); + ]); return (
diff --git a/packages/page-extrinsics/src/index.tsx b/packages/page-extrinsics/src/index.tsx index e438a6a4c3..084942d27c 100644 --- a/packages/page-extrinsics/src/index.tsx +++ b/packages/page-extrinsics/src/index.tsx @@ -4,7 +4,7 @@ import { AppProps as Props } from '@polkadot/react-components/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Tabs } from '@polkadot/react-components'; import Selection from './Selection'; @@ -13,18 +13,18 @@ import { useTranslation } from './translate'; function ExtrinsicsApp ({ basePath }: Props): React.ReactElement { const { t } = useTranslation(); - const items = useMemo(() => [{ + const itemsRef = useRef([{ isRoot: true, name: 'create', text: t('Extrinsic submission') - }], [t]); + }]); return (
diff --git a/packages/page-generic-asset/src/index.tsx b/packages/page-generic-asset/src/index.tsx index ebd856ad57..055143a467 100644 --- a/packages/page-generic-asset/src/index.tsx +++ b/packages/page-generic-asset/src/index.tsx @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Route, Switch } from 'react-router'; import { Tabs } from '@polkadot/react-components'; @@ -17,7 +17,8 @@ interface Props { function AssetApp ({ basePath }: Props): React.ReactElement { const { t } = useTranslation(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'assets', @@ -27,14 +28,14 @@ function AssetApp ({ basePath }: Props): React.ReactElement { name: 'transfer', text: t('Transfer') } - ], [t]); + ]); return (
diff --git a/packages/page-parachains/src/Overview/Parachains.tsx b/packages/page-parachains/src/Overview/Parachains.tsx index 6a75b8756e..cd190c6a51 100644 --- a/packages/page-parachains/src/Overview/Parachains.tsx +++ b/packages/page-parachains/src/Overview/Parachains.tsx @@ -4,7 +4,7 @@ import { DeriveParachain } from '@polkadot/api-derive/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import styled from 'styled-components'; import { Table } from '@polkadot/react-components'; @@ -18,18 +18,18 @@ interface Props { function Parachains ({ parachains }: Props): React.ReactElement { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('parachains'), 'start', 3], [t('heads'), 'start'], [t('swap to id')], [t('scheduling')], [] - ], [t]); + ]); return (
('There are no registered parachains')} - header={header} + header={headerRef.current} > {parachains?.map((parachain): React.ReactNode => (
('No bids')} - header={header} + header={headerRef.current} > {bids?.map((bid, index): React.ReactNode => ( (null); - const voteOpts = useMemo(() => [ + + const voteOptsRef = useRef([ { text: t('Aye, I approve'), value: true }, { text: t('Nay, I do not approve'), value: false } - ], [t]); + ]); return ( <> @@ -39,7 +40,7 @@ function CandidateVoting ({ candidateId, isMember, ownMembers }: Props): React.R help={t('Approve this candidacy.')} label={t('vote for candidate')} onChange={setVote} - options={voteOpts} + options={voteOptsRef.current} value={vote} /> diff --git a/packages/page-society/src/Overview/Candidates.tsx b/packages/page-society/src/Overview/Candidates.tsx index 9c175b9e3c..29abdd1429 100644 --- a/packages/page-society/src/Overview/Candidates.tsx +++ b/packages/page-society/src/Overview/Candidates.tsx @@ -4,7 +4,7 @@ import { DeriveSocietyCandidate } from '@polkadot/api-derive/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Table } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; @@ -23,18 +23,18 @@ function Candidates ({ allMembers, className = '', isMember, ownMembers }: Props const { api } = useApi(); const candidates = useCall(api.derive.society.candidates, []); - const header = useMemo(() => [ + const headerRef = useRef([ [t('candidates'), 'start'], [t('kind')], [t('value')], [t('votes'), 'start'] - ], [t]); + ]); return (
('No candidates')} - header={header} + header={headerRef.current} > {candidates?.map((candidate): React.ReactNode => ( !!vote) + .map(({ accountId, vote }): VoteType => [accountId.toString(), vote as SocietyVote]); +} + function Defender ({ className = '', info, isMember, ownMembers }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const votes = useCall(api.derive.society.members, [], { - transform: (members: DeriveSocietyMember[]): VoteType[] => - members - .filter(({ vote }): boolean => !!vote) - .map(({ accountId, vote }): VoteType => [accountId.toString(), vote as SocietyVote]) + transform: transformVotes }); - const header = useMemo(() => [ + const headerRef = useRef([ [t('defender'), 'start'], [t('votes'), 'start'], [] - ], [t]); + ]); if (!info || !info.hasDefender || !info.defender) { return null; @@ -44,7 +47,7 @@ function Defender ({ className = '', info, isMember, ownMembers }: Props): React return (
diff --git a/packages/page-society/src/Overview/DefenderVoting.tsx b/packages/page-society/src/Overview/DefenderVoting.tsx index ea446fcb97..18ea68e232 100644 --- a/packages/page-society/src/Overview/DefenderVoting.tsx +++ b/packages/page-society/src/Overview/DefenderVoting.tsx @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import React, { useMemo, useState } from 'react'; +import React, { useRef, useState } from 'react'; import { useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; @@ -18,10 +18,11 @@ function DefenderVoting ({ isMember, ownMembers }: Props): React.ReactElement(null); - const voteOpts = useMemo(() => [ + + const voteOptsRef = useRef([ { text: t('Aye, I approve'), value: true }, { text: t('Nay, I do not approve'), value: false } - ], [t]); + ]); return ( <> @@ -38,7 +39,7 @@ function DefenderVoting ({ isMember, ownMembers }: Props): React.ReactElement('Approve or reject this defender.')} label={t('vote for defender')} onChange={setVote} - options={voteOpts} + options={voteOptsRef.current} value={vote} /> diff --git a/packages/page-society/src/Overview/Members.tsx b/packages/page-society/src/Overview/Members.tsx index 8d8b47fdad..c6b5d64a31 100644 --- a/packages/page-society/src/Overview/Members.tsx +++ b/packages/page-society/src/Overview/Members.tsx @@ -4,7 +4,7 @@ import { DeriveSociety, DeriveSocietyMember } from '@polkadot/api-derive/types'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useMemo, useRef } from 'react'; import { Table } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; @@ -20,25 +20,23 @@ function Members ({ className = '', info }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const members = useCall(api.derive.society.members, []); - const [filtered, setFiltered] = useState([]); - useEffect((): void => { - members && setFiltered( - members.filter((member): boolean => !info || !info.hasDefender || !member.accountId.eq(info.defender)) - ); - }, [info, members]); - - const header = useMemo(() => [ + const headerRef = useRef([ [t('members'), 'start', 3], [t('strikes')], [undefined, 'mini'] - ], [t]); + ]); + + const filtered = useMemo( + () => (members || []).filter((member) => !info || !info.hasDefender || !member.accountId.eq(info.defender)), + [info, members] + ); return ( ('No active members')} - header={header} + header={headerRef.current} > {filtered.map((member): React.ReactNode => ( { const { t } = useTranslation(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'overview', text: t('Society overview') } - ], [t]); + ]); return (
diff --git a/packages/page-staking/src/Actions/Account/InjectKeys.tsx b/packages/page-staking/src/Actions/Account/InjectKeys.tsx index ccaa7a1722..ab09eec681 100644 --- a/packages/page-staking/src/Actions/Account/InjectKeys.tsx +++ b/packages/page-staking/src/Actions/Account/InjectKeys.tsx @@ -4,7 +4,7 @@ import { KeypairType } from '@polkadot/util-crypto/types'; -import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import { Button, Dropdown, Input, Modal, StatusContext } from '@polkadot/react-components'; import keyring from '@polkadot/ui-keyring'; import { assert, u8aToHex } from '@polkadot/util'; @@ -34,13 +34,14 @@ function InjectKeys ({ onClose }: Props): React.ReactElement | null { const [publicKey, setPublicKey] = useState(EMPTY_KEY); const [suri, setSuri] = useState(''); const [keyType, setKeyType] = useState('babe'); - const keyTypeOpt = useMemo(() => [ + + const keyTypeOptRef = useRef([ { text: t('Aura'), value: 'aura' }, { text: t('Babe'), value: 'babe' }, { text: t('Grandpa'), value: 'gran' }, { text: t('I\'m Online'), value: 'imon' }, { text: t('Parachains'), value: 'para' } - ], [t]); + ]); useEffect((): void => { setCrypto(CRYPTO_MAP[keyType][0]); @@ -66,6 +67,7 @@ function InjectKeys ({ onClose }: Props): React.ReactElement | null { }), [keyType, publicKey, queueRpc, suri] ); + const _cryptoOptions = useMemo( () => CRYPTO_MAP[keyType].map((value): { text: string; value: KeypairType } => ({ text: value === 'ed25519' @@ -104,7 +106,7 @@ function InjectKeys ({ onClose }: Props): React.ReactElement | null { ('key type to set')} onChange={setKeyType} - options={keyTypeOpt} + options={keyTypeOptRef.current} value={keyType} /> extractState(ownStashes), - [ownStashes] - ); - - const header = useMemo(() => [ + const headerRef = useRef([ [t('stashes'), 'start', 2], [t('controller'), 'address'], [t('rewards'), 'number ui--media-1200'], [t('bonded'), 'number'], [undefined, undefined, 2] - ], [t]); + ]); + + const { bondedTotal, foundStashes } = useMemo( + () => extractState(ownStashes), + [ownStashes] + ); const footer = useMemo(() => (
@@ -93,7 +93,7 @@ function Actions ({ className = '', isInElection, ownStashes, targets }: Props):
('No funds staked yet. Bond funds to validate or nominate a validator')} footer={footer} - header={header} + header={headerRef.current} > {foundStashes?.map((info): React.ReactNode => ( [ + const headerWaitingRef = useRef([ [t('intentions'), 'start', 2], [t('nominators'), 'start', 2], [t('commission'), 'number', 1], [], [] - ], [t]); + ]); - const headerActive = useMemo(() => [ + const headerActiveRef = useRef([ [t('validators'), 'start', 2], [t('other stake')], [t('own stake')], @@ -130,7 +130,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi [t('last #')], [], [] - ], [t]); + ]); const _renderRows = useCallback( (addresses?: AccountExtend[], isMain?: boolean): React.ReactNode[] => @@ -167,7 +167,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi withIdentity={withIdentity} /> } - header={headerWaiting} + header={headerWaitingRef.current} > {isLoading ? undefined : _renderRows(elected, false).concat(_renderRows(waiting, false))}
@@ -183,7 +183,7 @@ function CurrentList ({ favorites, hasQueries, isIntentions, next, stakingOvervi withIdentity={withIdentity} /> } - header={headerActive} + header={headerActiveRef.current} > {isLoading ? undefined : _renderRows(validators, true)}
diff --git a/packages/page-staking/src/Payouts/index.tsx b/packages/page-staking/src/Payouts/index.tsx index 9e5bc22fcd..df8fa91f25 100644 --- a/packages/page-staking/src/Payouts/index.tsx +++ b/packages/page-staking/src/Payouts/index.tsx @@ -8,7 +8,7 @@ import { StakerState } from '@polkadot/react-hooks/types'; import { PayoutStash, PayoutValidator } from './types'; import BN from 'bn.js'; -import React, { useMemo, useState } from 'react'; +import React, { useMemo, useRef, useState } from 'react'; import styled from 'styled-components'; import { ApiPromise } from '@polkadot/api'; import { Button, Table } from '@polkadot/react-components'; @@ -182,13 +182,13 @@ function Payouts ({ className = '', isInElection, ownValidators }: Props): React [undefined, undefined, 3] ], [myStashesIndex, t]); - const headerValidators = useMemo(() => [ + const headerValidatorsRef = useRef([ [t('payout/validator'), 'start', 2], [t('eras'), 'start'], [t('available')], [('remaining')], [undefined, undefined, 3] - ], [t]); + ]); const valOptions = useMemo(() => [ { isDisabled: !hasOwnValidators, text: t('My validators'), value: 'val' }, @@ -245,7 +245,7 @@ function Payouts ({ className = '', isInElection, ownValidators }: Props): React {api.tx.staking.payoutStakers && (myStashesIndex === 1) && !isLoadingRewards && validators && (validators.length !== 0) && ( {!isLoadingRewards && validators.map((payout): React.ReactNode => ( diff --git a/packages/page-staking/src/Query/ChartPoints.tsx b/packages/page-staking/src/Query/ChartPoints.tsx index ce792a77a3..b56ccbce98 100644 --- a/packages/page-staking/src/Query/ChartPoints.tsx +++ b/packages/page-staking/src/Query/ChartPoints.tsx @@ -5,7 +5,7 @@ import { DeriveStakerPoints } from '@polkadot/api-derive/types'; import { ChartInfo, LineDataEntry, Props } from './types'; -import React, { useMemo } from 'react'; +import React, { useMemo, useRef } from 'react'; import { Chart, Spinner } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; @@ -50,10 +50,10 @@ function ChartPoints ({ validatorId }: Props): React.ReactElement { [stakerPoints] ); - const legends = useMemo(() => [ + const legendsRef = useRef([ t('points'), t('average') - ], [t]); + ]); return (
@@ -63,7 +63,7 @@ function ChartPoints ({ validatorId }: Props): React.ReactElement { ) diff --git a/packages/page-staking/src/Slashes/Era.tsx b/packages/page-staking/src/Slashes/Era.tsx index 3aecc66938..15dd30949b 100644 --- a/packages/page-staking/src/Slashes/Era.tsx +++ b/packages/page-staking/src/Slashes/Era.tsx @@ -4,7 +4,7 @@ import { SlashEra } from './types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Table } from '@polkadot/react-components'; import { useTranslation } from '../translate'; @@ -17,20 +17,21 @@ interface Props { function Slashes ({ slash }: Props): React.ReactElement | null { const { t } = useTranslation(); - const header = useMemo((): [string?, string?, number?][] => [ + + const headerRef = useRef<[string?, string?, number?][]>([ [undefined, 'start', 3], [t('reporters'), 'address'], [t('own')], [t('other')], [t('total')], [t('payout')] - ], [t]); + ]); return (
- {header.map(([label, className, colSpan = 1], index): React.ReactNode => ( + {headerRef.current.map(([label, className, colSpan = 1], index): React.ReactNode => (
{ const { t } = useTranslation(); const { api } = useApi(); @@ -95,22 +98,19 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement !!q), [api, slashes, t]); - const hiddenTabs = useMemo( - (): string[] => !hasAccounts - ? ['actions', 'payouts', 'query'] - : !hasQueries - ? ['returns', 'query'] - : [], - [hasAccounts, hasQueries] - ); - return (
diff --git a/packages/page-storage/src/Selection/index.tsx b/packages/page-storage/src/Selection/index.tsx index 831b3f114b..b1f2165ef7 100644 --- a/packages/page-storage/src/Selection/index.tsx +++ b/packages/page-storage/src/Selection/index.tsx @@ -4,7 +4,7 @@ import { QueryTypes, ParitalQueryTypes } from '../types'; -import React, { useCallback, useMemo } from 'react'; +import React, { useCallback, useRef } from 'react'; import { Route, Switch } from 'react-router'; import { Tabs } from '@polkadot/react-components'; @@ -22,7 +22,8 @@ let id = -1; function Selection ({ basePath, onAdd }: Props): React.ReactElement { const { t } = useTranslation(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'modules', @@ -36,7 +37,7 @@ function Selection ({ basePath, onAdd }: Props): React.ReactElement { name: 'raw', text: t('Raw storage') } - ], [t]); + ]); const _onAdd = useCallback( (query: ParitalQueryTypes): void => onAdd({ ...query, id: ++id }), @@ -48,7 +49,7 @@ function Selection ({ basePath, onAdd }: Props): React.ReactElement {
diff --git a/packages/page-sudo/src/index.tsx b/packages/page-sudo/src/index.tsx index 22aca9438b..515e802bc9 100644 --- a/packages/page-sudo/src/index.tsx +++ b/packages/page-sudo/src/index.tsx @@ -4,7 +4,7 @@ import { AppProps as Props } from '@polkadot/react-components/types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Route, Switch } from 'react-router'; import { Icon, Tabs } from '@polkadot/react-components'; import { useSudo } from '@polkadot/react-hooks'; @@ -17,7 +17,8 @@ import { useTranslation } from './translate'; function SudoApp ({ basePath }: Props): React.ReactElement { const { t } = useTranslation(); const { allAccounts, isMine, sudoKey } = useSudo(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'index', @@ -27,14 +28,14 @@ function SudoApp ({ basePath }: Props): React.ReactElement { name: 'key', text: t('Set sudo key') } - ], [t]); + ]); return (
{isMine diff --git a/packages/page-tech-comm/src/Overview/Members.tsx b/packages/page-tech-comm/src/Overview/Members.tsx index de921c3781..640cbe4966 100644 --- a/packages/page-tech-comm/src/Overview/Members.tsx +++ b/packages/page-tech-comm/src/Overview/Members.tsx @@ -4,7 +4,7 @@ import { AccountId } from '@polkadot/types/interfaces'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { AddressSmall, Table, Tag } from '@polkadot/react-components'; import { useTranslation } from '../translate'; @@ -18,15 +18,15 @@ interface Props { function Members ({ className = '', members, prime }: Props): React.ReactElement { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('members'), 'start', 3] - ], [t]); + ]); return ( ('No members found')} - header={header} + header={headerRef.current} > {members?.map((accountId): React.ReactNode => ( diff --git a/packages/page-tech-comm/src/Proposals/index.tsx b/packages/page-tech-comm/src/Proposals/index.tsx index c0de06dcd9..4cbbe7cb1a 100644 --- a/packages/page-tech-comm/src/Proposals/index.tsx +++ b/packages/page-tech-comm/src/Proposals/index.tsx @@ -5,7 +5,7 @@ import { Hash } from '@polkadot/types/interfaces'; import { ComponentProps as Props } from '../types'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { Button, Table } from '@polkadot/react-components'; import { useTranslation } from '../translate'; @@ -15,14 +15,14 @@ import Propose from './Propose'; function Proposals ({ className = '', isMember, members, prime, proposals }: Props): React.ReactElement { const { t } = useTranslation(); - const header = useMemo(() => [ + const headerRef = useRef([ [t('proposals'), 'start', 2], [t('threshold')], [t('voting end')], [t('aye'), 'address'], [t('nay'), 'address'], [] - ], [t]); + ]); return (
@@ -34,7 +34,7 @@ function Proposals ({ className = '', isMember, members, prime, proposals }: Pro
('No committee proposals')} - header={header} + header={headerRef.current} > {proposals?.map((hash: Hash): React.ReactNode => ( { const { t } = useTranslation(); const { hasAccounts } = useAccounts(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'rpc', @@ -36,19 +39,15 @@ function ToolboxApp ({ basePath }: Props): React.ReactElement { name: 'verify', text: t('Verify signature') } - ], [t]); + ]); return (
diff --git a/packages/page-treasury/src/Overview/Submission.tsx b/packages/page-treasury/src/Overview/Submission.tsx index 2411282c6e..e37761e132 100644 --- a/packages/page-treasury/src/Overview/Submission.tsx +++ b/packages/page-treasury/src/Overview/Submission.tsx @@ -6,7 +6,7 @@ import { SubmittableExtrinsic } from '@polkadot/api/types'; import { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; import { ProposalIndex } from '@polkadot/types/interfaces'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { getTreasuryThreshold } from '@polkadot/app-council/thresholds'; import { Button, Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components'; import { useApi, useToggle } from '@polkadot/react-hooks'; @@ -36,10 +36,10 @@ function Submission ({ councilProposals, id, isDisabled, members }: Props): Reac const threshold = Math.ceil((members?.length || 0) * getTreasuryThreshold(api)); - const councilTypeOpt = useMemo(() => [ + const councilTypeOptRef = useRef([ { text: t('Acceptance proposal to council'), value: 'accept' }, { text: t('Rejection proposal to council'), value: 'reject' } - ], [t]); + ]); useEffect((): void => { setHasProposals( @@ -91,7 +91,7 @@ function Submission ({ councilProposals, id, isDisabled, members }: Props): Reac help={t('The type of council proposal to submit.')} label={t('council proposal type')} onChange={setCouncilType} - options={councilTypeOpt} + options={councilTypeOptRef.current} value={councilType} /> diff --git a/packages/page-treasury/src/Overview/Tips.tsx b/packages/page-treasury/src/Overview/Tips.tsx index 1b99a9469c..81b46a9149 100644 --- a/packages/page-treasury/src/Overview/Tips.tsx +++ b/packages/page-treasury/src/Overview/Tips.tsx @@ -4,7 +4,7 @@ import { BlockNumber, OpenTip, OpenTipTo225 } from '@polkadot/types/interfaces'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { Table } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { Option } from '@polkadot/types'; @@ -45,7 +45,7 @@ function Tips ({ className = '', hashes, isMember, members }: Props): React.Reac ); }, [hashes, optTips]); - const header = useMemo(() => [ + const headerRef = useRef([ [t('tips'), 'start'], [t('finder'), 'address'], [t('reason'), 'start'], @@ -53,13 +53,13 @@ function Tips ({ className = '', hashes, isMember, members }: Props): React.Reac [], [undefined, 'badge ui--media-1700'], [undefined, 'mini ui--media-1700'] - ].filter((v) => v), [t]); + ]); return (
('No open tips')} - header={header} + header={headerRef.current} > {tips?.map(([hash, tip]): React.ReactNode => ( { const { t } = useTranslation(); - const items = useMemo(() => [ + + const itemsRef = useRef([ { isRoot: true, name: 'overview', text: t('Treasury overview') } - ], [t]); + ]); return (
@@ -33,7 +34,7 @@ function TreasuryApp ({ basePath }: Props): React.ReactElement {
diff --git a/packages/react-params/src/Param/Vote.tsx b/packages/react-params/src/Param/Vote.tsx index 6d4c80c77b..4f2091a763 100644 --- a/packages/react-params/src/Param/Vote.tsx +++ b/packages/react-params/src/Param/Vote.tsx @@ -5,7 +5,7 @@ import { Props } from '../types'; import BN from 'bn.js'; -import React, { useMemo } from 'react'; +import React, { useRef } from 'react'; import { GenericVote } from '@polkadot/types'; import { Dropdown } from '@polkadot/react-components'; @@ -23,23 +23,22 @@ function doChange (onChange?: (value: any) => void): (_: number) => void { function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, onChange, withLabel }: Props): React.ReactElement { const { t } = useTranslation(); - const optAye = useMemo(() => { - return [ - { text: t('Nay'), value: 0 }, - { text: t('Aye'), value: -1 } - ]; - }, [t]); - const optConv = useMemo(() => { - return [ - { text: t('None'), value: 0 }, - { text: t('Locked1x'), value: 1 }, - { text: t('Locked2x'), value: 2 }, - { text: t('Locked3x'), value: 3 }, - { text: t('Locked4x'), value: 4 }, - { text: t('Locked5x'), value: 5 }, - { text: t('Locked6x'), value: 6 } - ]; - }, [t]); + + const optAyeRef = useRef([ + { text: t('Nay'), value: 0 }, + { text: t('Aye'), value: -1 } + ]); + + const optConvRef = useRef([ + { text: t('None'), value: 0 }, + { text: t('Locked1x'), value: 1 }, + { text: t('Locked2x'), value: 2 }, + { text: t('Locked3x'), value: 3 }, + { text: t('Locked4x'), value: 4 }, + { text: t('Locked5x'), value: 5 }, + { text: t('Locked6x'), value: 6 } + ]); + const defaultValue = value instanceof BN ? value.toNumber() : value instanceof GenericVote @@ -58,7 +57,7 @@ function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, o isError={isError} label={t('aye: bool')} onChange={doChange(onChange)} - options={optAye} + options={optAyeRef.current} withLabel={withLabel} /> {isDisabled && ( @@ -68,7 +67,7 @@ function Vote ({ className = '', defaultValue: { value }, isDisabled, isError, o isDisabled={isDisabled} isError={isError} label={t('conviction: Conviction')} - options={optConv} + options={optConvRef.current} withLabel={withLabel} /> )}