| 12345678910111213141516171819202122232425262728293031323334353637 |
- export interface PagedExporterOptions<TParams> {
- pageSize?: number;
- request: (params: TParams & { current: number; pageSize: number }) => Promise<{ data: any[]; total: number } | undefined>;
- }
- export interface PagedExporterResult {
- fetchData: () => Promise<{ currentCount: number; totalCount: number }>;
- reset: () => void;
- getAll: () => any[];
- }
- // 可复用的分页导出 Hook(无 React 依赖,简单状态机)
- export function createPagedExporter<TParams = any>(options: PagedExporterOptions<TParams>) {
- const state = {
- current: 0,
- totalData: [] as any[],
- pageSize: options.pageSize || 100,
- };
- const fetchData = async (passThroughParams: TParams): Promise<{ currentCount: number; totalCount: number }> => {
- state.current += 1;
- const resp = await options.request({ ...(passThroughParams as any), current: state.current, pageSize: state.pageSize });
- const total = resp?.total || 0;
- const data = resp?.data || [];
- state.totalData = [...state.totalData, ...data];
- return { currentCount: state.totalData.length, totalCount: total };
- };
- const reset = () => {
- state.current = 0;
- state.totalData = [];
- };
- const getAll = () => state.totalData;
- return { fetchData, reset, getAll } as const;
- }
|