Skip to content

Commit

Permalink
Expose useChainInfo (re-use of some info for signer) (#2460)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr authored Mar 26, 2020
1 parent 4cd8e31 commit e2bbfb4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
23 changes: 6 additions & 17 deletions packages/page-settings/src/Extensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions packages/page-settings/src/useChainInfo.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e2bbfb4

Please sign in to comment.