Files
reco/client/src/pages/Review.js
T
fukunee 1598455f5f update route,
reducing unnecessary complexity.
 Update Dashboard and Review pages, refactor statistics endpoint

Replace source-based summary with categorized breakdown on Dashboard,
remove
batch confirm and individual confirm actions from Review page, and
update
server stats route to return category breakdown with max income/expense
details
2026-05-06 02:02:39 +08:00

448 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useState, useEffect } from "react";
import { Edit3, Loader2 } from "lucide-react";
import {
getDeals,
updateDeal,
formatAmount,
formatDate,
SOURCE_TYPE_MAP,
getCategories,
} from "../hooks/useApi";
const PAGE_SIZE = 20;
export default function Review() {
const [deals, setDeals] = useState([]);
const [loading, setLoading] = useState(true);
const [loadingMore, setLoadingMore] = useState(false);
const [total, setTotal] = useState(0);
const [page, setPage] = useState(1);
const [totalPages, setTotalPages] = useState(0);
const [categories, setCategories] = useState([]);
const [editingDeal, setEditingDeal] = useState(null);
const [showReviewed, setShowReviewed] = useState(true);
const [keyword, setKeyword] = useState("");
useEffect(() => {
loadCategories();
}, []);
useEffect(() => {
loadReviewDeals(1, false);
}, [showReviewed, keyword]);
async function loadCategories() {
try {
const cats = await getCategories();
setCategories(cats);
} catch (err) {}
}
async function loadReviewDeals(p, append) {
const isFirstPage = !append;
if (isFirstPage) {
setLoading(true);
setPage(1);
} else {
setLoadingMore(true);
}
try {
const params = {
min_amount: 100,
limit: PAGE_SIZE,
page: p || 1,
sort_by: "amount",
sort_order: "DESC",
};
if (!showReviewed) params.reviewed = 0;
if (keyword) params.keyword = keyword;
const result = await getDeals(params);
if (append) {
setDeals((prev) => [...prev, ...result.deals]);
} else {
setDeals(result.deals);
}
setTotal(result.total);
setPage(result.page);
setTotalPages(result.totalPages);
updateBadge(result, isFirstPage);
} catch (err) {}
setLoading(false);
setLoadingMore(false);
}
async function updateBadge(result, isFirstPage) {
try {
const badge = document.getElementById("review-badge");
if (!badge) return;
if (!showReviewed) {
badge.textContent = result.total;
} else if (isFirstPage) {
const unreviewed = await getDeals({
min_amount: 100,
reviewed: 0,
limit: 1,
});
badge.textContent = unreviewed.total;
}
} catch (e) {}
}
function handleLoadMore() {
if (loadingMore || page >= totalPages) return;
loadReviewDeals(page + 1, true);
}
async function handleUpdateDeal(id, updates) {
try {
await updateDeal(id, updates);
setEditingDeal(null);
loadReviewDeals(1, false);
} catch (err) {}
}
const hasMore = page < totalPages;
return (
<div>
<div className="page-header">
<h2>🔍 大额交易确认</h2>
<p>逐笔确认 ≥100 元的 Deal(关联交易组),核对AI分类和描述是否正确</p>
</div>
{/* Toolbar */}
<div
style={{
display: "flex",
gap: 10,
marginBottom: 20,
flexWrap: "wrap",
alignItems: "center",
}}
>
<div className="form-group" style={{ marginBottom: 0, minWidth: 200 }}>
<input
type="text"
className="form-input"
placeholder="🔍 搜索对方/描述..."
value={keyword}
onChange={(e) => setKeyword(e.target.value)}
/>
</div>
<label
style={{
display: "flex",
alignItems: "center",
gap: 6,
cursor: "pointer",
fontSize: 14,
}}
>
<input
type="checkbox"
checked={showReviewed}
onChange={(e) => setShowReviewed(e.target.checked)}
/>
显示已确认
</label>
<div style={{ flex: 1 }} />
<span style={{ fontSize: 14, color: "var(--text-secondary)" }}>
已加载 <strong>{deals.length}</strong> / <strong>{total}</strong> 笔
{!showReviewed && (
<span style={{ color: "var(--warning)", marginLeft: 4 }}>
(待确认)
</span>
)}
</span>
</div>
{/* Review List */}
<div className="card">
{loading ? (
<div className="loading">
<div className="spinner"></div>加载中...
</div>
) : deals.length > 0 ? (
<div>
{deals.map((deal) => (
<div
key={deal.id}
className={`review-item ${deal.reviewed ? "reviewed" : ""}`}
>
<div
className={`amount-col ${deal.direction === "income" ? "stat-value income" : "stat-value expense"}`}
>
{formatAmount(deal.amount)}
</div>
<div className="info-col">
<div className="title">
{deal.ai_description || deal.description || "无描述"}
</div>
<div className="meta">
{formatDate(deal.deal_date)} ·
{deal.counterparty && <span> {deal.counterparty} ·</span>}
{deal.category && (
<span
className="tag tag-matched"
style={{ marginLeft: 4 }}
>
{deal.category}
</span>
)}
{deal.reviewed && (
<span style={{ marginLeft: 6, color: "var(--success)" }}>
✓ 已确认
</span>
)}
{deal.match_count > 1 && (
<span
className="tag tag-transfer"
style={{ marginLeft: 6 }}
>
关联×{deal.match_count}
</span>
)}
</div>
</div>
<div className="action-col">
{deal.transactions && deal.transactions.length > 0 && (
<span
className="tag tag-transfer"
style={{ fontSize: 11, padding: "3px 8px" }}
>
{deal.transactions.length}笔
</span>
)}
<button
className="btn btn-secondary btn-sm"
onClick={() => setEditingDeal({ ...deal })}
>
<Edit3 size={12} /> {deal.reviewed ? "修改" : "编辑"}
</button>
</div>
</div>
))}
{/* Load More */}
{hasMore && (
<div style={{ textAlign: "center", padding: "16px 0 4px" }}>
<button
className="btn btn-secondary"
onClick={handleLoadMore}
disabled={loadingMore}
style={{ minWidth: 160 }}
>
{loadingMore ? (
<>
<Loader2
size={14}
style={{ animation: "spin 0.8s linear infinite" }}
/>{" "}
加载中...
</>
) : (
<>
加载更多({deals.length} / {total})
</>
)}
</button>
</div>
)}
</div>
) : (
<div className="empty-state">
<div className="icon">✅</div>
<p>
{showReviewed
? "没有已确认的大额 Deal"
: "所有大额 Deal 已确认!"}
</p>
<p style={{ fontSize: 13, marginTop: 8 }}>
{showReviewed
? "尝试切换显示模式"
: "可以去交易明细页面查看所有记录"}
</p>
</div>
)}
</div>
{/* Edit Modal */}
{editingDeal && (
<div className="modal-overlay" onClick={() => setEditingDeal(null)}>
<div className="modal" onClick={(e) => e.stopPropagation()}>
<h3>编辑 Deal 分类与描述</h3>
<div
style={{
marginBottom: 16,
padding: 12,
background: "var(--bg)",
borderRadius: 8,
fontSize: 13,
}}
>
<div style={{ fontWeight: 600, marginBottom: 4 }}>
{formatAmount(editingDeal.amount)} ·{" "}
{formatDate(editingDeal.deal_date)}
</div>
<div style={{ color: "var(--text-secondary)" }}>
{editingDeal.counterparty} ·{" "}
{editingDeal.description || "无描述"}
</div>
{editingDeal.match_count > 1 && (
<div
style={{
fontSize: 11,
color: "var(--primary)",
marginTop: 2,
}}
>
关联 {editingDeal.match_count} 笔原始交易
</div>
)}
{editingDeal.ai_description && (
<div
style={{
marginTop: 8,
padding: "6px 10px",
background: "#eef2ff",
borderRadius: 6,
fontSize: 12,
color: "var(--primary)",
}}
>
🤖 AI:{editingDeal.ai_description}
</div>
)}
</div>
{editingDeal.transactions &&
editingDeal.transactions.length > 0 && (
<div
style={{
marginBottom: 16,
padding: 10,
background: "var(--bg)",
borderRadius: 6,
fontSize: 12,
maxHeight: 120,
overflowY: "auto",
}}
>
<div
style={{
fontWeight: 600,
marginBottom: 6,
color: "var(--text-secondary)",
fontSize: 11,
}}
>
关联原始交易明细:
</div>
{editingDeal.transactions.map((tx, i) => (
<div
key={i}
style={{
display: "flex",
gap: 8,
alignItems: "center",
padding: "3px 0",
fontSize: 11,
color: "var(--text-secondary)",
}}
>
<span
className={`source-type-label source-${tx.source_type}`}
>
{SOURCE_TYPE_MAP[tx.source_type] || tx.source_type}
</span>
<span style={{ flex: 1 }}>
{tx.description || "无描述"} ·{" "}
{tx.counterparty || "未知"}
</span>
<span
style={{
fontWeight: 500,
color:
tx.direction === "income"
? "var(--success)"
: "var(--danger)",
}}
>
{formatAmount(tx.amount)}
</span>
</div>
))}
</div>
)}
<div className="form-group">
<label>分类</label>
<select
className="form-select"
value={editingDeal.category || ""}
onChange={(e) =>
setEditingDeal({ ...editingDeal, category: e.target.value })
}
>
<option value="">选择分类</option>
{categories.map((c) => (
<option key={c.id} value={c.name}>
{c.name}
</option>
))}
</select>
</div>
<div className="form-group">
<label>描述</label>
<input
type="text"
className="form-input"
value={editingDeal.ai_description || ""}
onChange={(e) =>
setEditingDeal({
...editingDeal,
ai_description: e.target.value,
})
}
placeholder="简要描述这笔交易..."
/>
</div>
<div
style={{
display: "flex",
gap: 10,
justifyContent: "flex-end",
marginTop: 20,
}}
>
<button
className="btn btn-secondary"
onClick={() => setEditingDeal(null)}
>
取消
</button>
<button
className="btn btn-primary"
onClick={() =>
handleUpdateDeal(editingDeal.id, {
category: editingDeal.category,
ai_description: editingDeal.ai_description,
reviewed: 1,
})
}
>
保存并确认
</button>
</div>
</div>
</div>
)}
</div>
);
}