App for runtime calls (#7811)
* App for runtime calls * Display typoe in outputs * Adjust for released * sorting * dedupe
This commit is contained in:
parent
889c6c0465
commit
1ad8c1bf5e
@ -24,6 +24,7 @@ import nfts from './nfts';
|
||||
import parachains from './parachains';
|
||||
import poll from './poll';
|
||||
import rpc from './rpc';
|
||||
import runtime from './runtime';
|
||||
import settings from './settings';
|
||||
import signing from './signing';
|
||||
import society from './society';
|
||||
@ -63,6 +64,7 @@ export default function create (t: TFunction): Routes {
|
||||
storage(t),
|
||||
extrinsics(t),
|
||||
rpc(t),
|
||||
runtime(t),
|
||||
signing(t),
|
||||
sudo(t),
|
||||
files(t),
|
||||
|
||||
20
packages/apps-routing/src/runtime.ts
Normal file
20
packages/apps-routing/src/runtime.ts
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2022 @polkadot/apps-routing authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { TFunction } from 'i18next';
|
||||
import type { Route } from './types';
|
||||
|
||||
import Component from '@polkadot/app-runtime';
|
||||
|
||||
export default function create (t: TFunction): Route {
|
||||
return {
|
||||
Component,
|
||||
display: {
|
||||
needsApi: []
|
||||
},
|
||||
group: 'developer',
|
||||
icon: 'arrows-to-circle',
|
||||
name: 'runtime',
|
||||
text: t('nav.runtime', 'Runtime calls', { ns: 'apps-routing' })
|
||||
};
|
||||
}
|
||||
@ -1 +1 @@
|
||||
# @polkadot/app-toolbox
|
||||
# @polkadot/app-rpc
|
||||
|
||||
@ -24,11 +24,11 @@ function Results ({ queue = [] }: Props): React.ReactElement<Props> | null {
|
||||
|
||||
return (
|
||||
<section className='rpc--Results'>
|
||||
{filtered.map(({ error, id, result, rpc: { method, section } }): React.ReactNode => (
|
||||
{filtered.map(({ error, id, result, rpc: { method, section, type } }): React.ReactNode => (
|
||||
<Output
|
||||
isError={!!error}
|
||||
key={id}
|
||||
label={`${id}: ${section}.${method}`}
|
||||
label={`${id}: ${section}.${method}: ${type}`}
|
||||
value={
|
||||
error
|
||||
? error.message
|
||||
|
||||
@ -23,7 +23,7 @@ function RpcApp ({ basePath }: Props): React.ReactElement<Props> {
|
||||
]);
|
||||
|
||||
return (
|
||||
<main className='toolbox--App'>
|
||||
<main className='rpc--App'>
|
||||
<Tabs
|
||||
basePath={basePath}
|
||||
items={itemsRef.current}
|
||||
|
||||
0
packages/page-runtime/.skip-build
Normal file
0
packages/page-runtime/.skip-build
Normal file
0
packages/page-runtime/.skip-npm
Normal file
0
packages/page-runtime/.skip-npm
Normal file
201
packages/page-runtime/LICENSE
Normal file
201
packages/page-runtime/LICENSE
Normal file
@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
1
packages/page-runtime/README.md
Normal file
1
packages/page-runtime/README.md
Normal file
@ -0,0 +1 @@
|
||||
# @polkadot/app-runtime
|
||||
19
packages/page-runtime/package.json
Normal file
19
packages/page-runtime/package.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"bugs": "https://github.com/polkadot-js/apps/issues",
|
||||
"homepage": "https://github.com/polkadot-js/apps/tree/master/packages/page-rpc#readme",
|
||||
"license": "Apache-2.0",
|
||||
"name": "@polkadot/app-runtime",
|
||||
"private": true,
|
||||
"repository": {
|
||||
"directory": "packages/page-runtime",
|
||||
"type": "git",
|
||||
"url": "https://github.com/polkadot-js/apps.git"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"version": "0.116.2-65-x",
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.18.6",
|
||||
"@polkadot/react-components": "^0.116.2-73-x"
|
||||
}
|
||||
}
|
||||
38
packages/page-runtime/src/Runtime/Results.tsx
Normal file
38
packages/page-runtime/src/Runtime/Results.tsx
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Result } from './types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import { Output } from '@polkadot/react-components';
|
||||
import valueToText from '@polkadot/react-params/valueToText';
|
||||
|
||||
interface Props {
|
||||
results: Result[];
|
||||
}
|
||||
|
||||
function Results ({ results }: Props): React.ReactElement<Props> | null {
|
||||
if (!results.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<section className='runtime--Results'>
|
||||
{results.map(({ def: { method, section, type }, error, id, result }): React.ReactNode => (
|
||||
<Output
|
||||
isError={!!error}
|
||||
key={id}
|
||||
label={`${id}: ${section}.${method}: ${type}`}
|
||||
value={
|
||||
error
|
||||
? error.message
|
||||
: <pre>{valueToText('', result)}</pre>
|
||||
}
|
||||
/>
|
||||
))}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Results);
|
||||
100
packages/page-runtime/src/Runtime/Selection.tsx
Normal file
100
packages/page-runtime/src/Runtime/Selection.tsx
Normal file
@ -0,0 +1,100 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ParamDef, RawParam } from '@polkadot/react-params/types';
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
|
||||
import React, { useCallback, useMemo, useState } from 'react';
|
||||
|
||||
import { Button, InputCalls } from '@polkadot/react-components';
|
||||
import Params from '@polkadot/react-params';
|
||||
import { getTypeDef } from '@polkadot/types/create';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
|
||||
interface Props {
|
||||
onSubmit: (call: DefinitionCallNamed, values: RawParam[]) => void;
|
||||
}
|
||||
|
||||
interface State {
|
||||
isValid: boolean;
|
||||
method: DefinitionCallNamed | null;
|
||||
values: RawParam[];
|
||||
}
|
||||
|
||||
function Selection ({ onSubmit }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const [{ isValid, method, values }, setState] = useState<State>({
|
||||
isValid: false,
|
||||
method: null,
|
||||
values: []
|
||||
});
|
||||
|
||||
const params = useMemo(
|
||||
() => method
|
||||
? method.params.map(({ name, type }): ParamDef => ({
|
||||
name,
|
||||
type: getTypeDef(type)
|
||||
}))
|
||||
: [],
|
||||
[method]
|
||||
);
|
||||
|
||||
const _nextState = useCallback(
|
||||
(newState: Partial<State>) => setState((prevState: State): State => {
|
||||
const { method = prevState.method, values = prevState.values } = newState;
|
||||
const isValid = values.reduce((isValid, value) => isValid && value.isValid === true, !!method && method.params.length <= values.length);
|
||||
|
||||
return {
|
||||
isValid,
|
||||
method,
|
||||
values
|
||||
};
|
||||
}),
|
||||
[]
|
||||
);
|
||||
|
||||
const _onChangeMethod = useCallback(
|
||||
(method: DefinitionCallNamed) => _nextState({ method, values: [] }),
|
||||
[_nextState]
|
||||
);
|
||||
|
||||
const _onChangeValues = useCallback(
|
||||
(values: RawParam[]) => _nextState({ values }),
|
||||
[_nextState]
|
||||
);
|
||||
|
||||
const _onSubmit = useCallback(
|
||||
(): void => {
|
||||
method && onSubmit(method, values);
|
||||
},
|
||||
[onSubmit, method, values]
|
||||
);
|
||||
|
||||
return (
|
||||
<section className='runtime--Selection'>
|
||||
<InputCalls
|
||||
help={t<string>('The actual runtime function to make a call to.')}
|
||||
label={t<string>('call the selected endpoint')}
|
||||
onChange={_onChangeMethod}
|
||||
/>
|
||||
{method && (
|
||||
<Params
|
||||
key={`${method.section}.${method.method}:params` /* force re-render on change */}
|
||||
onChange={_onChangeValues}
|
||||
params={params}
|
||||
/>
|
||||
)}
|
||||
<Button.Group>
|
||||
<Button
|
||||
icon='sign-in-alt'
|
||||
isDisabled={!isValid || !method}
|
||||
label={t<string>('Submit Runtime call')}
|
||||
onClick={_onSubmit}
|
||||
/>
|
||||
</Button.Group>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(Selection);
|
||||
46
packages/page-runtime/src/Runtime/index.tsx
Normal file
46
packages/page-runtime/src/Runtime/index.tsx
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { RawParam } from '@polkadot/react-params/types';
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { Result } from './types';
|
||||
|
||||
import React, { useCallback, useState } from 'react';
|
||||
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
|
||||
import Results from './Results';
|
||||
import Selection from './Selection';
|
||||
|
||||
let id = 0;
|
||||
|
||||
function RuntimeApp (): React.ReactElement {
|
||||
const { api } = useApi();
|
||||
const [results, setResults] = useState<Result[]>([]);
|
||||
|
||||
const addResult = useCallback(
|
||||
(result: Result) => setResults((prev) => [result].concat(...prev)),
|
||||
[]
|
||||
);
|
||||
|
||||
const onSubmit = useCallback(
|
||||
(def: DefinitionCallNamed, values: RawParam[]): void => {
|
||||
api.call[def.section][def.method](...values.map(({ value }) => value))
|
||||
.then((result) => addResult({ def, id: ++id, result }))
|
||||
.catch((e): void => {
|
||||
addResult({ def, error: e as Error, id: ++id });
|
||||
console.error(e);
|
||||
});
|
||||
},
|
||||
[api, addResult]
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<Selection onSubmit={onSubmit} />
|
||||
<Results results={results} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(RuntimeApp);
|
||||
11
packages/page-runtime/src/Runtime/types.ts
Normal file
11
packages/page-runtime/src/Runtime/types.ts
Normal file
@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, DefinitionCallNamed } from '@polkadot/types/types';
|
||||
|
||||
export interface Result {
|
||||
id: number;
|
||||
error?: Error;
|
||||
def: DefinitionCallNamed;
|
||||
result?: Codec;
|
||||
}
|
||||
38
packages/page-runtime/src/index.tsx
Normal file
38
packages/page-runtime/src/index.tsx
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AppProps as Props } from '@polkadot/react-components/types';
|
||||
|
||||
import React, { useRef } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
|
||||
import { Tabs } from '@polkadot/react-components';
|
||||
|
||||
import Runtime from './Runtime';
|
||||
import { useTranslation } from './translate';
|
||||
|
||||
function RuntimeApp ({ basePath }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const itemsRef = useRef([
|
||||
{
|
||||
isRoot: true,
|
||||
name: 'runtime',
|
||||
text: t<string>('Calls')
|
||||
}
|
||||
]);
|
||||
|
||||
return (
|
||||
<main className='runtime--App'>
|
||||
<Tabs
|
||||
basePath={basePath}
|
||||
items={itemsRef.current}
|
||||
/>
|
||||
<Switch>
|
||||
<Route><Runtime /></Route>
|
||||
</Switch>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(RuntimeApp);
|
||||
10
packages/page-runtime/src/translate.ts
Normal file
10
packages/page-runtime/src/translate.ts
Normal file
@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2022 @polkadot/app-runtime authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { UseTranslationResponse } from 'react-i18next';
|
||||
|
||||
import { useTranslation as useTranslationBase } from 'react-i18next';
|
||||
|
||||
export function useTranslation (): UseTranslationResponse<'app-runtime', undefined> {
|
||||
return useTranslationBase('app-runtime');
|
||||
}
|
||||
9
packages/page-runtime/tsconfig.build.json
Normal file
9
packages/page-runtime/tsconfig.build.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "..",
|
||||
"outDir": "./build",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"references": []
|
||||
}
|
||||
43
packages/react-components/src/InputCalls/SelectMethod.tsx
Normal file
43
packages/react-components/src/InputCalls/SelectMethod.tsx
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { DropdownOption } from '../util/types';
|
||||
|
||||
import React, { useCallback } from 'react';
|
||||
|
||||
import Dropdown from '../Dropdown';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
defs: Record<string, Record<string, DefinitionCallNamed>>;
|
||||
isError?: boolean;
|
||||
onChange: (value: DefinitionCallNamed) => void;
|
||||
options: DropdownOption[];
|
||||
value: DefinitionCallNamed;
|
||||
}
|
||||
|
||||
function SelectMethod ({ className = '', defs, isError, onChange, options, value }: Props): React.ReactElement<Props> | null {
|
||||
const _transform = useCallback(
|
||||
(method: string) => defs[value.section][method],
|
||||
[defs, value]
|
||||
);
|
||||
|
||||
if (!options.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<Dropdown
|
||||
className={`ui--DropdownLinked-Items ${className}`}
|
||||
isError={isError}
|
||||
onChange={onChange}
|
||||
options={options}
|
||||
transform={_transform}
|
||||
value={value.method}
|
||||
withLabel={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(SelectMethod);
|
||||
34
packages/react-components/src/InputCalls/SelectSection.tsx
Normal file
34
packages/react-components/src/InputCalls/SelectSection.tsx
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { DropdownOptions } from '../util/types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
import Dropdown from '../Dropdown';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
defaultValue?: string;
|
||||
isError?: boolean;
|
||||
onChange: (value: string) => void;
|
||||
options: DropdownOptions;
|
||||
value: DefinitionCallNamed;
|
||||
}
|
||||
|
||||
function SelectSection ({ className = '', defaultValue, isError, onChange, options, value }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<Dropdown
|
||||
className={`ui--DropdownLinked-Sections ${className}`}
|
||||
defaultValue={defaultValue}
|
||||
isError={isError}
|
||||
onChange={onChange}
|
||||
options={options}
|
||||
value={value.section}
|
||||
withLabel={false}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(SelectSection);
|
||||
86
packages/react-components/src/InputCalls/index.tsx
Normal file
86
packages/react-components/src/InputCalls/index.tsx
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// TODO: We have a lot shared between this and InputExtrinsic & InputStorage
|
||||
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { DropdownOptions } from '../util/types';
|
||||
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
|
||||
import LinkedWrapper from '../InputExtrinsic/LinkedWrapper';
|
||||
import methodOptions from './options/method';
|
||||
import sectionOptions from './options/section';
|
||||
import SelectMethod from './SelectMethod';
|
||||
import SelectSection from './SelectSection';
|
||||
import useRuntime from './useRuntime';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
help?: React.ReactNode;
|
||||
label: React.ReactNode;
|
||||
onChange?: (value: DefinitionCallNamed) => void;
|
||||
withLabel?: boolean;
|
||||
}
|
||||
|
||||
function InputCalls ({ className, help, label, onChange, withLabel }: Props): React.ReactElement<Props> | null {
|
||||
const [defs, defaultValue] = useRuntime();
|
||||
const [optionsSection] = useState<DropdownOptions>(() => sectionOptions(defs));
|
||||
const [optionsMethod, setOptionsMethod] = useState<DropdownOptions>(() => methodOptions(defs, defaultValue && defaultValue.section));
|
||||
const [value, setValue] = useState<DefinitionCallNamed | null>(() => defaultValue);
|
||||
|
||||
useEffect((): void => {
|
||||
value && onChange && onChange(value);
|
||||
}, [onChange, value]);
|
||||
|
||||
const _onMethodChange = useCallback(
|
||||
(newValue: DefinitionCallNamed): void => {
|
||||
if (value !== newValue) {
|
||||
// set via callback since the method is a function itself
|
||||
setValue(() => newValue);
|
||||
}
|
||||
},
|
||||
[value]
|
||||
);
|
||||
|
||||
const _onSectionChange = useCallback(
|
||||
(newSection: string): void => {
|
||||
if (value && newSection !== value.section) {
|
||||
const optionsMethod = methodOptions(defs, newSection);
|
||||
|
||||
setOptionsMethod(optionsMethod);
|
||||
_onMethodChange(defs[newSection][optionsMethod[0].value]);
|
||||
}
|
||||
},
|
||||
[_onMethodChange, defs, value]
|
||||
);
|
||||
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<LinkedWrapper
|
||||
className={className}
|
||||
help={help}
|
||||
label={label}
|
||||
withLabel={withLabel}
|
||||
>
|
||||
<SelectSection
|
||||
className='small'
|
||||
onChange={_onSectionChange}
|
||||
options={optionsSection}
|
||||
value={value}
|
||||
/>
|
||||
<SelectMethod
|
||||
className='large'
|
||||
defs={defs}
|
||||
onChange={_onMethodChange}
|
||||
options={optionsMethod}
|
||||
value={value}
|
||||
/>
|
||||
</LinkedWrapper>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(InputCalls);
|
||||
47
packages/react-components/src/InputCalls/options/method.tsx
Normal file
47
packages/react-components/src/InputCalls/options/method.tsx
Normal file
@ -0,0 +1,47 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { DropdownOption, DropdownOptions } from '../../util/types';
|
||||
|
||||
import React from 'react';
|
||||
|
||||
export default function createOptions (runtime: Record<string, Record<string, DefinitionCallNamed>>, sectionName: string | null): DropdownOptions {
|
||||
if (!sectionName) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const section = runtime[sectionName];
|
||||
|
||||
if (!section || Object.keys(runtime[sectionName]).length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Object
|
||||
.keys(runtime[sectionName])
|
||||
.sort()
|
||||
.map((m) => section[m])
|
||||
.map(({ description, method, params }): DropdownOption => {
|
||||
const inputs = params.map(({ name }) => name).join(', ');
|
||||
|
||||
return {
|
||||
className: 'ui--DropdownLinked-Item',
|
||||
key: `${sectionName}_${method}`,
|
||||
text: [
|
||||
<div
|
||||
className='ui--DropdownLinked-Item-call'
|
||||
key={`${sectionName}_${method}:call`}
|
||||
>
|
||||
{method}({inputs})
|
||||
</div>,
|
||||
<div
|
||||
className='ui--DropdownLinked-Item-text'
|
||||
key={`${sectionName}_${method}:text`}
|
||||
>
|
||||
{description || method}
|
||||
</div>
|
||||
],
|
||||
value: method
|
||||
};
|
||||
});
|
||||
}
|
||||
16
packages/react-components/src/InputCalls/options/section.ts
Normal file
16
packages/react-components/src/InputCalls/options/section.ts
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
import type { DropdownOptions } from '../../util/types';
|
||||
|
||||
export default function createOptions (runtime: Record<string, Record<string, DefinitionCallNamed>>): DropdownOptions {
|
||||
return Object
|
||||
.keys(runtime)
|
||||
.sort()
|
||||
.filter((s) => Object.keys(runtime[s]).length !== 0)
|
||||
.map((value): { text: string; value: string } => ({
|
||||
text: value,
|
||||
value
|
||||
}));
|
||||
}
|
||||
55
packages/react-components/src/InputCalls/useRuntime.ts
Normal file
55
packages/react-components/src/InputCalls/useRuntime.ts
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright 2017-2022 @polkadot/react-components authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@polkadot/api';
|
||||
import type { DefinitionCallNamed } from '@polkadot/types/types';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { createNamedHook, useApi } from '@polkadot/react-hooks';
|
||||
|
||||
function getEntries <T> (obj: Record<string, T>): [string, T][] {
|
||||
return Object
|
||||
.entries(obj)
|
||||
.sort(([a], [b]) => a.localeCompare(b));
|
||||
}
|
||||
|
||||
function getAllCalls (api: ApiPromise): [Record<string, Record<string, DefinitionCallNamed>>, DefinitionCallNamed | null] {
|
||||
const result: Record<string, Record<string, DefinitionCallNamed>> = {};
|
||||
let defValue: DefinitionCallNamed | null = null;
|
||||
const sections = getEntries(api.call);
|
||||
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
const [section, methodsObj] = sections[i];
|
||||
const methods = getEntries(methodsObj);
|
||||
|
||||
for (let j = 0; j < methods.length; j++) {
|
||||
const [method, { meta }] = methods[j] as unknown as [string, { meta: DefinitionCallNamed }];
|
||||
|
||||
if (meta) {
|
||||
if (!result[section]) {
|
||||
result[section] = {};
|
||||
|
||||
if (defValue === null) {
|
||||
defValue = meta;
|
||||
}
|
||||
}
|
||||
|
||||
result[section][method] = meta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [result, defValue];
|
||||
}
|
||||
|
||||
function useRuntimeImpl (): [Record<string, Record<string, DefinitionCallNamed>>, DefinitionCallNamed | null] {
|
||||
const { api } = useApi();
|
||||
|
||||
return useMemo(
|
||||
() => getAllCalls(api),
|
||||
[api]
|
||||
);
|
||||
}
|
||||
|
||||
export default createNamedHook('useRuntime', useRuntimeImpl);
|
||||
@ -21,7 +21,6 @@ interface Props {
|
||||
className?: string;
|
||||
defaultValue: DefinitionRpcExt;
|
||||
help?: React.ReactNode;
|
||||
isError?: boolean;
|
||||
label: React.ReactNode;
|
||||
onChange?: (value: DefinitionRpcExt) => void;
|
||||
withLabel?: boolean;
|
||||
|
||||
@ -54,6 +54,7 @@ export { default as InputAddress } from './InputAddress';
|
||||
export { default as InputAddressMulti } from './InputAddressMulti';
|
||||
export { default as InputAddressSimple } from './InputAddressSimple';
|
||||
export { default as InputBalance } from './InputBalance';
|
||||
export { default as InputCalls } from './InputCalls';
|
||||
export { default as InputConsts } from './InputConsts';
|
||||
export { default as InputExtrinsic } from './InputExtrinsic';
|
||||
export { default as InputFile } from './InputFile';
|
||||
|
||||
@ -52,6 +52,8 @@
|
||||
"@polkadot/app-poll/*": ["page-poll/src/*"],
|
||||
"@polkadot/app-rpc": ["page-rpc/src"],
|
||||
"@polkadot/app-rpc/*": ["page-rpc/src/*"],
|
||||
"@polkadot/app-runtime": ["page-runtime/src"],
|
||||
"@polkadot/app-runtime/*": ["page-runtime/src/*"],
|
||||
"@polkadot/app-settings": ["page-settings/src"],
|
||||
"@polkadot/app-settings/*": ["page-settings/src/*"],
|
||||
"@polkadot/app-signing": ["page-signing/src"],
|
||||
|
||||
@ -2930,6 +2930,15 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@polkadot/app-runtime@workspace:packages/page-runtime":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@polkadot/app-runtime@workspace:packages/page-runtime"
|
||||
dependencies:
|
||||
"@babel/runtime": ^7.18.6
|
||||
"@polkadot/react-components": ^0.116.2-73-x
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@polkadot/app-settings@workspace:packages/page-settings":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@polkadot/app-settings@workspace:packages/page-settings"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user