mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* show flag next to epoch block * block view customizations * add hint with link * display block epoch transfers * show epoch election reward types * show reward details * show base fee token from API * display current epoch on main page * mobile view * tests * add infinite scroll loading to reward details resource * update useApiQuery options * update screenshots * update layout of reward details on desktop * change hints of block epoch reward distribution
42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
import type { UseQueryOptions } from '@tanstack/react-query';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
|
|
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'>>;
|
|
}
|
|
|
|
export function getResourceKey<R extends ResourceName>(resource: R, { pathParams, queryParams }: Params<R> = {}) {
|
|
if (pathParams || queryParams) {
|
|
return [ resource, { ...pathParams, ...queryParams } ];
|
|
}
|
|
|
|
return [ resource ];
|
|
}
|
|
|
|
export default function useApiQuery<R extends ResourceName, E = unknown, D = ResourcePayload<R>>(
|
|
resource: R,
|
|
{ queryOptions, pathParams, queryParams, fetchParams }: Params<R, E, D> = {},
|
|
) {
|
|
const apiFetch = useApiFetch();
|
|
|
|
return useQuery<ResourcePayload<R>, ResourceError<E>, D>({
|
|
// eslint-disable-next-line @tanstack/query/exhaustive-deps
|
|
queryKey: queryOptions?.queryKey || getResourceKey(resource, { pathParams, queryParams }),
|
|
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, fetchParams: { ...fetchParams, signal } }) as Promise<ResourcePayload<R>>;
|
|
},
|
|
...queryOptions,
|
|
});
|
|
}
|