// Copyright 2017-2024 @polkadot/react-components authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ApiPromise } from '@polkadot/api'; import type { DefinitionCallNamed } from '@polkadot/types/types'; import { useMemo } from 'react'; import { createNamedHook, useApi } from '@polkadot/react-hooks'; function getEntries (obj: Record): [string, T][] { return Object .entries(obj) .sort(([a], [b]) => a.localeCompare(b)); } function getAllCalls (api: ApiPromise): [Record>, DefinitionCallNamed | null] { const result: Record> = {}; let defValue: DefinitionCallNamed | null = null; const sections = getEntries(api.call); for (let i = 0, secCount = sections.length; i < secCount; i++) { const [section, methodsObj] = sections[i]; const methods = getEntries(methodsObj); for (let j = 0, metCount = methods.length; j < metCount; j++) { const [method, { meta }] = methods[j] as unknown as [string, { meta: DefinitionCallNamed }]; if (meta) { if (!result[section]) { result[section] = {}; if (defValue === null) { defValue = meta; } } result[section][method] = meta; } } } return [result, defValue]; } function useRuntimeImpl (): [Record>, DefinitionCallNamed | null] { const { api } = useApi(); return useMemo( () => getAllCalls(api), [api] ); } export default createNamedHook('useRuntime', useRuntimeImpl);