feat: 创建首页并集成账户、交易、预算等数据,实现并行数据加载和支出统计。
This commit is contained in:
@@ -99,7 +99,7 @@ function Home() {
|
|||||||
getSettings().catch(() => null),
|
getSettings().catch(() => null),
|
||||||
getBudgets().catch(() => []),
|
getBudgets().catch(() => []),
|
||||||
getStreakInfo().catch(() => null),
|
getStreakInfo().catch(() => null),
|
||||||
getTransactions({ startDate: rangeStartStr, endDate: rangeEndStr, type: 'expense', pageSize: 1000 }), // Bulk fetch
|
getTransactions({ startDate: rangeStartStr, endDate: rangeEndStr, pageSize: 1000 }), // Bulk fetch (All types for AI)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
setAccounts(accountsData || []);
|
setAccounts(accountsData || []);
|
||||||
@@ -111,30 +111,36 @@ function Home() {
|
|||||||
|
|
||||||
// In-Memory Aggregation for Daily Spend (Fixes Timezone Issues)
|
// In-Memory Aggregation for Daily Spend (Fixes Timezone Issues)
|
||||||
const allRangeTx = rangeTxData?.items || [];
|
const allRangeTx = rangeTxData?.items || [];
|
||||||
|
const expenseRangeTx = allRangeTx.filter(t => t.type === 'expense');
|
||||||
|
|
||||||
// Today
|
// Today (Expense Only)
|
||||||
const todayTxItems = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), today));
|
const todayTxItems = expenseRangeTx.filter(t => isSameDay(new Date(t.transactionDate), today));
|
||||||
setTodayTransactions(todayTxItems);
|
setTodayTransactions(todayTxItems);
|
||||||
setTodaySpend(calculateTotalExpense(todayTxItems));
|
setTodaySpend(calculateTotalExpense(todayTxItems));
|
||||||
|
|
||||||
// Yesterday
|
// Yesterday (Expense Only)
|
||||||
const yesterdayTxItems = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), yesterday));
|
const yesterdayTxItems = expenseRangeTx.filter(t => isSameDay(new Date(t.transactionDate), yesterday));
|
||||||
setYesterdaySpend(calculateTotalExpense(yesterdayTxItems));
|
setYesterdaySpend(calculateTotalExpense(yesterdayTxItems));
|
||||||
|
|
||||||
// Last Week Same Day
|
// Last Week Same Day (Expense Only)
|
||||||
const lastWeekTxItems = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), lastWeekSameDay));
|
const lastWeekTxItems = expenseRangeTx.filter(t => isSameDay(new Date(t.transactionDate), lastWeekSameDay));
|
||||||
setLastWeekSpend(calculateTotalExpense(lastWeekTxItems));
|
setLastWeekSpend(calculateTotalExpense(lastWeekTxItems));
|
||||||
|
|
||||||
// Last 7 Days Array (for chart)
|
// Last 7 Days Array (for chart - Expense Only)
|
||||||
const last7DaysSpendArray = [];
|
const last7DaysSpendArray = [];
|
||||||
const weekTxList: Transaction[] = [];
|
const weekTxList: Transaction[] = []; // Filtered by date, contains ALL types
|
||||||
|
|
||||||
for (let i = 6; i >= 0; i--) {
|
for (let i = 6; i >= 0; i--) {
|
||||||
const d = new Date(today);
|
const d = new Date(today);
|
||||||
d.setDate(d.getDate() - i);
|
d.setDate(d.getDate() - i);
|
||||||
const dayTx = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), d));
|
|
||||||
last7DaysSpendArray.push(calculateTotalExpense(dayTx));
|
// Expense for chart
|
||||||
weekTxList.push(...dayTx);
|
const dayExpenseTx = expenseRangeTx.filter(t => isSameDay(new Date(t.transactionDate), d));
|
||||||
|
last7DaysSpendArray.push(calculateTotalExpense(dayExpenseTx));
|
||||||
|
|
||||||
|
// All types for AI
|
||||||
|
const dayAllTx = allRangeTx.filter(t => isSameDay(new Date(t.transactionDate), d));
|
||||||
|
weekTxList.push(...dayAllTx);
|
||||||
}
|
}
|
||||||
setLast7DaysSpend(last7DaysSpendArray);
|
setLast7DaysSpend(last7DaysSpendArray);
|
||||||
setWeekTransactions(weekTxList);
|
setWeekTransactions(weekTxList);
|
||||||
|
|||||||
Reference in New Issue
Block a user