mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-10-24 12:35:01 +00:00
feat: initial Pezkuwi Apps rebrand from polkadot-apps
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
This commit is contained in:
commit
d21bfb1320
5867 files changed
+329019
No files matched your search
@@ -0,0 +1,96 @@
|
||||
// Copyright 2017-2025 @pezkuwi/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@pezkuwi/api';
|
||||
import type { QueryableStorageMultiArg } from '@pezkuwi/api/types';
|
||||
import type { Tracker } from './useCall.js';
|
||||
import type { MountedRef } from './useIsMountedRef.js';
|
||||
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { isUndefined, nextTick } from '@pezkuwi/util';
|
||||
|
||||
import { useApi } from './useApi.js';
|
||||
import { handleError, transformIdentity, unsubscribe } from './useCall.js';
|
||||
import { useIsMountedRef } from './useIsMountedRef.js';
|
||||
|
||||
interface TrackerRef {
|
||||
current: Tracker;
|
||||
}
|
||||
|
||||
interface CallOptions <T> {
|
||||
defaultValue?: T;
|
||||
transform?: (value: any, api: ApiPromise) => T;
|
||||
}
|
||||
|
||||
// subscribe, trying to play nice with the browser threads
|
||||
function subscribe <T> (api: ApiPromise, mountedRef: MountedRef, tracker: TrackerRef, calls: QueryableStorageMultiArg<'promise'>[], setValue: (value: T) => void, { transform = transformIdentity }: CallOptions<T> = {}): void {
|
||||
unsubscribe(tracker);
|
||||
|
||||
nextTick((): void => {
|
||||
if (mountedRef.current) {
|
||||
const included = calls.map((c) => !!c && (!Array.isArray(c) || !!c[0]));
|
||||
const filtered = calls.filter((_, index) => included[index]);
|
||||
|
||||
if (filtered.length) {
|
||||
// swap to active mode
|
||||
tracker.current.isActive = true;
|
||||
tracker.current.subscriber = api.queryMulti(filtered, (value): void => {
|
||||
// we use the isActive flag here since .subscriber may not be set on immediate callback)
|
||||
if (mountedRef.current && tracker.current.isActive) {
|
||||
let valueIndex = -1;
|
||||
|
||||
try {
|
||||
setValue(
|
||||
transform(
|
||||
calls.map((_, index) =>
|
||||
included[index]
|
||||
? value[++valueIndex]
|
||||
: undefined
|
||||
),
|
||||
api
|
||||
)
|
||||
);
|
||||
} catch (error) {
|
||||
handleError(error as Error, tracker);
|
||||
}
|
||||
}
|
||||
}).catch((error) => handleError(error as Error, tracker));
|
||||
} else {
|
||||
tracker.current.subscriber = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// very much copied from useCall
|
||||
// FIXME This is generic, we cannot really use createNamedHook
|
||||
export function useCallMulti <T> (calls?: QueryableStorageMultiArg<'promise'>[] | null | false, options?: CallOptions<T>): T {
|
||||
const { api } = useApi();
|
||||
const mountedRef = useIsMountedRef();
|
||||
const tracker = useRef<Tracker>({ error: null, fn: null, isActive: false, serialized: null, subscriber: null, type: 'useCallMulti' });
|
||||
const [value, setValue] = useState<T>(() => (isUndefined(options?.defaultValue) ? [] : options?.defaultValue) as unknown as T);
|
||||
|
||||
// initial effect, we need an un-subscription
|
||||
useEffect((): () => void => {
|
||||
return () => unsubscribe(tracker);
|
||||
}, []);
|
||||
|
||||
// on changes, re-subscribe
|
||||
useEffect((): void => {
|
||||
// check if we have a function & that we are mounted
|
||||
if (mountedRef.current && calls) {
|
||||
const serialized = JSON.stringify(calls);
|
||||
|
||||
if (serialized !== tracker.current.serialized) {
|
||||
tracker.current.serialized = serialized;
|
||||
|
||||
subscribe(api, mountedRef, tracker, calls, setValue, options);
|
||||
}
|
||||
}
|
||||
}, [api, calls, options, mountedRef]);
|
||||
|
||||
// throwOnError(tracker.current);
|
||||
|
||||
return value;
|
||||
}
|
||||
Reference in new issue
Block a user