From 8aa881bcc1e6c4c25e4176248e93d9ceff4878ff Mon Sep 17 00:00:00 2001 From: admin <1297598740@qq.com> Date: Thu, 29 Jan 2026 08:48:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=AF=8F=E6=97=A5?= =?UTF-8?q?=E6=B4=9E=E5=AF=9F=E5=8D=A1=E7=89=87=E7=BB=84=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E6=B6=88=E8=B4=B9=E5=92=8C=E9=A2=84=E7=AE=97?= =?UTF-8?q?=E5=88=86=E6=9E=90=E5=B9=B6=E9=9B=86=E6=88=90AI=E6=B4=9E?= =?UTF-8?q?=E5=AF=9F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DailyInsightCard/DailyInsightCard.tsx | 2 +- src/pages/Home/Home.tsx | 46 ++++++++----------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/components/home/DailyInsightCard/DailyInsightCard.tsx b/src/components/home/DailyInsightCard/DailyInsightCard.tsx index a0647a1..505ace7 100644 --- a/src/components/home/DailyInsightCard/DailyInsightCard.tsx +++ b/src/components/home/DailyInsightCard/DailyInsightCard.tsx @@ -136,7 +136,7 @@ export const DailyInsightCard: React.FC = ({
- {aiData ? 'AI 每日简报' : '每日简报'} + {aiData ? 'AI 消费简报' : '消费简报'} {aiData?.emoji && {aiData.emoji}} {isAiLoading && !aiData && AI 思考中...}
diff --git a/src/pages/Home/Home.tsx b/src/pages/Home/Home.tsx index 4f1ff31..837c565 100644 --- a/src/pages/Home/Home.tsx +++ b/src/pages/Home/Home.tsx @@ -21,6 +21,7 @@ import { HealthScoreModal } from '../../components/home/HealthScoreModal/HealthS import { ContributionModal } from '../../components/common/ContributionGraph/ContributionModal'; import { DailyInsightCard } from '../../components/home/DailyInsightCard/DailyInsightCard'; // Import import { getBudgets } from '../../services/budgetService'; // Import +import { toLocalDateString, isSameDay } from '../../utils/dateUtils'; import type { Account, Transaction, Category, Ledger, UserSettings } from '../../types'; @@ -58,6 +59,7 @@ function Home() { const [todayTransactions, setTodayTransactions] = useState([]); const [lastWeekSpend, setLastWeekSpend] = useState(0); const [last7DaysSpend, setLast7DaysSpend] = useState([]); // New: 最近7天支出 + const [weekTransactions, setWeekTransactions] = useState([]); // New: 最近7天交易详情 (for AI) useEffect(() => { @@ -69,20 +71,9 @@ function Home() { setLoading(true); setError(null); - // Helper for local date string YYYY-MM-DD - const toLocalDate = (d: Date) => { - const year = d.getFullYear(); - const month = String(d.getMonth() + 1).padStart(2, '0'); - const day = String(d.getDate()).padStart(2, '0'); - return `${year}-${month}-${day}`; - }; - // Helper to check if two dates are the same day (using local time) - const isSameDay = (d1: Date, d2: Date) => { - return d1.getFullYear() === d2.getFullYear() && - d1.getMonth() === d2.getMonth() && - d1.getDate() === d2.getDate(); - }; + + // Helper for local date string YYYY-MM-DD - REMOVED, using imported utils const today = new Date(); const yesterday = new Date(today); @@ -96,8 +87,8 @@ function Home() { const rangeEnd = new Date(today); rangeEnd.setDate(rangeEnd.getDate() + 1); - const rangeStartStr = toLocalDate(rangeStart); - const rangeEndStr = toLocalDate(rangeEnd); + const rangeStartStr = toLocalDateString(rangeStart); + const rangeEndStr = toLocalDateString(rangeEnd); // Parallel fetch optimized: Single request for transaction history const [accountsData, recentTxData, categoriesData, ledgersData, settingsData, budgetsData, streakData, rangeTxData] = await Promise.all([ @@ -136,13 +127,17 @@ function Home() { // Last 7 Days Array (for chart) const last7DaysSpendArray = []; + const weekTxList: Transaction[] = []; + for (let i = 6; i >= 0; i--) { const d = new Date(today); d.setDate(d.getDate() - i); const dayTx = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), d)); last7DaysSpendArray.push(calculateTotalExpense(dayTx)); + weekTxList.push(...dayTx); } setLast7DaysSpend(last7DaysSpendArray); + setWeekTransactions(weekTxList); // Monthly Budget Stats let mTotal = 0; @@ -168,20 +163,17 @@ function Home() { }; const { maxTransaction, topCategory, top3Categories, todayTransactionCount, avgDailySpend } = useMemo(() => { - if (todayTransactions.length === 0) return { - maxTransaction: undefined, - topCategory: undefined, - top3Categories: undefined, - todayTransactionCount: 0, - avgDailySpend: 0 - }; + // Basic stats + const todayCount = todayTransactions.length; - // Max Transaction + // Max Transaction (Keep as Today's max for immediate context) const maxTx = [...todayTransactions].sort((a, b) => b.amount - a.amount)[0]; - // Category aggregation + // Category aggregation - Use Week Transactions for better AI context + const sourceTransactions = weekTransactions.length > 0 ? weekTransactions : todayTransactions; + const catMap: Record = {}; - todayTransactions.forEach((t) => { + sourceTransactions.forEach((t) => { catMap[t.categoryId] = (catMap[t.categoryId] || 0) + t.amount; }); @@ -210,10 +202,10 @@ function Home() { maxTransaction: maxTx, topCategory: topCatName ? { name: topCatName, amount: catMap[Number(sortedCatIds[0])] } : undefined, top3Categories: top3.length > 0 ? top3 : undefined, - todayTransactionCount: todayTransactions.length, + todayTransactionCount: todayCount, avgDailySpend: avgDaily }; - }, [todayTransactions, categories, monthlyBudgetSpentTotal]); + }, [todayTransactions, weekTransactions, categories, monthlyBudgetSpentTotal]); const handleQuickTransaction = () => { navigate('/transactions?action=new');