key.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017-2021 @polkadot/react-components authors & contributors
  2. // SPDX-License-Identifier: Apache-2.0
  3. import type { StorageEntry } from '@polkadot/types/primitive/types';
  4. import type { DropdownOption, DropdownOptions } from '../../util/types';
  5. import React from 'react';
  6. import { ApiPromise } from '@polkadot/api';
  7. import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey';
  8. import { registry } from '@joystream/types';
  9. export default function createOptions (api: ApiPromise, sectionName: string): DropdownOptions {
  10. const section = api.query[sectionName];
  11. if (!section || Object.keys(section).length === 0) {
  12. return [];
  13. }
  14. return Object
  15. .keys(section)
  16. .sort()
  17. .map((value): DropdownOption => {
  18. const method = section[value] as unknown as StorageEntry;
  19. const type = method.meta.type;
  20. const input = type.isMap
  21. ? type.asMap.key.toString()
  22. : type.isDoubleMap
  23. ? `${type.asDoubleMap.key1.toString()}, ${type.asDoubleMap.key2.toString()}`
  24. : '';
  25. const output = method.meta.modifier.isOptional
  26. ? `Option<${unwrapStorageType(registry, type)}>`
  27. : unwrapStorageType(registry, type);
  28. return {
  29. className: 'ui--DropdownLinked-Item',
  30. key: `${sectionName}_${value}`,
  31. text: [
  32. <div
  33. className='ui--DropdownLinked-Item-call'
  34. key={`${sectionName}_${value}:call`}
  35. >
  36. {value}({input}): {output}
  37. </div>,
  38. <div
  39. className='ui--DropdownLinked-Item-text'
  40. key={`${sectionName}_${value}:text`}
  41. >
  42. {(method.meta.docs[0] || method.meta.name).toString()}
  43. </div>
  44. ],
  45. value
  46. };
  47. });
  48. }