Extract derive.accounts.* into own hooks (#6755)
* Extract derive.accounts.* into own hooks * useAccountApi as applicable * Rename step 1 * useSystemApi
This commit is contained in:
parent
1dcb74b967
commit
a7a48c1140
@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/app-addresses authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import type { ActionStatus } from '@polkadot/react-components/Status/types';
|
||||
import type { ThemeDef } from '@polkadot/react-components/types';
|
||||
import type { KeyringAddress } from '@polkadot/ui-keyring/types';
|
||||
@ -11,7 +10,7 @@ import styled, { ThemeContext } from 'styled-components';
|
||||
|
||||
import Transfer from '@polkadot/app-accounts/modals/Transfer';
|
||||
import { AddressInfo, AddressSmall, Button, ChainLock, ExpandButton, Forget, Icon, LinkExternal, Menu, Popup, Tags } from '@polkadot/react-components';
|
||||
import { useApi, useBalancesAll, useCall, useToggle } from '@polkadot/react-hooks';
|
||||
import { useApi, useBalancesAll, useDeriveAccountInfo, useToggle } from '@polkadot/react-hooks';
|
||||
import { keyring } from '@polkadot/ui-keyring';
|
||||
import { BN_ZERO, formatNumber, isFunction } from '@polkadot/util';
|
||||
|
||||
@ -31,7 +30,7 @@ function Address ({ address, className = '', filter, isFavorite, toggleFavorite
|
||||
const { t } = useTranslation();
|
||||
const { theme } = useContext(ThemeContext as React.Context<ThemeDef>);
|
||||
const api = useApi();
|
||||
const info = useCall<DeriveAccountInfo>(api.api.derive.accounts.info, [address]);
|
||||
const info = useDeriveAccountInfo(address);
|
||||
const balancesAll = useBalancesAll(address);
|
||||
const [tags, setTags] = useState<string[]>([]);
|
||||
const [accName, setAccName] = useState('');
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type BN from 'bn.js';
|
||||
import type { DeriveAccountInfo, DeriveHeartbeatAuthor } from '@polkadot/api-derive/types';
|
||||
import type { DeriveHeartbeatAuthor } from '@polkadot/api-derive/types';
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { SlashingSpans, ValidatorPrefs } from '@polkadot/types/interfaces';
|
||||
import type { NominatedBy as NominatedByType, ValidatorInfo } from '../../types';
|
||||
@ -13,7 +13,7 @@ import React, { useMemo } from 'react';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { AddressSmall, Icon, LinkExternal } from '@polkadot/react-components';
|
||||
import { checkVisibility } from '@polkadot/react-components/util';
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { useApi, useCall, useDeriveAccountInfo } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
@ -78,7 +78,7 @@ const transformSlashes = {
|
||||
|
||||
function useAddressCalls (api: ApiPromise, address: string, isMain?: boolean) {
|
||||
const params = useMemo(() => [address], [address]);
|
||||
const accountInfo = useCall<DeriveAccountInfo>(api.derive.accounts.info, params);
|
||||
const accountInfo = useDeriveAccountInfo(address);
|
||||
const slashingSpans = useCall<SlashingSpans | null>(!isMain && api.query.staking.slashingSpans, params, transformSlashes);
|
||||
|
||||
return { accountInfo, slashingSpans };
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type BN from 'bn.js';
|
||||
import type { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import type { UnappliedSlash } from '@polkadot/types/interfaces';
|
||||
import type { NominatedBy, ValidatorInfo } from '../types';
|
||||
|
||||
@ -10,7 +9,7 @@ import React, { useCallback, useMemo } from 'react';
|
||||
|
||||
import { AddressSmall, Badge, Checkbox, Icon } from '@polkadot/react-components';
|
||||
import { checkVisibility } from '@polkadot/react-components/util';
|
||||
import { useApi, useBlockTime, useCall } from '@polkadot/react-hooks';
|
||||
import { useApi, useBlockTime, useDeriveAccountInfo } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -37,7 +36,7 @@ function queryAddress (address: string): void {
|
||||
function Validator ({ allSlashes, canSelect, filterName, info, isNominated, isSelected, nominatedBy = [], toggleFavorite, toggleSelected }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const accountInfo = useCall<DeriveAccountInfo>(api.derive.accounts.info, [info.accountId]);
|
||||
const accountInfo = useDeriveAccountInfo(info.accountId);
|
||||
const [,, time] = useBlockTime(info.lastPayout);
|
||||
|
||||
const isVisible = useMemo(
|
||||
|
||||
@ -7,7 +7,7 @@ import type { AccountId, Address } from '@polkadot/types/interfaces';
|
||||
import React, { useMemo } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { useDeriveAccountInfo, useSystemApi } from '@polkadot/react-hooks';
|
||||
|
||||
interface Props {
|
||||
children?: React.ReactNode;
|
||||
@ -24,8 +24,8 @@ function extractIndex ({ accountIndex }: Partial<DeriveAccountInfo> = {}): strin
|
||||
}
|
||||
|
||||
function AccountIndex ({ children, className = '', defaultValue, label, value }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
const info = useCall<DeriveAccountInfo>(api.derive.accounts.info, [value]);
|
||||
const api = useSystemApi();
|
||||
const info = useDeriveAccountInfo(value);
|
||||
|
||||
const accountIndex = useMemo(
|
||||
() => extractIndex(info),
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { IconName } from '@fortawesome/fontawesome-svg-core';
|
||||
import type { DeriveAccountInfo, DeriveAccountRegistration } from '@polkadot/api-derive/types';
|
||||
import type { DeriveAccountRegistration } from '@polkadot/api-derive/types';
|
||||
import type { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useCallback, useContext, useEffect, useState } from 'react';
|
||||
@ -10,7 +10,7 @@ import styled from 'styled-components';
|
||||
|
||||
import { AccountSidebarToggle } from '@polkadot/app-accounts/Sidebar';
|
||||
import registry from '@polkadot/react-api/typeRegistry';
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { useDeriveAccountInfo, useSystemApi } from '@polkadot/react-hooks';
|
||||
import { isFunction, stringToU8a } from '@polkadot/util';
|
||||
|
||||
import Badge from './Badge';
|
||||
@ -131,8 +131,8 @@ function extractIdentity (address: string, identity: DeriveAccountRegistration):
|
||||
}
|
||||
|
||||
function AccountName ({ children, className = '', defaultName, label, onClick, override, toggle, value, withSidebar }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const info = useCall<DeriveAccountInfo>(api.derive.accounts.info, [value]);
|
||||
const api = useSystemApi();
|
||||
const info = useDeriveAccountInfo(value);
|
||||
const [name, setName] = useState<React.ReactNode>(() => extractName((value || '').toString(), undefined, defaultName));
|
||||
const toggleSidebar = useContext(AccountSidebarToggle);
|
||||
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
// Copyright 2017-2021 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
|
||||
import React, { useCallback, useMemo } from 'react';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { useApi, useCall } from '@polkadot/react-hooks';
|
||||
import { useApi, useDeriveAccountInfo } from '@polkadot/react-hooks';
|
||||
|
||||
import AddressMini from './AddressMini';
|
||||
import Toggle from './Toggle';
|
||||
@ -24,7 +22,7 @@ interface Props {
|
||||
|
||||
function AddressToggle ({ address, className = '', filter, isHidden, noToggle, onChange, value }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
const info = useCall<DeriveAccountInfo>(api.derive.accounts.info, [address]);
|
||||
const info = useDeriveAccountInfo(address);
|
||||
|
||||
const isVisible = useMemo(
|
||||
() => info ? checkVisibility(api, address, info, filter, false) : true,
|
||||
|
||||
@ -22,6 +22,9 @@ export { useCollectiveMembers } from './useCollectiveMembers';
|
||||
export { useFormField } from './useFormField';
|
||||
export { useDebounce } from './useDebounce';
|
||||
export { useDelegations } from './useDelegations';
|
||||
export { useSystemApi } from './useSystemApi';
|
||||
export { useDeriveAccountFlags } from './useDeriveAccountFlags';
|
||||
export { useDeriveAccountInfo } from './useDeriveAccountInfo';
|
||||
export { useEventTrigger } from './useEventTrigger';
|
||||
export { useExtrinsicTrigger } from './useExtrinsicTrigger';
|
||||
export { useFavorites } from './useFavorites';
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveAccountFlags, DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import type { Nominations, ValidatorPrefs } from '@polkadot/types/interfaces';
|
||||
import type { KeyringJson$Meta } from '@polkadot/ui-keyring/types';
|
||||
import type { AddressFlags, AddressIdentity, UseAccountInfo } from './types';
|
||||
@ -16,6 +15,8 @@ import { useAccounts } from './useAccounts';
|
||||
import { useAddresses } from './useAddresses';
|
||||
import { useApi } from './useApi';
|
||||
import { useCall } from './useCall';
|
||||
import { useDeriveAccountFlags } from './useDeriveAccountFlags';
|
||||
import { useDeriveAccountInfo } from './useDeriveAccountInfo';
|
||||
import { useToggle } from './useToggle';
|
||||
|
||||
const IS_NONE = {
|
||||
@ -42,8 +43,8 @@ function useAccountInfoImpl (value: string | null, isContract = false): UseAccou
|
||||
const { api } = useApi();
|
||||
const { isAccount } = useAccounts();
|
||||
const { isAddress } = useAddresses();
|
||||
const accountInfo = useCall<DeriveAccountInfo>(api.derive.accounts.info, [value]);
|
||||
const accountFlags = useCall<DeriveAccountFlags>(api.derive.accounts.flags, [value]);
|
||||
const accountInfo = useDeriveAccountInfo(value);
|
||||
const accountFlags = useDeriveAccountFlags(value);
|
||||
const nominator = useCall<Nominations>(api.query.staking?.nominators, [value]);
|
||||
const validator = useCall<ValidatorPrefs>(api.query.staking?.validators, [value]);
|
||||
const [accountIndex, setAccountIndex] = useState<string | undefined>(undefined);
|
||||
@ -242,7 +243,7 @@ function useAccountInfoImpl (value: string | null, isContract = false): UseAccou
|
||||
);
|
||||
|
||||
const setTags = useCallback(
|
||||
(tags: string[]): void => setSortedTags(tags.sort()),
|
||||
(tags: string[]) => setSortedTags(tags.sort()),
|
||||
[]
|
||||
);
|
||||
|
||||
|
||||
17
packages/react-hooks/src/useDeriveAccountFlags.ts
Normal file
17
packages/react-hooks/src/useDeriveAccountFlags.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveAccountFlags } from '@polkadot/api-derive/types';
|
||||
import type { AccountId, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useCall } from './useCall';
|
||||
import { useSystemApi } from './useSystemApi';
|
||||
|
||||
function useDeriveAccountFlagsImpl (value?: AccountId | Address | Uint8Array | string | null): DeriveAccountFlags | undefined {
|
||||
const api = useSystemApi();
|
||||
|
||||
return useCall<DeriveAccountFlags>(api.derive.accounts.flags, [value]);
|
||||
}
|
||||
|
||||
export const useDeriveAccountFlags = createNamedHook('useDeriveAccountFlags', useDeriveAccountFlagsImpl);
|
||||
17
packages/react-hooks/src/useDeriveAccountInfo.ts
Normal file
17
packages/react-hooks/src/useDeriveAccountInfo.ts
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import type { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useCall } from './useCall';
|
||||
import { useSystemApi } from './useSystemApi';
|
||||
|
||||
function useDeriveAccountInfoImpl (value?: AccountId | AccountIndex | Address | Uint8Array | string | null): DeriveAccountInfo | undefined {
|
||||
const api = useSystemApi();
|
||||
|
||||
return useCall<DeriveAccountInfo>(api.derive.accounts.info, [value]);
|
||||
}
|
||||
|
||||
export const useDeriveAccountInfo = createNamedHook('useDeriveAccountInfo', useDeriveAccountInfoImpl);
|
||||
13
packages/react-hooks/src/useSystemApi.ts
Normal file
13
packages/react-hooks/src/useSystemApi.ts
Normal file
@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@polkadot/api';
|
||||
|
||||
import { createNamedHook } from './createNamedHook';
|
||||
import { useApi } from './useApi';
|
||||
|
||||
function useSystemApiImpl (): ApiPromise {
|
||||
return useApi().api;
|
||||
}
|
||||
|
||||
export const useSystemApi = createNamedHook('useSystemApi', useSystemApiImpl);
|
||||
Loading…
Reference in New Issue
Block a user