mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* context for storing subchain config and tx view for subchain * address page and block page for subchain * address aggregated page: local txs tab * connect MultichainSelect to context * home page placeholder * refactor subchain select state * envs for demo * fix ts * clear value for l2 review * subchain widgets on home page * csp policies * sockets, duck and goose * fix socket on subchain address page * link builder for subchain views * update home widget * fix time increment * enable tx interpretator and metadata service * generate multichain config based on every chain app config * Fix the multichain config to work in Docker * multichain socket test * rename subchain-id to subchain-slug path param * refactoring * update chain icons on entities * home page: latest local txs * latest cross-chain txs * minor improvements * renaming, pt. 1 * rename chain routes * enable blockchain interaction * add loading state to icon shield * fix build * fix tests * update types package
52 lines
2.3 KiB
TypeScript
52 lines
2.3 KiB
TypeScript
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
import multichainConfig from 'configs/multichain';
|
|
import { useMultichainContext } from 'lib/contexts/multichain';
|
|
import type { Params as FetchParams } from 'lib/hooks/useFetch';
|
|
|
|
import type { ResourceError, ResourceName, ResourcePathParams, ResourcePayload } from './resources';
|
|
import useApiFetch from './useApiFetch';
|
|
|
|
export interface Params<R extends ResourceName, E = unknown, D = ResourcePayload<R>> {
|
|
pathParams?: ResourcePathParams<R>;
|
|
queryParams?: Record<string, string | Array<string> | number | boolean | undefined>;
|
|
fetchParams?: Pick<FetchParams, 'body' | 'method' | 'headers'>;
|
|
queryOptions?: Partial<Omit<UseQueryOptions<ResourcePayload<R>, ResourceError<E>, D>, 'queryFn'>>;
|
|
logError?: boolean;
|
|
chainSlug?: string;
|
|
}
|
|
|
|
export interface GetResourceKeyParams<R extends ResourceName, E = unknown, D = ResourcePayload<R>>
|
|
extends Pick<Params<R, E, D>, 'pathParams' | 'queryParams'> {
|
|
chainSlug?: string;
|
|
}
|
|
|
|
export function getResourceKey<R extends ResourceName>(resource: R, { pathParams, queryParams, chainSlug }: GetResourceKeyParams<R> = {}) {
|
|
if (pathParams || queryParams) {
|
|
return [ resource, chainSlug, { ...pathParams, ...queryParams } ].filter(Boolean);
|
|
}
|
|
|
|
return [ resource, chainSlug ].filter(Boolean);
|
|
}
|
|
|
|
export default function useApiQuery<R extends ResourceName, E = unknown, D = ResourcePayload<R>>(
|
|
resource: R,
|
|
{ queryOptions, pathParams, queryParams, fetchParams, logError, chainSlug }: Params<R, E, D> = {},
|
|
) {
|
|
const apiFetch = useApiFetch();
|
|
const { chain } = useMultichainContext() ||
|
|
{ chain: chainSlug ? multichainConfig()?.chains.find((chain) => chain.slug === chainSlug) : undefined };
|
|
|
|
return useQuery<ResourcePayload<R>, ResourceError<E>, D>({
|
|
queryKey: queryOptions?.queryKey || getResourceKey(resource, { pathParams, queryParams, chainSlug: chain?.slug }),
|
|
queryFn: async({ signal }) => {
|
|
// all errors and error typing is handled by react-query
|
|
// so error response will never go to the data
|
|
// that's why we are safe here to do type conversion "as Promise<ResourcePayload<R>>"
|
|
return apiFetch(resource, { pathParams, queryParams, chain, logError, fetchParams: { ...fetchParams, signal } }) as Promise<ResourcePayload<R>>;
|
|
},
|
|
...queryOptions,
|
|
});
|
|
}
|