|
@@ -1,13 +1,13 @@
|
|
|
import {createPageContext, createSearchContext, usePage, useTableSearch} from '@hooks';
|
|
|
-import {BaseListResult, ListParams, OriginalListParams} from '@models';
|
|
|
+import {BaseListResult, GetBaseListType, ListParams, OriginalListParams} from '@models';
|
|
|
import {useQuery} from '@tanstack/react-query';
|
|
|
import {shallowEqual} from '@utils';
|
|
|
import {useEffect, useRef, useState} from 'react';
|
|
|
|
|
|
-type UseQueryListResult<D> = [
|
|
|
+type UseQueryListResult<D extends BaseListResult> = [
|
|
|
{
|
|
|
count: number,
|
|
|
- data: D[],
|
|
|
+ data: GetBaseListType<D>[],
|
|
|
isFetching: boolean,
|
|
|
},
|
|
|
{refetch: () => void},
|
|
@@ -15,9 +15,9 @@ type UseQueryListResult<D> = [
|
|
|
|
|
|
type UseQueryListOptions<
|
|
|
P extends ListParams,
|
|
|
- R,
|
|
|
+ R extends BaseListResult,
|
|
|
> = {
|
|
|
- queryFn: (params: P) => BaseListResult<R>,
|
|
|
+ queryFn: (params: P) => R,
|
|
|
params: OriginalListParams<P>,
|
|
|
pageContext: ReturnType<typeof createPageContext>,
|
|
|
searchContext: ReturnType<typeof createSearchContext>,
|
|
@@ -25,7 +25,7 @@ type UseQueryListOptions<
|
|
|
|
|
|
export function useQueryList<
|
|
|
P extends ListParams,
|
|
|
- R,
|
|
|
+ R extends BaseListResult,
|
|
|
>(
|
|
|
{queryFn, params, pageContext, searchContext}: UseQueryListOptions<P, R>,
|
|
|
): UseQueryListResult<R> {
|
|
@@ -34,7 +34,7 @@ export function useQueryList<
|
|
|
const prevData = useRef<R[]>([]);
|
|
|
const prevParams = useRef(params);
|
|
|
|
|
|
- const {data = [], isFetching, refetch} = useQuery<R[]>(
|
|
|
+ const {data = [], isFetching, refetch} = useQuery<GetBaseListType<R>[]>(
|
|
|
[queryFn.name, page, pageSize, ...Object.values(params)],
|
|
|
async function() {
|
|
|
/**
|
|
@@ -88,11 +88,12 @@ export function useQueryList<
|
|
|
searchChange(isFetching);
|
|
|
}, [isFetching, searchChange]);
|
|
|
|
|
|
- return [{
|
|
|
- count,
|
|
|
- data,
|
|
|
- isFetching,
|
|
|
- },
|
|
|
- {refetch},
|
|
|
+ return [
|
|
|
+ {
|
|
|
+ count,
|
|
|
+ data,
|
|
|
+ isFetching,
|
|
|
+ },
|
|
|
+ {refetch},
|
|
|
];
|
|
|
}
|