Skip to content

Commit

Permalink
Bump API (incl. staking ledger fixes) (polkadot-js#2156)
Browse files Browse the repository at this point in the history
* Bump API (incl. staking ledger fixes)

* Api context

* Replace counter

* Linting

* Swap t -> useTranslation

* Cleanup unwrap

* Rebuild i18n (& make auto-config)

* Remove old eslint override
  • Loading branch information
jacogr authored and jordy25519 committed Feb 20, 2020
1 parent 58b9626 commit f28f8e4
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 170 deletions.
89 changes: 33 additions & 56 deletions i18next-scanner.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
// Copyright 2017-2020 @polkadot/apps authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

const fs = require('fs');
const path = require('path');
const typescript = require('typescript');

const findPackages = require('./scripts/findPackages');

function transform (file, enc, done) {
const { ext } = path.parse(file.path);

if (ext === '.tsx') {
const content = fs.readFileSync(file.path, enc);

const { outputText } = typescript.transpileModule(content, {
compilerOptions: {
target: 'es2018'
},
fileName: path.basename(file.path)
});

const parserHandler = (key, options) => {
options.defaultValue = key;
options.ns = /packages\/(.*?)\/src/g.exec(file.path)[1];
this.parser.set(key, options);
};

this.parser.parseFuncFromString(outputText, parserHandler);
}

done();
}

module.exports = {
input: [
'packages/*/src/**/*.{ts,tsx}',
Expand All @@ -22,38 +53,7 @@ module.exports = {
},
lngs: ['en'],
defaultLng: 'en',
ns: [
'app-123code',
'app-accounts',
'app-address-book',
'app-claims',
'app-contracts',
'app-council',
'app-dashboard',
'app-democracy',
'app-explorer',
'app-extrinsics',
'app-generic-asset',
'app-js',
'app-parachains',
'app-settings',
'app-society',
'app-staking',
'app-storage',
'app-sudo',
'app-tech-comm',
'app-toolbox',
'app-transfer',
'app-treasury',
'apps',
'apps-routing',
'react-api',
'react-components',
'react-params',
'react-query',
'react-signer',
'ui'
],
ns: findPackages().map(({ dir }) => dir),
defaultNs: 'ui',
resource: {
loadPath: 'packages/apps/public/locales/{{lng}}/{{ns}}.json',
Expand All @@ -64,28 +64,5 @@ module.exports = {
nsSeparator: false, // namespace separator
keySeparator: false // key separator
},
transform: function transform (file, enc, done) {
const { ext } = path.parse(file.path);

if (ext === '.tsx') {
const content = fs.readFileSync(file.path, enc);

const { outputText } = typescript.transpileModule(content, {
compilerOptions: {
target: 'es2018'
},
fileName: path.basename(file.path)
});

const parserHandler = (key, options) => {
options.defaultValue = key;
options.ns = /packages\/(.*?)\/src/g.exec(file.path)[1];
this.parser.set(key, options);
};

this.parser.parseFuncFromString(outputText, parserHandler);
}

done();
}
transform
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"packages/*"
],
"resolutions": {
"@polkadot/api": "^1.0.0-beta.12",
"@polkadot/api-contract": "^1.0.0-beta.12",
"@polkadot/api": "^1.0.0-beta.13",
"@polkadot/api-contract": "^1.0.0-beta.13",
"@polkadot/keyring": "^2.0.0-beta.4",
"@polkadot/types": "^1.0.0-beta.12",
"@polkadot/types": "^1.0.0-beta.13",
"@polkadot/util": "^2.0.0-beta.4",
"@polkadot/util-crypto": "^2.0.0-beta.4",
"babel-core": "^7.0.0-bridge.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/app-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime": "^7.7.7",
"@polkadot/api-contract": "^1.0.0-beta.12"
"@polkadot/api-contract": "^1.0.0-beta.13"
}
}
3 changes: 0 additions & 3 deletions packages/app-society/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import { AppProps, BareProps } from '@polkadot/react-components/types';
import React from 'react';
import { Tabs } from '@polkadot/react-components';

import useCheck from './useCheck';
import Overview from './Overview';
import { useTranslation } from './translate';

export { useCheck };

interface Props extends AppProps, BareProps {}

export default function SocietyApp ({ basePath, className }: Props): React.ReactElement<Props> {
Expand Down
21 changes: 0 additions & 21 deletions packages/app-society/src/useCheck.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { I18nProps } from '@polkadot/react-components/types';
import { AccountId, StakingLedger } from '@polkadot/types/interfaces';

import React, { useEffect, useState } from 'react';
import { Icon } from '@polkadot/react-components';
import { Option } from '@polkadot/types';
import { useApi, useCall } from '@polkadot/react-hooks';

import translate from '../../translate';
import { useTranslation } from '../../translate';

interface Props extends I18nProps {
interface Props {
accountId: string | null;
controllerId: string | null;
defaultController?: string;
Expand All @@ -22,25 +21,20 @@ interface Props extends I18nProps {

const DISTINCT = 'Distinct stash and controller accounts are recommended to ensure fund security.';

function ValidateController ({ accountId, controllerId, defaultController, isUnsafeChain, onError, t }: Props): React.ReactElement<Props> | null {
export default function ValidateController ({ accountId, controllerId, defaultController, isUnsafeChain, onError }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { api } = useApi();
const bondedId = useCall<string | null>(api.query.staking.bonded, [controllerId], {
transform: (value: Option<AccountId>): string | null => {
const extracted = value.unwrapOr(null);

return extracted
? extracted.toString()
: null;
}
transform: (value: Option<AccountId>): string | null =>
value.isSome
? value.unwrap().toString()
: null
});
const stashId = useCall<string | null>(api.query.staking.ledger, [controllerId], {
transform: (value: Option<StakingLedger>): string | null => {
const extracted = value.unwrapOr({ stash: null }).stash;

return extracted
? extracted.toString()
: null;
}
const stashId = useCall<string | null>(controllerId ? api.query.staking.ledger : null, [controllerId], {
transform: (value: Option<StakingLedger>): string | null =>
value.isSome
? value.unwrap().stash.toString()
: null
});
const [error, setError] = useState<string | null>(null);

Expand Down Expand Up @@ -77,5 +71,3 @@ function ValidateController ({ accountId, controllerId, defaultController, isUns
</article>
);
}

export default translate(ValidateController);
3 changes: 1 addition & 2 deletions packages/apps-routing/src/society.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

import { Routes } from './types';

import Society, { useCheck } from '@polkadot/app-society';
import Society from '@polkadot/app-society';

export default ([
{
Component: Society,
useCheck,
display: {
needsApi: [
'query.society.pot'
Expand Down
1 change: 1 addition & 0 deletions packages/apps/public/locales/en/react-hooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
8 changes: 3 additions & 5 deletions packages/apps/src/SideBar/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { isFunction } from '@polkadot/util';

import { useTranslation } from '../translate';

const DUMMY_CHECK = (): boolean => true;
const DUMMY_COUNTER = (): number => 0;

interface Props {
Expand Down Expand Up @@ -74,23 +73,22 @@ function checkVisible (name: string, { api, isApiReady, isApiConnected }: ApiPro
return notFound.length === 0;
}

export default function Item ({ route: { Modal, useCheck = DUMMY_CHECK, useCounter = DUMMY_COUNTER, display, i18n, icon, name }, isCollapsed, onClick }: Props): React.ReactElement<Props> | null {
export default function Item ({ route: { Modal, useCounter = DUMMY_COUNTER, display, i18n, icon, name }, isCollapsed, onClick }: Props): React.ReactElement<Props> | null {
const { t } = useTranslation();
const { allAccounts, hasAccounts } = useAccounts();
const apiProps = useApi();
const sudoKey = useCall<AccountId>(apiProps.isApiReady ? apiProps.api.query.sudo?.key : undefined, []);
const [hasSudo, setHasSudo] = useState(false);
const [isVisible, setIsVisible] = useState(false);
const checkState = useCheck();
const count = useCounter();

useEffect((): void => {
setHasSudo(!!sudoKey && allAccounts.some((address): boolean => sudoKey.eq(address)));
}, [allAccounts, sudoKey]);

useEffect((): void => {
setIsVisible(checkState && checkVisible(name, apiProps, hasAccounts, hasSudo, display));
}, [apiProps, checkState, hasAccounts, hasSudo]);
setIsVisible(checkVisible(name, apiProps, hasAccounts, hasSudo, display));
}, [apiProps, hasAccounts, hasSudo]);

if (!isVisible) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"homepage": "https://github.com/polkadot-js/ui/tree/master/packages/ui-reactive#readme",
"dependencies": {
"@babel/runtime": "^7.7.6",
"@polkadot/api": "^1.0.0-beta.12",
"@polkadot/api": "^1.0.0-beta.13",
"@polkadot/extension-dapp": "^0.15.0-beta.1",
"edgeware-node-types": "^1.0.10",
"rxjs-compat": "^6.5.3"
Expand Down
16 changes: 9 additions & 7 deletions packages/react-api/src/Api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ export default function Api ({ children, queuePayload, queueSetTxStatus, url }:
setIsApiLoading(false);
}, []);

return api
? (
<ApiContext.Provider value={{ ...state, api, isApiConnected, isApiLoading, isWaitingInjected }}>
{children}
</ApiContext.Provider>
)
: null;
if (isApiLoading) {
return null;
}

return (
<ApiContext.Provider value={{ ...state, api, isApiConnected, isWaitingInjected }}>
{children}
</ApiContext.Provider>
);
}
1 change: 0 additions & 1 deletion packages/react-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface ApiProps extends ApiState {
api: ApiPromise;
isWaitingInjected: boolean;
isApiConnected: boolean;
isApiLoading: boolean;
}

export interface OnChangeCbObs {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-hooks/src/useCall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function subscribe <T> (mounted: MountedRef, tracker: TrackerRef, fn: TrackFn |
// - returns a promise with an unsubscribe function
// - has a callback to set the value
// FIXME The typings here need some serious TLC
export default function useCall <T> (fn: TrackFn | undefined, params: CallParams, options: CallOptions<T> = {}): T | undefined {
export default function useCall <T> (fn: TrackFn | undefined | null, params: CallParams, options: CallOptions<T> = {}): T | undefined {
const mounted = useIsMountedRef();
const tracker = useRef<Tracker>({ isActive: false, count: 0, serialized: null, subscriber: null });
const [value, setValue] = useState<T | undefined>(options.defaultValue);
Expand Down
3 changes: 1 addition & 2 deletions packages/react-query/src/Bonded.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable @typescript-eslint/camelcase */
// Copyright 2017-2020 @polkadot/react-query authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
Expand All @@ -23,7 +22,7 @@ export default function BondedDisplay ({ children, className, label, params }: P
transform: (value): AccountId | null =>
value.unwrapOr(null)
});
const stakingLedger = useCall<StakingLedger | null>(api.query.staking.ledger, [controllerId], {
const stakingLedger = useCall<StakingLedger | null>(controllerId && api.query.staking.ledger, [controllerId], {
transform: (value): StakingLedger | null =>
value.unwrapOr(null)
});
Expand Down
Loading

0 comments on commit f28f8e4

Please sign in to comment.