mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-07-26 22:25:41 +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
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { ProviderInterface } from '@pezkuwi/rpc-provider/types';
|
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
import { ApiPromise, WsProvider } from '@pezkuwi/api';
|
|
import { createWsEndpoints, typesBundle } from '@pezkuwi/apps-config';
|
|
import { settings } from '@pezkuwi/ui-settings';
|
|
import { arrayShuffle, isString } from '@pezkuwi/util';
|
|
|
|
import { createNamedHook } from './createNamedHook.js';
|
|
import { useIsMountedRef } from './useIsMountedRef.js';
|
|
|
|
const endpoints = createWsEndpoints();
|
|
|
|
function disconnect (provider: ProviderInterface | null): null {
|
|
provider?.disconnect().catch(console.error);
|
|
|
|
return null;
|
|
}
|
|
|
|
function useApiUrlImpl (url?: null | string | string[]): ApiPromise | null {
|
|
const providerRef = useRef<ProviderInterface | null>(null);
|
|
const mountedRef = useIsMountedRef();
|
|
const [state, setState] = useState<ApiPromise | null>(null);
|
|
|
|
const groupedEndpoints = useMemo(() => {
|
|
const map = new Map<string, string>();
|
|
|
|
for (const endpoint of endpoints) {
|
|
const rpcProvider = endpoint.textBy;
|
|
|
|
if (typeof rpcProvider === 'string' && rpcProvider.length > 0 && rpcProvider !== 'Placeholder') {
|
|
map.set(endpoint.value, rpcProvider);
|
|
}
|
|
}
|
|
|
|
return map;
|
|
}, []);
|
|
|
|
const urls = useMemo(
|
|
() => {
|
|
let validUrls = url
|
|
? isString(url)
|
|
? [url]
|
|
: arrayShuffle(url.filter((u) => !u.startsWith('light://')))
|
|
: [];
|
|
|
|
const apiUrl = settings.apiUrl; // Selected chain URL
|
|
const apiUrlProvider = groupedEndpoints.get(apiUrl);
|
|
|
|
if (groupedEndpoints.has(apiUrl)) {
|
|
const matchIndex = validUrls.findIndex(
|
|
(u) => groupedEndpoints.get(u) === apiUrlProvider
|
|
);
|
|
|
|
// Move the RPC URL to the first position if it's provider matches the selected chain's provider
|
|
if (matchIndex > -1) {
|
|
const [match] = validUrls.splice(matchIndex, 1);
|
|
|
|
validUrls = [match, ...validUrls];
|
|
}
|
|
}
|
|
|
|
return validUrls;
|
|
},
|
|
[groupedEndpoints, url]
|
|
);
|
|
|
|
useEffect((): () => void => {
|
|
return (): void => {
|
|
providerRef.current = disconnect(providerRef.current);
|
|
};
|
|
}, []);
|
|
|
|
useEffect((): void => {
|
|
setState(null);
|
|
providerRef.current = disconnect(providerRef.current);
|
|
|
|
urls.length &&
|
|
ApiPromise
|
|
.create({
|
|
provider: (providerRef.current = new WsProvider(urls)),
|
|
typesBundle
|
|
})
|
|
.then((api) => mountedRef.current && setState(api))
|
|
.catch(console.error);
|
|
}, [mountedRef, providerRef, urls]);
|
|
|
|
return state;
|
|
}
|
|
|
|
export const useApiUrl = createNamedHook('useApiUrl', useApiUrlImpl);
|