diff --git a/client/src/pages/Dashboard.js b/client/src/pages/Dashboard.js index 4b6a401..482342a 100644 --- a/client/src/pages/Dashboard.js +++ b/client/src/pages/Dashboard.js @@ -1,4 +1,10 @@ -import React, { useState, useEffect, useMemo } from "react"; +import React, { + useState, + useEffect, + useMemo, + useRef, + useCallback, +} from "react"; import { RefreshCcw } from "lucide-react"; import { BarChart, @@ -29,10 +35,8 @@ const COLORS = [ "#e11d48", ]; -/* ============ 默认不勾的分类 ============ */ const DEFAULT_UNCHECKED = ["投资理财", "转账红包"]; -/* ============ 饼图 label ============ */ const RADIAN = Math.PI / 180; function renderPieLabel({ cx, cy, midAngle, outerRadius, percent, category }) { const r = outerRadius + 30; @@ -66,7 +70,6 @@ function renderPieLabel({ cx, cy, midAngle, outerRadius, percent, category }) { ); } -/* ============ 饼图数据:topN + 其他 ============ */ function buildPieData(stats, topN = 7) { const list = stats || []; if (!list.length) return { slices: [], total: 0 }; @@ -83,18 +86,16 @@ function buildPieData(stats, topN = 7) { } export default function Dashboard() { - const [raw, setRaw] = useState(null); // 后端全量 + const [raw, setRaw] = useState(null); const [monthly, setMonthly] = useState([]); const [loading, setLoading] = useState(true); - - // 勾选状态:Set const [checked, setChecked] = useState(() => new Set()); + const allRef = useRef(null); useEffect(() => { loadData(); }, []); - // 勾选变化 → 重新拉月度数据 useEffect(() => { if (!raw) return; const cats = [...checked]; @@ -107,7 +108,6 @@ export default function Dashboard() { const [ov, mo] = await Promise.all([getOverview(), getMonthly(12)]); setRaw(ov); setMonthly(mo); - // 初始化勾选 const allCats = new Set( (ov?.categoryBreakdown || []).map((c) => c.category), ); @@ -127,21 +127,27 @@ export default function Dashboard() { }); } - /* ============ 基于勾选计算 ============ */ + const allCats = (raw?.categoryBreakdown || []).map((c) => c.category); + const allChecked = checked.size === allCats.length && allCats.length > 0; + const noneChecked = checked.size === 0; + + function toggleAll() { + if (allChecked) { + setChecked(new Set()); + } else { + setChecked(new Set(allCats)); + } + } + const derived = useMemo(() => { if (!raw) return null; const cb = raw.categoryBreakdown || []; const cs = raw.categoryStats || []; - - // 勾选的分类 const sel = cb.filter((c) => checked.has(c.category)); const totalIncome = sel.reduce((s, c) => s + c.total_income, 0); const totalExpense = sel.reduce((s, c) => s + c.total_expense, 0); const dealCount = sel.reduce((s, c) => s + c.deal_count, 0); - - // 饼图数据(只取勾选的支出) const pieStats = cs.filter((c) => checked.has(c.category)); - return { sel, totalIncome, totalExpense, dealCount, pieStats }; }, [raw, checked]); @@ -174,7 +180,6 @@ export default function Dashboard() { - {/* ========== Stats Cards ========== */}
💰 总收入
@@ -212,7 +217,6 @@ export default function Dashboard() {
- {/* ========== Charts Row ========== */}
- {/* Monthly */}

📅 月度收支趋势

@@ -255,7 +258,6 @@ export default function Dashboard() { )}
- {/* Pie */}

🏷️ 支出分类占比

@@ -306,7 +308,6 @@ export default function Dashboard() {
- {/* ========== Category Table ========== */}

📋 分类收支明细

@@ -319,7 +320,23 @@ export default function Dashboard() { - +
勾选 + { + if (el) el.indeterminate = !allChecked && !noneChecked; + }} + checked={allChecked} + onChange={toggleAll} + title={allChecked ? "全部取消" : "全部勾选"} + style={{ + width: 16, + height: 16, + cursor: "pointer", + accentColor: "var(--primary)", + }} + /> + 分类 笔数 收入