Expose useChainInfo (re-use of some info for signer) (#2460)

This commit is contained in:
Jaco Greeff 2020-03-26 14:01:12 +01:00 committed by GitHub
parent 4cd8e312bd
commit e2bbfb45e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 17 deletions

View File

@ -3,17 +3,16 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import React, { useCallback, useMemo, useState } from 'react';
import { getChainTypes } from '@polkadot/apps-config/api';
import { getSystemIcon } from '@polkadot/apps-config/ui';
import { Button, Dropdown } from '@polkadot/react-components';
import { useApi, useToggle } from '@polkadot/react-hooks';
import { useToggle } from '@polkadot/react-hooks';
import { useTranslation } from './translate';
import useChainInfo from './useChainInfo';
import useExtensions from './useExtensions';
function Extensions (): React.ReactElement {
const { api, systemChain, systemName } = useApi();
const { t } = useTranslation();
const metaDef = useChainInfo();
const { extensions } = useExtensions();
const [selectedIndex, setSelectedIndex] = useState(0);
const [isBusy, toggleBusy] = useToggle();
@ -23,26 +22,16 @@ function Extensions (): React.ReactElement {
);
const _updateMeta = useCallback(
(): void => {
if (extensions && extensions[selectedIndex]) {
if (metaDef && extensions?.[selectedIndex]) {
toggleBusy();
extensions[selectedIndex]
.update({
chain: systemChain,
genesisHash: api.genesisHash.toHex(),
icon: getSystemIcon(systemName),
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
specVersion: api.runtimeVersion.specVersion.toNumber(),
ss58Format: api.registry.chainSS58 || 42,
tokenDecimals: api.registry.chainDecimals || 12,
tokenSymbol: api.registry.chainToken || 'Unit',
types: getChainTypes(api.runtimeVersion.specName.toString(), systemChain)
})
.update(metaDef)
.catch(() => false)
.then(() => toggleBusy());
}
},
[api, extensions, selectedIndex, systemChain, systemName]
[extensions, metaDef, selectedIndex]
);
if (!options.length) {

View File

@ -0,0 +1,34 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
// Copyright 2017-2020 @polkadot/app-settings authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataDef } from '@polkadot/extension-inject/types';
import { useEffect, useState } from 'react';
import { getChainTypes } from '@polkadot/apps-config/api';
import { getSystemIcon } from '@polkadot/apps-config/ui';
import { useApi } from '@polkadot/react-hooks';
interface ChainInfo extends MetadataDef {}
export default function useChainInfo (): ChainInfo | null {
const { api, isApiReady, systemChain, systemName } = useApi();
const [state, setState] = useState<MetadataDef | null>(null);
useEffect((): void => {
isApiReady && setState({
chain: systemChain,
genesisHash: api.genesisHash.toHex(),
icon: getSystemIcon(systemName),
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
specVersion: api.runtimeVersion.specVersion.toNumber(),
ss58Format: api.registry.chainSS58 || 42,
tokenDecimals: api.registry.chainDecimals || 12,
tokenSymbol: api.registry.chainToken || 'Unit',
types: getChainTypes(api.runtimeVersion.specName.toString(), systemChain)
});
}, [api, isApiReady, systemChain, systemName]);
return state;
}