// Copyright 2017-2021 @polkadot/app-storage authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { QueryableStorageEntry } from '@polkadot/api/types'; import type { RawParams } from '@polkadot/react-params/types'; import type { StorageEntryTypeLatest } from '@polkadot/types/interfaces'; import type { Registry, TypeDef } from '@polkadot/types/types'; import type { ComponentProps as Props } from '../types'; import React, { useCallback, useMemo, useState } from 'react'; import { ApiPromise } from '@polkadot/api'; import { Button, Input, InputStorage } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import Params from '@polkadot/react-params'; import { getTypeDef } from '@polkadot/types'; import { getSiName } from '@polkadot/types/metadata/util'; import { TypeDefInfo } from '@polkadot/types/types'; import { isHex, isNull, isUndefined } from '@polkadot/util'; import { useTranslation } from '../translate'; interface TypeDefExt extends TypeDef { withOptionActive?: boolean; } type ParamsType = { type: TypeDefExt }[]; interface KeyState { defaultValues: RawParams | undefined | null; isIterable: boolean; key: QueryableStorageEntry<'promise'>; params: ParamsType; } interface ValState { isValid: boolean; values: RawParams; } interface BlockHash { blockHash: string | null; textHash: string; } function areParamsValid ({ creator: { meta: { type } } }: QueryableStorageEntry<'promise'>, values: RawParams): boolean { return values.reduce((isValid: boolean, value) => isValid && !isUndefined(value) && !isUndefined(value.value) && value.isValid, (values.length === (type.isPlain ? 0 : type.asMap.hashers.length))); } function expandParams (registry: Registry, st: StorageEntryTypeLatest, isIterable: boolean): ParamsType { let types: string[] = []; if (st.isMap) { const { hashers, key } = st.asMap; types = hashers.length === 1 ? [getSiName(registry.lookup, key)] : registry.lookup.getSiType(key).def.asTuple.map((k) => getSiName(registry.lookup, k)); } return types.map((str, index) => { let type: TypeDefExt; if (isIterable && index === (types.length - 1)) { type = getTypeDef(`Option<${str}>`); type.withOptionActive = true; } else { type = getTypeDef(str); } return { type }; }); } function checkIterable (registry: Registry, type: StorageEntryTypeLatest): boolean { // in the case of Option keys, we don't allow map iteration, in this case // we would have option for the iterable and then option for the key value if (type.isPlain) { return true; } const { hashers, key } = type.asMap; if (hashers.length === 1) { return registry.lookup.getTypeDef(key).info !== TypeDefInfo.Option; } const keys = registry.lookup.getSiType(key).def.asTuple; return registry.lookup.getTypeDef(keys[keys.length - 1]).info !== TypeDefInfo.Option; } function expandKey (api: ApiPromise, key: QueryableStorageEntry<'promise'>): KeyState { const { creator: { meta: { type }, section } } = key; const isIterable = checkIterable(api.registry, type); return { defaultValues: section === 'session' && type.isMap && api.consts.session && api.consts.session.dedupKeyPrefix ? [{ isValid: true, value: api.consts.session.dedupKeyPrefix.toHex() }] : null, isIterable, key, params: expandParams(api.registry, type, isIterable) }; } function extractParams (isIterable: boolean, values: RawParams): [RawParams, boolean] { const params = values.filter(({ value }, index) => !isIterable || (index !== values.length - 1) || !isNull(value)); return [params, params.length === values.length]; } function Modules ({ onAdd }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [{ defaultValues, isIterable, key, params }, setKey] = useState(() => ({ defaultValues: undefined, isIterable: false, key: api.query.timestamp?.now || api.query.system.events, params: [] })); const [{ isValid, values }, setValues] = useState(() => ({ isValid: true, values: [] })); const [{ blockHash, textHash }, setBlockHash] = useState({ blockHash: null, textHash: '' }); const _onAdd = useCallback( (): void => { const [params, isAtEnabled] = extractParams(isIterable, values); isValid && onAdd({ blockHash: isAtEnabled ? blockHash : null, isConst: false, key, params }); }, [blockHash, isIterable, isValid, key, onAdd, values] ); const _onChangeAt = useCallback( (textHash: string) => setBlockHash({ blockHash: isHex(textHash, 256) ? textHash : null, textHash }), [] ); const _onChangeValues = useCallback( (values: RawParams) => setValues({ isValid: areParamsValid(key, values), values }), [key] ); const _onChangeKey = useCallback( (key: QueryableStorageEntry<'promise'>): void => { setKey(expandKey(api, key)); _onChangeValues([]); }, [_onChangeValues, api] ); const isAtAllowed = useMemo( () => isValid ? extractParams(isIterable, values)[1] : false, [isIterable, isValid, values] ); const { creator: { meta, method, section } } = key; return (
('selected state query')} onChange={_onChangeKey} /> ('blockhash to query at')} onChange={_onChangeAt} placeholder={t('0x...')} />
); } export default React.memo(Modules);