From 737d43fd161f2e8caf806068b432fb5075693cad Mon Sep 17 00:00:00 2001 From: jialin Date: Tue, 16 Jun 2026 17:19:40 +0800 Subject: [PATCH] refactor: use hook for fetch chart --- src/pages/_components/bar-chart/index.tsx | 2 +- src/pages/usage/storage-tab/index.tsx | 44 +++++++++++------------ 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/pages/_components/bar-chart/index.tsx b/src/pages/_components/bar-chart/index.tsx index 94f88397..24432bb8 100644 --- a/src/pages/_components/bar-chart/index.tsx +++ b/src/pages/_components/bar-chart/index.tsx @@ -330,7 +330,7 @@ const BarChart: React.FC = (props) => {
{ const { creators: userOptions, volumes: volumeOptions } = useResourceMeta(scope); - const [chartData, setChartData] = useState( - null - ); // Bumped on any filter change to snap every mounted table back to page 1; // each table owns its own page/sort state otherwise. const [pageResetKey, setPageResetKey] = useState(0); + const { + detailData: chartData, + loading: chartLoading, + fetchData: fetchChartData + } = useQueryStorageBreakdown({ key: 'storageBreakdownChart' }); + const baseRequest = (): Omit => ({ start_date: dateRange[0].format('YYYY-MM-DD'), end_date: dateRange[1].format('YYYY-MM-DD'), @@ -127,23 +131,18 @@ const StorageTab: React.FC = () => { perPage: 50 }); - const fetchChart = async () => { - try { - const data = await queryStorageBreakdown({ - ...baseRequest(), - // Split each bucket by the chosen dimension when grouping. - group_by: chartGroupBy ? ['date', chartGroupBy] : ['date'], - // A trend is a time series, not a paginated table: always fetch the - // whole range. The default order is metric-desc, so partial (current/ - // recent) buckets have smaller values and would be pushed onto later - // pages — dropping the newest hours from the chart under a small page. - perPage: 10000 - }); - setChartData(data); - } catch { - // Surfacing handled by global interceptor; keep last data. - } - }; + const fetchChart = useMemoizedFn(() => + fetchChartData({ + ...baseRequest(), + // Split each bucket by the chosen dimension when grouping. + group_by: chartGroupBy ? ['date', chartGroupBy] : ['date'], + // A trend is a time series, not a paginated table: always fetch the + // whole range. The default order is metric-desc, so partial (current/ + // recent) buckets have smaller values and would be pushed onto later + // pages — dropping the newest hours from the chart under a small page. + perPage: 10000 + }) + ); useEffect(() => { fetchChart(); @@ -356,6 +355,7 @@ const StorageTab: React.FC = () => { groupBy={chartGroupBy} groupByOptions={chartGroupByOptions} onGroupByChange={(v) => setChartGroupBy(v as GroupKey | null)} + loading={chartLoading} />