Update Dashboard.js
This commit is contained in:
@@ -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<categoryName>
|
||||
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() {
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* ========== Stats Cards ========== */}
|
||||
<div className="stats-grid">
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">💰 总收入</div>
|
||||
@@ -212,7 +217,6 @@ export default function Dashboard() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ========== Charts Row ========== */}
|
||||
<div
|
||||
style={{
|
||||
display: "grid",
|
||||
@@ -221,7 +225,6 @@ export default function Dashboard() {
|
||||
marginBottom: 24,
|
||||
}}
|
||||
>
|
||||
{/* Monthly */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>📅 月度收支趋势</h3>
|
||||
@@ -255,7 +258,6 @@ export default function Dashboard() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Pie */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>🏷️ 支出分类占比</h3>
|
||||
@@ -306,7 +308,6 @@ export default function Dashboard() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ========== Category Table ========== */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
<h3>📋 分类收支明细</h3>
|
||||
@@ -319,7 +320,23 @@ export default function Dashboard() {
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th style={{ width: 40 }}>勾选</th>
|
||||
<th style={{ width: 40 }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
ref={(el) => {
|
||||
if (el) el.indeterminate = !allChecked && !noneChecked;
|
||||
}}
|
||||
checked={allChecked}
|
||||
onChange={toggleAll}
|
||||
title={allChecked ? "全部取消" : "全部勾选"}
|
||||
style={{
|
||||
width: 16,
|
||||
height: 16,
|
||||
cursor: "pointer",
|
||||
accentColor: "var(--primary)",
|
||||
}}
|
||||
/>
|
||||
</th>
|
||||
<th>分类</th>
|
||||
<th style={{ textAlign: "right" }}>笔数</th>
|
||||
<th style={{ textAlign: "right" }}>收入</th>
|
||||
|
||||
Reference in New Issue
Block a user