Files
reco/client/src/pages/Review.js
T

527 lines
17 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, useRef, useCallback } from "react";
import { Edit3, Loader2, Check, X } 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("");
const [debouncedKeyword, setDebouncedKeyword] = useState("");
const [confirmingId, setConfirmingId] = useState(null);
const sentinelRef = useRef(null);
const debounceRef = useRef(null);
useEffect(() => {
loadCategories();
}, []);
// 搜索防抖 300ms
useEffect(() => {
if (debounceRef.current) clearTimeout(debounceRef.current);
debounceRef.current = setTimeout(() => setDebouncedKeyword(keyword), 300);
return () => clearTimeout(debounceRef.current);
}, [keyword]);
useEffect(() => {
loadReviewDeals(1, false);
}, [showReviewed, debouncedKeyword]);
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 (debouncedKeyword) params.keyword = debouncedKeyword;
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) {}
}
const handleLoadMore = useCallback(() => {
if (loadingMore || page >= totalPages) return;
loadReviewDeals(page + 1, true);
}, [loadingMore, page, totalPages, showReviewed, debouncedKeyword]);
// 自动滚动加载:哨兵元素进入视口时触发
useEffect(() => {
const sentinel = sentinelRef.current;
if (!sentinel) return;
const observer = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) handleLoadMore();
},
{ rootMargin: "200px" },
);
observer.observe(sentinel);
return () => observer.disconnect();
}, [handleLoadMore]);
async function handleUpdateDeal(id, updates) {
try {
await updateDeal(id, updates);
setEditingDeal(null);
loadReviewDeals(1, false);
} catch (err) {}
}
// 行内快速确认:信任当前AI分类,一键标记已确认
async function handleQuickConfirm(deal) {
setConfirmingId(deal.id);
try {
await updateDeal(deal.id, { reviewed: 1 });
loadReviewDeals(1, false);
} catch (err) {}
setConfirmingId(null);
}
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" : ""}${deal.direction === "income" ? " income-border" : " expense-border"}`}
>
<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">{deal.category}</span>
)}
{deal.reviewed === 1 ? (
<span
className="tag tag-income"
style={{ marginLeft: 6 }}
>
✓ 已确认
</span>
) : (
<span
className="tag tag-unreviewed"
style={{ marginLeft: 6 }}
>
待确认
</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>
)}
{deal.reviewed !== 1 && (
<button
className="btn btn-success btn-sm"
onClick={() => handleQuickConfirm(deal)}
disabled={confirmingId === deal.id}
>
{confirmingId === deal.id ? (
<Loader2
size={12}
style={{ animation: "spin 0.8s linear infinite" }}
/>
) : (
<Check size={12} />
)}
确认
</button>
)}
<button
className="btn btn-secondary btn-sm"
onClick={() => setEditingDeal({ ...deal })}
>
<Edit3 size={12} /> {deal.reviewed ? "修改" : "编辑"}
</button>
</div>
</div>
))}
{/* 自动加载哨兵 */}
{hasMore && (
<div
ref={sentinelRef}
style={{ textAlign: "center", padding: "24px 0 4px" }}
>
{loadingMore && (
<span
style={{ fontSize: 13, color: "var(--text-secondary)" }}
>
<Loader2
size={14}
style={{
animation: "spin 0.8s linear infinite",
marginRight: 6,
verticalAlign: "middle",
}}
/>
加载中...({deals.length} / {total})
</span>
)}
</div>
)}
{/* 全部加载完成 */}
{!hasMore && deals.length > 0 && (
<div
style={{
textAlign: "center",
padding: "24px 0 4px",
fontSize: 13,
color: "var(--text-secondary)",
}}
>
已全部加载 · 共 {total} 笔
</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-secondary"
onClick={() =>
handleUpdateDeal(editingDeal.id, {
category: editingDeal.category,
ai_description: editingDeal.ai_description,
})
}
>
仅保存
</button>
<button
className="btn btn-primary"
onClick={() =>
handleUpdateDeal(editingDeal.id, {
category: editingDeal.category,
ai_description: editingDeal.ai_description,
reviewed: 1,
})
}
>
<Check size={14} /> 保存并确认
</button>
</div>
</div>
</div>
)}
</div>
);
}