From afec14b9132735394d541979673cc0abffc4a4e8 Mon Sep 17 00:00:00 2001 From: YoVinchen Date: Mon, 8 Dec 2025 14:28:50 +0800 Subject: [PATCH] fix(usage): stabilize date range to prevent infinite re-renders --- src/components/usage/UsageSummaryCards.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/components/usage/UsageSummaryCards.tsx b/src/components/usage/UsageSummaryCards.tsx index 12dc93d63..5256eadd4 100644 --- a/src/components/usage/UsageSummaryCards.tsx +++ b/src/components/usage/UsageSummaryCards.tsx @@ -1,3 +1,4 @@ +import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { useUsageSummary } from "@/lib/query/usage"; @@ -8,8 +9,13 @@ interface UsageSummaryCardsProps { export function UsageSummaryCards({ days }: UsageSummaryCardsProps) { const { t } = useTranslation(); - const endDate = Math.floor(Date.now() / 1000); - const startDate = endDate - days * 24 * 60 * 60; + + // 使用 useMemo 稳定时间戳,只在 days 变化时重新计算 + const { startDate, endDate } = useMemo(() => { + const end = Math.floor(Date.now() / 1000); + const start = end - days * 24 * 60 * 60; + return { startDate: start, endDate: end }; + }, [days]); const { data: summary, isLoading } = useUsageSummary(startDate, endDate); const totalRequests = summary?.totalRequests ?? 0;