mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-06-23 12:51:02 +00:00
971df8edba
- Remove all 3rd party parachain configurations from endpoints: - productionRelayPolkadot.ts: Keep only system parachains - productionRelayDicle.ts: Keep only system parachains - testingRelayZagros.ts: Keep only system parachains - testingRelayTeyrChain.ts: Keep only system parachains - Update domain references: - polkadot.js.org → pezkuwichain.app - wiki.polkadot.network → wiki.pezkuwichain.io - dotapps.io → pezkuwichain.app - statement.polkadot.network → docs.pezkuwichain.io/statement - support.polkadot.network → docs.pezkuwichain.io - Update repository references: - github.com/pezkuwi-js/apps → github.com/pezkuwichain/pwap - Rename system parachains to Pezkuwi ecosystem: - PolkadotAssetHub → PezkuwiAssetHub - polkadotBridgeHub → pezkuwiBridgeHub - polkadotCollectives → pezkuwiCollectives - polkadotCoretime → pezkuwiCoretime - polkadotPeople → pezkuwiPeople - Update network name in claims utility: - Polkadot → Pezkuwi
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import '@pezkuwi/x-textencoder/shim';
|
|
import '@pezkuwi/x-textdecoder/shim';
|
|
|
|
import type { CallOptions } from './types.js';
|
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
import { useIsMountedRef } from './useIsMountedRef.js';
|
|
|
|
interface Options <T> extends CallOptions<T> {
|
|
transform?: (value: any) => T
|
|
}
|
|
|
|
export function normalizeMetadataLink (link?: string): string {
|
|
if (!link) {
|
|
return '';
|
|
} else if (link.toLowerCase().startsWith('http')) {
|
|
return link;
|
|
}
|
|
|
|
// handle V0 CID
|
|
const matchCidV0 = link.match(/Qm[A-Za-z0-9]{44}(?![A-Za-z0-9])/);
|
|
|
|
if (matchCidV0 !== null) {
|
|
return matchCidV0[0];
|
|
}
|
|
|
|
// handle V1 CID
|
|
const matchCidV1 = link.match(/[a-z0-9]{59}(?![A-Za-z0-9])/);
|
|
|
|
if (matchCidV1 !== null) {
|
|
return matchCidV1[0];
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
const cache = new Map<string, any>();
|
|
|
|
async function fetchMetadata <T> (metadataLinks: string[]): Promise<Map<string, T>> {
|
|
const result = new Map();
|
|
|
|
const promises = metadataLinks.map((metadataLink) => {
|
|
if (cache.has(metadataLink)) {
|
|
result.set(metadataLink, cache.get(metadataLink));
|
|
|
|
return Promise.resolve();
|
|
}
|
|
|
|
const fetchLink = metadataLink.startsWith('http') ? metadataLink : `https://ipfs.io/ipfs/${metadataLink}`;
|
|
|
|
return fetch(fetchLink)
|
|
.then(async (res) => {
|
|
const response = res.status >= 200 && res.status < 300 ? await res.text() : null;
|
|
|
|
cache.set(metadataLink, response);
|
|
result.set(metadataLink, response);
|
|
});
|
|
});
|
|
|
|
await Promise.allSettled(promises);
|
|
|
|
return result;
|
|
}
|
|
|
|
function postProcessData <T> (fetchedMetadata: Map<string, T>, { transform }: Options<T> = {}) {
|
|
if (!transform) {
|
|
return fetchedMetadata;
|
|
}
|
|
|
|
for (const [key, value] of fetchedMetadata.entries()) {
|
|
fetchedMetadata.set(key, transform(value));
|
|
}
|
|
|
|
return fetchedMetadata;
|
|
}
|
|
|
|
// FIXME This is generic, we cannot really use createNamedHook
|
|
export function useMetadataFetch <T> (rawLinks: string[] | undefined, options?: Options<T>): Map<string, T> | undefined {
|
|
const mountedRef = useIsMountedRef();
|
|
const [value, setValue] = useState<Map<string, T> | undefined>();
|
|
|
|
const metadataLinks = useMemo(() => {
|
|
if (!rawLinks) {
|
|
return undefined;
|
|
}
|
|
|
|
return rawLinks
|
|
.map((hash) => normalizeMetadataLink(hash))
|
|
.filter((hash) => !!hash);
|
|
}, [rawLinks]);
|
|
|
|
useEffect((): void => {
|
|
if (mountedRef.current && metadataLinks) {
|
|
fetchMetadata<T>(metadataLinks)
|
|
.then((fetchedMetadata) => setValue(postProcessData<T>(fetchedMetadata, options)))
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
.catch(() => { });
|
|
}
|
|
}, [metadataLinks, options, mountedRef]);
|
|
|
|
return value;
|
|
}
|