Migrate stats API to deals-based queries and add refresh button
This commit is contained in:
+112
-27
@@ -1,9 +1,39 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { TrendingUp, TrendingDown, AlertCircle, FileText, RefreshCcw } from 'lucide-react';
|
||||
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell } from 'recharts';
|
||||
import { getOverview, getMonthly, formatAmount } from '../hooks/useApi';
|
||||
import React, { useState, useEffect } from "react";
|
||||
import {
|
||||
TrendingUp,
|
||||
TrendingDown,
|
||||
AlertCircle,
|
||||
FileText,
|
||||
RefreshCcw,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
ResponsiveContainer,
|
||||
PieChart,
|
||||
Pie,
|
||||
Cell,
|
||||
} from "recharts";
|
||||
import { getOverview, getMonthly, formatAmount } from "../hooks/useApi";
|
||||
|
||||
const COLORS = ['#4f46e5', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#ec4899', '#06b6d4', '#f97316', '#84cc16', '#14b8a6', '#6366f1', '#e11d48'];
|
||||
const COLORS = [
|
||||
"#4f46e5",
|
||||
"#10b981",
|
||||
"#f59e0b",
|
||||
"#ef4444",
|
||||
"#8b5cf6",
|
||||
"#ec4899",
|
||||
"#06b6d4",
|
||||
"#f97316",
|
||||
"#84cc16",
|
||||
"#14b8a6",
|
||||
"#6366f1",
|
||||
"#e11d48",
|
||||
];
|
||||
|
||||
export default function Dashboard() {
|
||||
const [overview, setOverview] = useState(null);
|
||||
@@ -19,12 +49,12 @@ export default function Dashboard() {
|
||||
try {
|
||||
const [overviewData, monthlyData] = await Promise.all([
|
||||
getOverview(),
|
||||
getMonthly(12)
|
||||
getMonthly(12),
|
||||
]);
|
||||
setOverview(overviewData);
|
||||
setMonthly(monthlyData);
|
||||
} catch (err) {
|
||||
console.error('加载数据失败:', err);
|
||||
console.error("加载数据失败:", err);
|
||||
}
|
||||
setLoading(false);
|
||||
}
|
||||
@@ -42,26 +72,48 @@ export default function Dashboard() {
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="page-header">
|
||||
<h2>📊 财务仪表盘</h2>
|
||||
<p>全面掌握你的收支情况</p>
|
||||
<div
|
||||
className="page-header"
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "space-between",
|
||||
alignItems: "flex-start",
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
<h2>📊 财务仪表盘</h2>
|
||||
<p>全面掌握你的收支情况</p>
|
||||
</div>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
onClick={loadData}
|
||||
style={{ display: "inline-flex", alignItems: "center", gap: 6 }}
|
||||
>
|
||||
<RefreshCcw size={14} /> 刷新
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Stats Cards */}
|
||||
<div className="stats-grid">
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">💰 总收入</div>
|
||||
<div className="stat-value income">{formatAmount(ov.total_income)}</div>
|
||||
<div className="stat-value income">
|
||||
{formatAmount(ov.total_income)}
|
||||
</div>
|
||||
<div className="stat-sub">所有来源收入合计</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">💸 总支出</div>
|
||||
<div className="stat-value expense">{formatAmount(ov.total_expense)}</div>
|
||||
<div className="stat-value expense">
|
||||
{formatAmount(ov.total_expense)}
|
||||
</div>
|
||||
<div className="stat-sub">所有支出合计</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">📈 净收支</div>
|
||||
<div className={`stat-value ${(ov.total_income - ov.total_expense) >= 0 ? 'income' : 'expense'}`}>
|
||||
<div
|
||||
className={`stat-value ${ov.total_income - ov.total_expense >= 0 ? "income" : "expense"}`}
|
||||
>
|
||||
{formatAmount(ov.total_income - ov.total_expense)}
|
||||
</div>
|
||||
<div className="stat-sub">收入 - 支出</div>
|
||||
@@ -69,7 +121,9 @@ export default function Dashboard() {
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">📝 交易总数</div>
|
||||
<div className="stat-value primary">{ov.total_count || 0}</div>
|
||||
<div className="stat-sub">已导入 {ov.matched_count || 0} 笔已对账</div>
|
||||
<div className="stat-sub">
|
||||
共 {ov.matched_count || 0} 笔交易被记录
|
||||
</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-label">⚠️ 待确认</div>
|
||||
@@ -79,7 +133,14 @@ export default function Dashboard() {
|
||||
</div>
|
||||
|
||||
{/* Charts Row */}
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 20, marginBottom: 24 }}>
|
||||
<div
|
||||
style={{
|
||||
display: "grid",
|
||||
gridTemplateColumns: "1fr 1fr",
|
||||
gap: 20,
|
||||
marginBottom: 24,
|
||||
}}
|
||||
>
|
||||
{/* Monthly Trend */}
|
||||
<div className="card">
|
||||
<div className="card-header">
|
||||
@@ -95,8 +156,18 @@ export default function Dashboard() {
|
||||
formatter={(value) => formatAmount(value)}
|
||||
labelStyle={{ fontWeight: 600 }}
|
||||
/>
|
||||
<Bar dataKey="income" name="收入" fill="#10b981" radius={[4,4,0,0]} />
|
||||
<Bar dataKey="expense" name="支出" fill="#ef4444" radius={[4,4,0,0]} />
|
||||
<Bar
|
||||
dataKey="income"
|
||||
name="收入"
|
||||
fill="#10b981"
|
||||
radius={[4, 4, 0, 0]}
|
||||
/>
|
||||
<Bar
|
||||
dataKey="expense"
|
||||
name="支出"
|
||||
fill="#ef4444"
|
||||
radius={[4, 4, 0, 0]}
|
||||
/>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
) : (
|
||||
@@ -122,7 +193,9 @@ export default function Dashboard() {
|
||||
outerRadius={100}
|
||||
dataKey="total_amount"
|
||||
nameKey="category"
|
||||
label={({ category, percent }) => `${category} ${(percent * 100).toFixed(0)}%`}
|
||||
label={({ category, percent }) =>
|
||||
`${category} ${(percent * 100).toFixed(0)}%`
|
||||
}
|
||||
labelLine={false}
|
||||
>
|
||||
{overview.categoryStats.slice(0, 8).map((_, idx) => (
|
||||
@@ -162,18 +235,30 @@ export default function Dashboard() {
|
||||
{overview.sourceStats.map((s, i) => (
|
||||
<tr key={i}>
|
||||
<td>
|
||||
<span className={`source-type-label source-${s.source_type}`}>
|
||||
{s.source_type === 'alipay' && '支付宝'}
|
||||
{s.source_type === 'wechat' && '微信支付'}
|
||||
{s.source_type === 'bank_debit' && '银行借记卡'}
|
||||
{s.source_type === 'bank_credit' && '银行贷记卡'}
|
||||
{s.source_type === 'abc' && '农业银行'}
|
||||
<span
|
||||
className={`source-type-label source-${s.source_type}`}
|
||||
>
|
||||
{s.source_type === "alipay" && "支付宝"}
|
||||
{s.source_type === "wechat" && "微信支付"}
|
||||
{s.source_type === "bank_debit" && "银行借记卡"}
|
||||
{s.source_type === "bank_credit" && "银行贷记卡"}
|
||||
{s.source_type === "abc" && "农业银行"}
|
||||
</span>
|
||||
</td>
|
||||
<td>{s.count}</td>
|
||||
<td style={{ color: 'var(--success)' }}>{formatAmount(s.total_income)}</td>
|
||||
<td style={{ color: 'var(--danger)' }}>{formatAmount(s.total_expense)}</td>
|
||||
<td className={s.total_income - s.total_expense >= 0 ? 'stat-value income' : 'stat-value expense'}>
|
||||
<td style={{ color: "var(--success)" }}>
|
||||
{formatAmount(s.total_income)}
|
||||
</td>
|
||||
<td style={{ color: "var(--danger)" }}>
|
||||
{formatAmount(s.total_expense)}
|
||||
</td>
|
||||
<td
|
||||
className={
|
||||
s.total_income - s.total_expense >= 0
|
||||
? "stat-value income"
|
||||
: "stat-value expense"
|
||||
}
|
||||
>
|
||||
{formatAmount(s.total_income - s.total_expense)}
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user