优化:查询按范围分步加载并精简表格聚合
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { api } from './api';
|
||||
import { overviewQuery, splitMetricsLoad } from './priorityMetrics';
|
||||
import type { MetricsResponse, QueryParams } from './types';
|
||||
|
||||
/** 优先读取正式小型汇总;失败时允许后续正式查询独立回退。 */
|
||||
export function usePriorityMetrics(query: QueryParams | null, enabled: boolean, refresh: string | number,
|
||||
onUnauthorized: (error: unknown) => boolean) {
|
||||
const key = JSON.stringify([query, refresh]);
|
||||
const split = !!query && splitMetricsLoad(query);
|
||||
const [result, setResult] = useState<{ key: string; value?: MetricsResponse; failed?: boolean } | null>(null);
|
||||
useEffect(() => {
|
||||
if (!enabled || !query) return;
|
||||
const controller = new AbortController();
|
||||
api.metrics(split ? overviewQuery(query) : query, controller.signal)
|
||||
.then(value => { if (!controller.signal.aborted) setResult({ key, value }); })
|
||||
.catch(error => {
|
||||
if (!controller.signal.aborted && !onUnauthorized(error)) setResult({ key, failed: true });
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [enabled, key, onUnauthorized]);
|
||||
const current = enabled && result?.key === key ? result : null;
|
||||
return { value: current?.value || null, settled: !!current, failed: !!current?.failed, split };
|
||||
}
|
||||
Reference in New Issue
Block a user