feat(dashboard, risk): add SWR query hook and lazy load charts

This commit is contained in:
2026-06-10 13:58:31 +08:00
parent 1c80a3ae75
commit 13ae574aad
6 changed files with 108 additions and 37 deletions

View 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,
});
}