// Copyright 2017-2019 @polkadot/ui-app 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 './Params.css'; import React from 'react'; import { classes } from '@polkadot/ui-app/util'; import { isNull, isUndefined, u8aToHex } from '@polkadot/util'; import { Option, U8a } from '@polkadot/types'; // const empty = div({}, ''); const unknown = div({}, ''); type DivProps = { className?: string, key?: any }; function div ({ key, className }: DivProps, ...values: Array): React.ReactNode { return (
{values}
); } // function accountToText (_address: string | Uint8Array): React.ReactNode { // const address = isU8a(_address) // ? encodeAddress(_address) // : _address; // return div( // { className: 'nowrap', key: `account_${address}` }, // , // div({ key: 'address', className: 'ui--Param-Address' }, address), // // Empty div so double clicking on the address only selects the address //
// ); // } // function proposalToText ({ extrinsic, params }: ExtrinsicDecoded): React.ReactNode { // if (!extrinsic) { // return unknown; // } // const inputs = extrinsic.params.map(({ name, type }, index) => // div( // { key: `param_${index}` }, // div({ className: 'name', key: 'param_name' }, `${name}=`), // valueToText(type, params[index]) // ) // ); // return div( // {}, // div({ className: 'name' }, `${extrinsic.section}.${extrinsic.name}(`), // inputs, // div({ className: 'name' }, ')') // ); // } // function arrayToText (type: Param$Type$Array, value: Array, withBound: boolean = true): React.ReactNode { // if (value.length === 0) { // return empty; // } // if (type.length === 1) { // if (type[0] === 'KeyValueStorage') { // return div({}, value.length); // } // return value.map((value, index) => // div( // { key: `value_${index}` }, // div({ className: 'name', key: 'name' }, `${index}:`), // valueToText(type[0], value, false) // ) // ); // } // const values = type.map((_type, index) => // valueToText(_type, value[index], false) // ); // return div( // {}, // div({ className: 'name' }, '('), // values, // div({ className: 'name' }, ')') // ); // } function valueToText (type: string, value: any, swallowError: boolean = true, contentShorten: boolean = true): React.ReactNode { // try { // if (type === 'bool') { // return div({}, value ? 'Yes' : 'No'); // } // if (isUndefined(value)) { // return unknown; // } // if (Array.isArray(type)) { // return arrayToText(type, value); // } // if (type === 'AccountId') { // return value && value.length // ? accountToText(value as string) // : unknown; // } // if (type === 'Proposal') { // return proposalToText(value as ExtrinsicDecoded); // } // if (type === 'VoteThreshold') { // return div({}, thresholdTextMap[value]); // } // if (isU8a(value)) { // return div({}, u8aToHex(value, 256)); // } // if (isBn(value)) { // return div({}, formatNumber(value)); // } // } catch (error) { // if (!swallowError) { // throw error; // } else { // console.log('valueToText', type, value, error); // } // } // dont' even ask, nested ?: ... really? return isNull(value) || isUndefined(value) ? unknown : div( {}, ['Bytes', 'Data'].includes(type) ? u8aToHex(value.toU8a(true), contentShorten ? 512 : -1) : ( value instanceof U8a ? ( value.isEmpty ? '' : value.toString() ) : ( (value instanceof Option) && value.isNone ? '' : value.toString() ) ) ); } export default valueToText;