mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-04-23 10:37:59 +00:00
d21bfb1320
Rebranded terminology: - Polkadot → Pezkuwi - Kusama → Dicle - Westend → Zagros - Rococo → PezkuwiChain - Substrate → Bizinikiwi - parachain → teyrchain Custom logos with Kurdistan brand colors (#e6007a → #86e62a): - bizinikiwi-hexagon.svg - sora-bizinikiwi.svg - hezscanner.svg - heztreasury.svg - pezkuwiscan.svg - pezkuwistats.svg - pezkuwiassembly.svg - pezkuwiholic.svg
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
// Copyright 2017-2025 @pezkuwi/react-hooks authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { AugmentedEvent } from '@pezkuwi/api/types';
|
|
import type { Vec } from '@pezkuwi/types';
|
|
import type { EventRecord } from '@pezkuwi/types/interfaces';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { createNamedHook } from './createNamedHook.js';
|
|
import { useApi } from './useApi.js';
|
|
import { useCall } from './useCall.js';
|
|
import { useIsMountedRef } from './useIsMountedRef.js';
|
|
import { useMemoValue } from './useMemoValue.js';
|
|
|
|
export type EventCheck = AugmentedEvent<'promise'> | false | undefined | null;
|
|
|
|
interface Result {
|
|
blockHash: string;
|
|
events: EventRecord[];
|
|
}
|
|
|
|
const EMPTY_RESULT: Result = {
|
|
blockHash: '',
|
|
events: []
|
|
};
|
|
|
|
const IDENTITY_FILTER = () => true;
|
|
|
|
function useEventTriggerImpl (checks: EventCheck[], filter: (record: EventRecord) => boolean = IDENTITY_FILTER): Result {
|
|
const { api } = useApi();
|
|
const [state, setState] = useState(() => EMPTY_RESULT);
|
|
const memoChecks = useMemoValue(checks);
|
|
const mountedRef = useIsMountedRef();
|
|
const eventRecords = useCall<Vec<EventRecord>>(api.query.system.events);
|
|
|
|
useEffect((): void => {
|
|
if (mountedRef.current && eventRecords) {
|
|
const events = eventRecords.filter((r) =>
|
|
r.event &&
|
|
memoChecks.some((c) => c && c.is(r.event)) &&
|
|
filter(r)
|
|
);
|
|
|
|
if (events.length) {
|
|
setState({
|
|
blockHash: eventRecords.createdAtHash?.toHex() || '',
|
|
events
|
|
});
|
|
}
|
|
}
|
|
}, [eventRecords, filter, memoChecks, mountedRef]);
|
|
|
|
return state;
|
|
}
|
|
|
|
export const useEventTrigger = createNamedHook('useEventTrigger', useEventTriggerImpl);
|