// React Query hook for platform.getBlockchains. // Returns the list of all blockchains registered on the P-chain. // Uses the server-side /v1/node/p-chain proxy to bypass CORS. import { useQuery } from '@tanstack/react-query'; import React from 'react'; import type { PChainBlockchain } from './types'; const BLOCKCHAINS_STALE_TIME_MS = 300_000; const BLOCKCHAINS_QUERY_KEY = 'pchain:blockchains' as const; const EMPTY_BLOCKCHAINS: ReadonlyArray = []; async function fetchBlockchains(): Promise> { const res = await fetch('/v1/node/p-chain', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', method: 'platform.getBlockchains', params: {}, id: 1, }), }); if (!res.ok) { throw new Error(`P-chain proxy returned ${ res.status }`); } const json = await res.json() as { result?: { blockchains?: ReadonlyArray } }; return json.result?.blockchains ?? []; } export function useBlockchains() { const query = useQuery({ queryKey: [ BLOCKCHAINS_QUERY_KEY ], queryFn: fetchBlockchains, staleTime: BLOCKCHAINS_STALE_TIME_MS, retry: 2, }); const blockchains = React.useMemo( () => query.data ?? EMPTY_BLOCKCHAINS, [ query.data ], ); return { blockchains, isLoading: query.isLoading, isError: query.isError, error: query.error, refetch: query.refetch, }; }