feat(dashboard, risk): add SWR query hook and lazy load charts
This commit is contained in:
30
src/hooks/use-api-query.ts
Normal file
30
src/hooks/use-api-query.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import useSWR, { type SWRConfiguration, type SWRResponse } from "swr";
|
||||
|
||||
/**
|
||||
* 通用 API 查询 Hook —— 基于 SWR 的薄包装。
|
||||
*
|
||||
* @param key SWR 缓存键。传 `null` 表示暂不请求(条件请求)。
|
||||
* @param fetcher 无参异步函数,通常由 `api/*.ts` 的函数包装而来。
|
||||
* @param options SWR 配置覆盖。
|
||||
*
|
||||
* @example
|
||||
* ```tsx
|
||||
* const { data, error, isLoading } = useApiQuery(
|
||||
* ["admin/draws", page, status],
|
||||
* () => getAdminDraws({ page, status }),
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export function useApiQuery<Data = unknown, Error = unknown>(
|
||||
key: string | readonly unknown[] | null,
|
||||
fetcher: () => Promise<Data>,
|
||||
options?: SWRConfiguration<Data, Error>,
|
||||
): SWRResponse<Data, Error> {
|
||||
return useSWR<Data, Error>(key, fetcher, {
|
||||
revalidateOnFocus: false,
|
||||
dedupingInterval: 2_000,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user