ghost-portal/packages/react-params/src/Param/useParamDefs.ts
Jaco Greeff e10d56d0ec
Bump API (#4017)
* Bump API

* Rebuild i18n

* Fix InputAddress

* Pass through registry where applicable
2020-11-12 15:43:19 +01:00

41 lines
1.0 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Registry, TypeDef } from '@polkadot/types/types';
import { ParamDef } from '../types';
import { useEffect, useState } from 'react';
import { getTypeDef } from '@polkadot/types/create';
function expandDef (registry: Registry, td: TypeDef): TypeDef {
try {
return getTypeDef(
registry.createType(td.type as 'u32').toRawType()
);
} catch (e) {
return td;
}
}
export default function useParamDefs (registry: Registry, type: TypeDef): ParamDef[] {
const [params, setParams] = useState<ParamDef[]>([]);
useEffect((): void => {
const typeDef = expandDef(registry, type);
if (!typeDef.sub) {
return setParams([]);
}
setParams(
(Array.isArray(typeDef.sub) ? typeDef.sub : [typeDef.sub]).map((td): ParamDef => ({
length: typeDef.length,
name: td.name,
type: td // expandDef(td)
}))
);
}, [registry, type]);
return params;
}