Adjust deps for new hooks rules (#2757)

This commit is contained in:
Jaco Greeff 2020-05-16 10:45:54 +02:00 committed by GitHub
parent 35efbecc95
commit 029b5c6c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 49 additions and 42 deletions

View File

@ -11,7 +11,6 @@ module.exports = {
},
rules: {
...base.rules,
'@typescript-eslint/no-explicit-any': 'off',
'react-hooks/exhaustive-deps': 'warn'
'@typescript-eslint/no-explicit-any': 'off'
}
};

View File

@ -75,7 +75,7 @@ function Identity ({ address, className, onClose }: Props): React.ReactElement<P
const [valWeb, setValWeb] = useState('');
useEffect((): void => {
if (identityOpt?.isSome) {
if (identityOpt && identityOpt.isSome) {
const { info } = identityOpt.unwrap();
setData(info.display, null, setValDisplay);

View File

@ -35,7 +35,7 @@ function ValidateAddr ({ address, onChange }: Props): React.ReactElement<Props>
useEffect((): void => {
setIsStored(!!contractInfo?.isSome);
}, [contractInfo]);
}, [contractInfo?.isSome]);
useEffect((): void => {
onChange(isAddress && isStored);

View File

@ -48,7 +48,7 @@ function Slashing ({ className, isMember, members }: Props): React.ReactElement<
}, [slashes, t]);
useEffect((): void => {
const actioned = selectedEra && slashes?.find(([era]): boolean => era.eqn(selectedEra));
const actioned = selectedEra && slashes && slashes.find(([era]): boolean => era.eqn(selectedEra));
setProposal((): any =>
actioned

View File

@ -22,7 +22,7 @@ export default function useAvailableSlashes (): [BN, UnappliedSlash[]][] {
useEffect((): Unsub => {
let unsub: Unsub | undefined;
if (mountedRef.current && indexes && earliestSlash?.isSome) {
if (mountedRef.current && indexes && earliestSlash && earliestSlash.isSome) {
const from = earliestSlash.unwrap();
const range: BN[] = [];
let start = new BN(from);

View File

@ -42,7 +42,7 @@ function Fasttrack ({ imageHash, threshold }: Props): React.ReactElement<Props>
useEffect((): void => {
setProposal(
() => delayBlocks?.gtn(0) && votingBlocks?.gtn(0)
() => delayBlocks && delayBlocks.gtn(0) && votingBlocks && votingBlocks.gtn(0)
? api.tx.democracy.fastTrack(imageHash, votingBlocks, delayBlocks)
: null
);

View File

@ -18,7 +18,7 @@ export default function useCounter (): number {
(proposals?.length || 0) +
(referenda?.length || 0)
);
}, [mountedRef, proposals, referenda]);
}, [mountedRef, proposals?.length, referenda?.length]);
return counter;
}

View File

@ -27,7 +27,7 @@ function SummarySession ({ withEra = true, withSession = true }: Props): React.R
sessionInfo?.isEpoch
? t('epoch')
: t('session')
, [sessionInfo, t]);
, [sessionInfo?.isEpoch, t]);
return (
<>

View File

@ -32,7 +32,7 @@ function ParachainsApp ({ basePath }: Props): React.ReactElement<Props> {
text: t('Parachains overview')
},
...(
match?.params.id
match && match.params.id
? [{
name: match.params.id,
text: `${match.params.id}: ${parachainName(t, paraInfoRef.current)}`

View File

@ -25,7 +25,7 @@ function Summary ({ className, info }: Props): React.ReactElement<Props> {
const bestNumber = useCall<BlockNumber>(api.derive.chain.bestNumber, []);
const pot = useMemo((): string | null => {
return info?.pot.gtn(0)
return info && info.pot.gtn(0)
? info.pot.toString()
: null;
}, [info]);

View File

@ -56,7 +56,7 @@ export default function useInactives (stashId: string, nominees?: string[]): Ina
useEffect((): () => void => {
let unsub: (() => void) | undefined;
if (mountedRef.current && nominees?.length && indexes) {
if (mountedRef.current && nominees && nominees.length && indexes) {
api
.queryMulti(
[[api.query.staking.nominators, stashId] as any].concat(

View File

@ -18,7 +18,7 @@ export default function useUnbondDuration (): BN | undefined {
useEffect((): void => {
// We check sessionLength, for aura this is not properly calculated (not exposed),
// so the actual real block value would be invalid here
sessionInfo?.sessionLength.gt(ONE) && setDuration(
sessionInfo && sessionInfo.sessionLength.gt(ONE) && setDuration(
sessionInfo.eraLength.mul(api.consts.staking.bondingDuration)
);
}, [api, sessionInfo]);

View File

@ -17,7 +17,7 @@ export default function useEraBlocks (era?: BN): BN | undefined {
const [duration, setDuration] = useState<BN | undefined>();
useEffect((): void => {
depth && era && progress?.sessionLength.gt(ONE) && setDuration(
depth && era && progress && progress.sessionLength.gt(ONE) && setDuration(
progress.eraLength
.mul(
depth

View File

@ -20,7 +20,7 @@ export default function useStakerPayouts (): BN {
const migrateEraOpt = useCall<Option<EraIndex>>(api.query.staking?.migrateEra, []);
useEffect((): void => {
migrateEraOpt?.isSome && setState(migrateEraOpt.unwrap());
migrateEraOpt && migrateEraOpt.isSome && setState(migrateEraOpt.unwrap());
}, [migrateEraOpt]);
return stakerPayoutAfter;

View File

@ -8,6 +8,7 @@ import { SessionRewards } from '../types';
import { useEffect, useState } from 'react';
import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks';
import { u32 } from '@polkadot/types';
import { isFunction } from '@polkadot/util';
export default function useBlockCounts (accountId: string, sessionRewards: SessionRewards[]): u32[] {
const { api } = useApi();
@ -18,7 +19,7 @@ export default function useBlockCounts (accountId: string, sessionRewards: Sessi
const [historic, setHistoric] = useState<u32[]>([]);
useEffect((): void => {
if (api.query.imOnline?.authoredBlocks && sessionRewards?.length) {
if (isFunction(api.query.imOnline?.authoredBlocks) && sessionRewards && sessionRewards.length) {
const filtered = sessionRewards.filter(({ sessionIndex }): boolean => sessionIndex.gtn(0));
if (filtered.length) {

View File

@ -31,7 +31,7 @@ function Nominate ({ className, ownNominators, targets }: Props): React.ReactEle
const _onChangeStash = useCallback(
(accountId?: string | null): void => {
const acc = ownNominators?.find(({ stashId }) => stashId === accountId);
const acc = ownNominators && ownNominators.find(({ stashId }) => stashId === accountId);
setIds(
acc

View File

@ -26,7 +26,7 @@ function Summary ({ lastReward, numNominators, numValidators, totalStaked }: Pro
const [percentage, setPercentage] = useState<string | undefined>();
useEffect((): void => {
totalIssuance && totalStaked?.gtn(0) && setPercentage(
totalIssuance && totalStaked && totalStaked.gtn(0) && setPercentage(
`${(totalStaked.muln(10000).div(totalIssuance).toNumber() / 100).toFixed(2)}%`
);
}, [totalIssuance, totalStaked]);

View File

@ -41,9 +41,10 @@ function Sign ({ className }: Props): React.ReactElement<Props> {
const [isUnlockVisible, toggleUnlock] = useToggle();
useEffect((): void => {
const isExternal = currentPair?.meta.isExternal || false;
const isHardware = currentPair?.meta.isHardware || false;
const isInjected = currentPair?.meta.isInjected || false;
const meta = (currentPair && currentPair.meta) || {};
const isExternal = meta.isExternal || false;
const isHardware = meta.isHardware || false;
const isInjected = meta.isInjected || false;
const isUsable = !(isExternal || isHardware || isInjected);
setAccountState({
@ -54,18 +55,16 @@ function Sign ({ className }: Props): React.ReactElement<Props> {
setIsLocked(
isInjected
? false
: currentPair?.isLocked || false
: (currentPair && currentPair.isLocked) || false
);
setSignature('');
setSigner({ isUsable, signer: null });
// for injected, retrieve the signer
if (currentPair && isInjected) {
const { meta: { source } } = currentPair;
web3FromSource(source)
if (meta.source && isInjected) {
web3FromSource(meta.source)
.catch((): null => null)
.then((injected): void => setSigner({
.then((injected) => setSigner({
isUsable: isFunction(injected?.signer?.signRaw),
signer: injected?.signer || null
}));
@ -88,7 +87,7 @@ function Sign ({ className }: Props): React.ReactElement<Props> {
return;
}
if (signer?.signRaw) {
if (signer && isFunction(signer.signRaw)) {
setSignature('');
signer
@ -99,7 +98,7 @@ function Sign ({ className }: Props): React.ReactElement<Props> {
: stringToHex(data),
type: 'bytes'
})
.then(({ signature }): void => setSignature(signature));
.then(({ signature }) => setSignature(signature));
} else {
setSignature(u8aToHex(
currentPair.sign(

View File

@ -23,7 +23,7 @@ function Unlock ({ onClose, onUnlock, pair }: Props): React.ReactElement<Props>
useEffect((): void => {
setAddress(pair?.address || '');
}, [pair]);
}, [pair?.address]);
useEffect((): void => {
setUnlockError(null);

View File

@ -15,7 +15,7 @@ export default function useCounter (): number {
useEffect((): void => {
setCounter(proposals?.proposals.length || 0);
}, [proposals]);
}, [proposals?.proposals.length]);
return counter;
}

View File

@ -32,7 +32,7 @@ function VoteValue ({ accountId, autoFocus, onChange }: Props): React.ReactEleme
useEffect((): void => {
// if the set accountId changes and the new balances is for that id, set it
(accountId !== selectedId) && allBalances?.accountId.eq(accountId) && setValue({
(accountId !== selectedId) && allBalances && allBalances.accountId.eq(accountId) && setValue({
selectedId: accountId,
value: allBalances.lockedBalance
});

View File

@ -111,7 +111,7 @@ export default function useAccountInfo (value: string): UseAccountInfo {
setMeta(accountOrAddress?.meta);
setName(accountOrAddress?.meta.name || '');
setSortedTags(accountOrAddress?.meta.tags ? accountOrAddress.meta.tags.sort() : []);
}, [identity, isAccount, isAddress, value]);
}, [identity?.display, isAccount, isAddress, value]);
const onSaveName = useCallback(
(): void => {

View File

@ -18,27 +18,35 @@ interface Details {
type?: string;
}
interface RawModuleError {
value: DispatchError;
}
function isModuleError (defaultValue?: { value: any }): defaultValue is RawModuleError {
return !!defaultValue?.value?.isModule;
}
function ErrorDisplay (props: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [{ details, type }, setDetails] = useState<Details>({});
useEffect((): void => {
if (details !== null && props.defaultValue?.value?.isModule) {
if (isModuleError(props.defaultValue)) {
try {
const { documentation, name, section } = registry.findMetaError((props.defaultValue.value as DispatchError).asModule);
const { documentation, name, section } = registry.findMetaError(props.defaultValue.value.asModule);
setDetails({
return setDetails({
details: documentation.join(', '),
type: `${section}.${name}`
});
} catch (error) {
// Errors may not actually be exposed, in this case, just return the default representation
console.error(error);
setDetails({ details: null });
}
}
}, [details, props.defaultValue]);
setDetails({ details: null });
}, [props.defaultValue]);
if (!props.isDisabled || !details) {
return <Unknown {...props} />;

View File

@ -63,7 +63,7 @@ function BlockToTime ({ blocks, children, className, label, style }: Props): Rea
api.consts.timestamp?.minimumPeriod.muln(2) ||
DEFAULT_TIME
).divn(1000);
const time = extractTime(blocks?.mul(blockTime).toNumber());
const time = extractTime(blocks && blocks.mul(blockTime).toNumber());
return [
time[0] ? (time[0] > 1) ? t('{{d}} days', { replace: { d: time[0] } }) : t('1 day') : null,

View File

@ -10,7 +10,7 @@ import React, { useState, useEffect } from 'react';
import { Trans } from 'react-i18next';
import { Expander } from '@polkadot/react-components';
import { useApi } from '@polkadot/react-hooks';
import { formatBalance } from '@polkadot/util';
import { formatBalance, isFunction } from '@polkadot/util';
interface Props {
accountId?: string | null;
@ -26,7 +26,7 @@ function Checks ({ accountId, className, extrinsic }: Props): React.ReactElement
const [dispatchInfo, setDispatchInfo] = useState<RuntimeDispatchInfo | null>(null);
useEffect((): void => {
accountId && extrinsic?.paymentInfo && api.rpc.payment?.queryInfo &&
accountId && extrinsic && isFunction(api.rpc.payment?.queryInfo) &&
extrinsic
.paymentInfo(accountId)
.then(setDispatchInfo);