// Copyright 2017-2018 @polkadot/app-storage authors & contributors // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. import { Storage$Key$Value } from '@polkadot/storage/types'; import { I18nProps } from '@polkadot/ui-app/types'; import { StorageQuery } from './types'; import React from 'react'; import typeToString from '@polkadot/params/typeToString'; import Button from '@polkadot/ui-app/Button'; import Labelled from '@polkadot/ui-app/Labelled'; import valueToText from '@polkadot/ui-app/Params/valueToText'; import classes from '@polkadot/ui-app/util/classes'; import withStorageDiv from '@polkadot/ui-react-rx/with/storageDiv'; import translate from './translate'; type Props = I18nProps & { onRemove: (id: number) => void, value: StorageQuery }; type ComponentProps = {}; type State = { inputs: Array, // node? Component: React.ComponentType; }; const cache: Array> = []; class Query extends React.PureComponent { state: State = {} as State; static getCachedComponent ({ id, key, params }: StorageQuery): React.ComponentType { if (!cache[id]) { const values: Array = params.map(({ value }) => // FIXME not 100% convinced, arrays could be an issue? (Plus, if we have to cast, something just _seems_ off) value as Storage$Key$Value ); cache[id] = withStorageDiv(key, { params: values })( (value: any) => valueToText(key.type, value), { className: 'ui--output' } ); } return cache[id]; } static getDerivedStateFromProps ({ value }: Props, prevState: State): State | null { const Component = Query.getCachedComponent(value); const { key, params } = value; const inputs = key.params.map(({ name, type }, index) => ( {name}={valueToText(type, params[index].value)} )); return { Component, inputs }; } render () { const { className, style, value: { key } } = this.props; const { Component, inputs } = this.state; return (
{key.section}.{key.name}({inputs}): {typeToString(key.type)}
} >