Handle non-codec queryStorage results (#5616)

* Handle non-codec queryStorage results

* Re-use valueToText
This commit is contained in:
Jaco 2021-06-16 17:57:10 +03:00 committed by GitHub
parent be309d0d7c
commit 468e17d013
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 10 deletions

View File

@ -2,11 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
import type { QueueTx } from '@polkadot/react-components/Status/types';
import type { Codec } from '@polkadot/types/types';
import React from 'react';
import { Output } from '@polkadot/react-components';
import valueToText from '@polkadot/react-params/valueToText';
import { isUndefined } from '@polkadot/util';
interface Props {
@ -32,7 +32,7 @@ function Results ({ queue = [] }: Props): React.ReactElement<Props> | null {
value={
error
? error.message
: <pre>{JSON.stringify((result as Codec).toHuman(), null, 2).replace(/"/g, '').replace(/\\/g, '').replace(/\],\[/g, '],\n[')}</pre>
: <pre>{valueToText('', result, false)}</pre>
}
/>
))}

View File

@ -63,9 +63,9 @@ function createComponent (type: string, Component: React.ComponentType<any>, def
return {
Component,
// In order to modify the parameters which are used to render the default component, we can use this method
refresh: (swallowErrors: boolean, contentShorten: boolean): React.ComponentType<any> =>
refresh: (contentShorten: boolean): React.ComponentType<any> =>
renderHelper(
(value: any) => <pre>{valueToText(type, value, swallowErrors, contentShorten)}</pre>,
(value: any) => <pre>{valueToText(type, value, contentShorten)}</pre>,
defaultProps
),
// In order to replace the default component during runtime we can provide a RenderFn to create a new 'plugged' component
@ -123,7 +123,7 @@ function getCachedComponent (query: QueryTypes): CacheInstance {
const defaultProps = { className: 'ui--output' };
const Component = renderHelper(
// By default we render a simple div node component with the query results in it
(value: any) => <pre>{valueToText(type, value, true, true)}</pre>,
(value: any) => <pre>{valueToText(type, value, true)}</pre>,
defaultProps
);

View File

@ -34,18 +34,22 @@ function formatKeys (keys: [ValidatorId, Keys][]): string {
}
function toHuman (value: Codec | Codec[]): unknown {
// eslint-disable-next-line @typescript-eslint/unbound-method
return isFunction((value as Codec).toHuman)
? (value as Codec).toHuman()
: (value as Codec[]).map(toHuman);
: Array.isArray(value)
? value.map((v) => toHuman(v))
: value.toString();
}
function toString (value: any): string {
return JSON.stringify(value, null, 2).replace(/"/g, '').replace(/\\/g, '').replace(/\],\[/g, '],\n[');
return JSON
.stringify(value, null, 2)
.replace(/"/g, '')
.replace(/\\/g, '')
.replace(/\],\[/g, '],\n[');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default function valueToText (type: string, value: Codec | undefined | null, swallowError = true, contentShorten = true): React.ReactNode {
export default function valueToText (type: string, value: Codec | undefined | null, contentShorten = true): React.ReactNode {
if (isNull(value) || isUndefined(value)) {
return div({}, '<unknown>');
}