Skip to content

Commit

Permalink
Only hide extension updates in settings on dev (polkadot-js#3678)
Browse files Browse the repository at this point in the history
* Only hide extension updates in settings on dev

* Adjust color indicator for Centrifuge
  • Loading branch information
jacogr authored Sep 14, 2020
1 parent 65cd3c7 commit f8ee5b8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/apps-config/src/ui/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const chainColors: Record<string, any> = [
// alphabetical
const nodeColors: Record<string, any> = [
['centrifuge chain', nodeCentrifuge],
['Centrifuge Chain Node', nodeCentrifuge],
['edgeware node', nodeEdgeware],
['kilt node', nodeKilt],
['nodle chain node', nodeNodle],
Expand Down
4 changes: 2 additions & 2 deletions packages/apps-config/src/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function getSystemIcon (systemName: string): 'beachball' | 'polkadot' | '
return (identityNodes[systemName.toLowerCase().replace(/-/g, ' ')] || 'substrate') as 'substrate';
}

export const getSystemChainColor = (systemChain: string, systemName: string): string | undefined => {
export function getSystemChainColor (systemChain: string, systemName: string): string | undefined {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return chainColors[sanitize(systemChain)] || nodeColors[sanitize(systemName)];
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface Props {
className?: string;
}

function ChainColorIndicator ({ className = '', color }: Props): React.ReactElement<Props> {
function ChainColorIndicator ({ className, color }: Props): React.ReactElement<Props> {
return (
<div
className={className}
Expand Down
12 changes: 9 additions & 3 deletions packages/page-settings/src/Metadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import React from 'react';
import useChainInfo from '../useChainInfo';
import { useApi } from '@polkadot/react-hooks';

import { useTranslation } from '../translate';
import useChainInfo from '../useChainInfo';
import Extensions from './Extensions';
import NetworkSpecs from './NetworkSpecs';

export default function Metadata (): React.ReactElement {
const { t } = useTranslation();
const { isDevelopment } = useApi();
const chainInfo = useChainInfo();

return (
<>
<h1>{t<string>('Extensions')}</h1>
<Extensions chainInfo={chainInfo} />
{!isDevelopment && (
<>
<h1>{t<string>('Extensions')}</h1>
<Extensions chainInfo={chainInfo} />
</>
)}
<h1>{t<string>('Chain specifications')}</h1>
<NetworkSpecs chainInfo={chainInfo} />
</>
Expand Down
8 changes: 3 additions & 5 deletions packages/page-settings/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export { useCounter };

function SettingsApp ({ basePath, onStatusChange }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { isApiConnected, isApiReady, isDevelopment } = useApi();
const { isApiConnected, isApiReady } = useApi();
const numExtensions = useCounter();

const items = useMemo(() => [
Expand All @@ -47,11 +47,9 @@ function SettingsApp ({ basePath, onStatusChange }: Props): React.ReactElement<P

const hidden = useMemo(
() => (isApiConnected && isApiReady)
? isDevelopment
? ['metadata']
: []
? []
: ['metadata', 'i18n'],
[isApiConnected, isApiReady, isDevelopment]
[isApiConnected, isApiReady]
);

return (
Expand Down
38 changes: 19 additions & 19 deletions packages/page-settings/src/useChainInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@

import { ChainInfo } from './types';

import { useMemo } from 'react';

import { getSystemChainColor, getSystemIcon } from '@polkadot/apps-config/ui';
import { getSpecTypes } from '@polkadot/types-known';
import { registry } from '@polkadot/react-api';
import { useApi } from '@polkadot/react-hooks';
import { isNumber } from '@polkadot/util';

import { useEffect, useState } from 'react';

export default function useChainInfo (): ChainInfo | null {
const { api, isApiReady, systemChain, systemName } = useApi();
const [state, setState] = useState<ChainInfo | null>(null);

useEffect((): void => {
isApiReady && setState({
chain: systemChain,
color: getSystemChainColor(systemChain, systemName),
genesisHash: api.genesisHash.toHex(),
icon: getSystemIcon(systemName),
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
specVersion: api.runtimeVersion.specVersion.toNumber(),
ss58Format: isNumber(api.registry.chainSS58) ? api.registry.chainSS58 : 42,
tokenDecimals: isNumber(api.registry.chainDecimals) ? api.registry.chainDecimals : 12,
tokenSymbol: api.registry.chainToken || 'Unit',
types: getSpecTypes(registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>
});
}, [api, isApiReady, systemChain, systemName]);

return state;
return useMemo(
() => isApiReady
? {
chain: systemChain,
color: getSystemChainColor(systemChain, systemName),
genesisHash: api.genesisHash.toHex(),
icon: getSystemIcon(systemName),
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
specVersion: api.runtimeVersion.specVersion.toNumber(),
ss58Format: isNumber(api.registry.chainSS58) ? api.registry.chainSS58 : 42,
tokenDecimals: isNumber(api.registry.chainDecimals) ? api.registry.chainDecimals : 12,
tokenSymbol: api.registry.chainToken || 'Unit',
types: getSpecTypes(registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>
}
: null,
[api, isApiReady, systemChain, systemName]
);
}
25 changes: 13 additions & 12 deletions packages/page-settings/src/useExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { InjectedExtension, InjectedMetadataKnown, MetadataDef } from '@polkadot/extension-inject/types';

import { useEffect, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import store from 'store';
import { ApiPromise } from '@polkadot/api';
import { registry } from '@polkadot/react-api';
Expand All @@ -22,7 +22,7 @@ interface ExtensionInfo extends ExtensionKnown {

interface Extensions {
count: number;
extensions?: ExtensionInfo[];
extensions: ExtensionInfo[];
}

interface ExtensionProperties {
Expand Down Expand Up @@ -131,18 +131,20 @@ async function getExtensionInfo (api: ApiPromise, extension: InjectedExtension):
}
}

async function getKnown (api: ApiPromise, extensions: InjectedExtension[]): Promise<ExtensionKnown[]> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function getKnown (api: ApiPromise, extensions: InjectedExtension[], _: number): Promise<ExtensionKnown[]> {
const all = await Promise.all(
extensions.map((extension) => getExtensionInfo(api, extension))
);

return all.filter((info): info is ExtensionKnown => !!info);
}

const EMPTY_STATE = { count: 0, extensions: [] };

export default function useExtensions (): Extensions {
const { api, extensions, isApiReady, isDevelopment } = useApi();
const [all, setAll] = useState<ExtensionKnown[] | undefined>();
const [state, setState] = useState<Extensions>({ count: 0 });
const [trigger, setTrigger] = useState(0);

useEffect((): () => void => {
Expand All @@ -156,14 +158,13 @@ export default function useExtensions (): Extensions {
}, []);

useEffect((): void => {
extensions && getKnown(api, extensions).then(setAll);
extensions && getKnown(api, extensions, trigger).then(setAll);
}, [api, extensions, trigger]);

useEffect((): void => {
isDevelopment
? setState({ count: 0, extensions: [] })
: isApiReady && all && setState(filterAll(api, all));
}, [all, api, isApiReady, isDevelopment]);

return state;
return useMemo(
() => isDevelopment || !isApiReady || !all
? EMPTY_STATE
: filterAll(api, all),
[all, api, isApiReady, isDevelopment]
);
}

0 comments on commit f8ee5b8

Please sign in to comment.