ghost-portal/packages/react-hooks/src/useAssetIds.ts
Arjun Porwal 331fc4b9a2
Enhance Preimages Page with Improved Usability (#11342)
* enhance: Add user preimages component and update preimage handling

* refactor: user preimages handling and improve UI components

* style: enhance table row styling in Preimage component

* feat: unnote multiple preimages at once

* chore: check if id is not undefined
2025-03-05 22:52:59 +05:30

44 lines
1.3 KiB
TypeScript

// Copyright 2017-2025 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { StorageKey, u32 } from '@polkadot/types';
import type { EventRecord } from '@polkadot/types/interfaces';
import type { Changes } from './useEventChanges.js';
import { createNamedHook, useApi, useEventChanges, useMapKeys } from './index.js';
const EMPTY_PARAMS: unknown[] = [];
const OPT_KEY = {
transform: (keys: StorageKey<[u32]>[]): u32[] =>
keys.map(({ args: [id] }) => id).filter((id) => id !== undefined)
};
function filter (records: EventRecord[]): Changes<u32> {
const added: u32[] = [];
const removed: u32[] = [];
records.forEach(({ event: { data: [id], method } }): void => {
if (method === 'Created' || method === 'ForceCreated') {
added.push(id as u32);
} else {
removed.push(id as u32);
}
});
return { added, removed };
}
function useAssetIdsImpl (): u32[] | undefined {
const { api } = useApi();
const startValue = useMapKeys(api.query.assets?.asset, EMPTY_PARAMS, OPT_KEY) || [];
return useEventChanges([
api.events.assets?.Created,
api.events.assets?.Destroyed,
api.events.assets?.ForceCreated
], filter, startValue);
}
export const useAssetIds = createNamedHook('useAssetIds', useAssetIdsImpl);