diff --git a/client/src/pages/Transactions.js b/client/src/pages/Transactions.js index 506e582..5f9c1fa 100644 --- a/client/src/pages/Transactions.js +++ b/client/src/pages/Transactions.js @@ -1,6 +1,6 @@ import React, { useState, useEffect } from 'react'; import { RotateCcw, Link2, Brain, ChevronLeft, ChevronRight, ArrowUpDown, ArrowUp, ArrowDown } from 'lucide-react'; -import { getTransactions, updateTransaction, runReconciliation, classifyAll, classifyTransactions, formatAmount, SOURCE_TYPE_MAP, getCategories } from '../hooks/useApi'; +import { getTransactions, runReconciliation, classifyTransactions, formatAmount, SOURCE_TYPE_MAP, getCategories } from '../hooks/useApi'; function fmtDatetime(dt) { if (!dt) return ''; @@ -11,18 +11,17 @@ export default function Transactions() { const [data, setData] = useState({ transactions: [], total: 0, page: 1, totalPages: 0 }); const [filters, setFilters] = useState({ page: 1, limit: 50, start_date: '', end_date: '', - source_type: '', direction: '', category: '', reviewed: '', min_amount: '', keyword: '', + source_type: '', direction: '', keyword: '', sort_by: 'transaction_time', sort_order: 'DESC' }); const [loading, setLoading] = useState(true); const [categories, setCategories] = useState([]); - const [editingTx, setEditingTx] = useState(null); const [reconciling, setReconciling] = useState(false); const [classifying, setClassifying] = useState(false); const [classifyProgress, setClassifyProgress] = useState(0); const [deals, setDeals] = useState(null); - // 恢复分类进度(刷新后轮询) + // 恢复分类进度 useEffect(() => { const saved = sessionStorage.getItem('classifyTask'); if (!saved) return; @@ -35,67 +34,44 @@ export default function Transactions() { const { total } = await res.json(); const p = task.total > 0 ? Math.round((task.total - total) / task.total * 100) : 100; setClassifyProgress(Math.min(Math.max(p, 0), 100)); - if (total <= 0 || p >= 100) { - clearInterval(poll); setClassifying(false); setClassifyProgress(100); - sessionStorage.removeItem('classifyTask'); loadTransactions(); - } + if (total <= 0 || p >= 100) { clearInterval(poll); setClassifying(false); sessionStorage.removeItem('classifyTask'); loadTransactions(); } } catch (e) { clearInterval(poll); setClassifying(false); sessionStorage.removeItem('classifyTask'); } }, 2000); return () => clearInterval(poll); }, []); - useEffect(() => { loadCategories(); }, []); + useEffect(() => { loadCategories(); loadDeals(); }, []); useEffect(() => { loadTransactions(); }, [filters]); - async function loadCategories() { - try { setCategories(await getCategories()); } catch (err) {} - } + async function loadCategories() { try { setCategories(await getCategories()); } catch (e) {} } + async function loadDeals() { try { const r = await fetch('/api/deals'); setDeals((await r.json()).deals); } catch (e) {} } async function loadTransactions() { setLoading(true); try { - const params = {}; - for (const [k, v] of Object.entries(filters)) if (v !== '' && v !== undefined) params[k] = v; - setData(await getTransactions(params)); - } catch (err) {} + const p = {}; for (const [k, v] of Object.entries(filters)) if (v !== '' && v !== undefined) p[k] = v; + setData(await getTransactions(p)); + } catch (e) {} setLoading(false); } - function updateFilter(key, value) { - setFilters(prev => ({ ...prev, [key]: value, page: key === 'page' ? value : 1 })); - } + function updateFilter(k, v) { setFilters(prev => ({ ...prev, [k]: v, page: k === 'page' ? v : 1 })); } function toggleSort(field) { - setFilters(prev => ({ - ...prev, sort_by: field, - sort_order: prev.sort_by === field && prev.sort_order === 'DESC' ? 'ASC' : 'DESC', page: 1 - })); + setFilters(prev => ({ ...prev, sort_by: field, sort_order: prev.sort_by === field && prev.sort_order === 'DESC' ? 'ASC' : 'DESC', page: 1 })); } function SortIcon({ field }) { if (filters.sort_by !== field) return ; return filters.sort_order === 'ASC' ? : ; } - function Th({ field, children, style }) { - return ( - toggleSort(field)} style={{ cursor: 'pointer', userSelect: 'none', ...style }}> - - {children} - - - ); + return toggleSort(field)} style={{ cursor: 'pointer', userSelect: 'none', ...style }}>{children}; } - async function loadDeals() { - try { const res = await fetch("/api/deals"); const d = await res.json(); setDeals(d.deals); } catch (e) {} - } - - useEffect(() => { loadDeals(); }, []); - async function handleReconcile() { setReconciling(true); - try { const r = await runReconciliation(); loadDeals(); alert(`对账完成!成功匹配 ${r.matched} 对`); loadTransactions(); } catch (err) {} + try { const r = await runReconciliation(); alert('匹配 ' + (r.matched || 0) + ' 对,生成 ' + (r.deals || 0) + ' 条关联'); loadTransactions(); loadDeals(); } catch (e) {} setReconciling(false); } @@ -116,60 +92,33 @@ export default function Transactions() { sessionStorage.setItem('classifyTask', JSON.stringify({ total, progress: pct, ts: Date.now() })); } sessionStorage.removeItem('classifyTask'); - alert('分类完成!共重分类 ' + total + ' 笔交易'); + alert('分类完成!共重分类 ' + total + ' 笔'); loadTransactions(); } catch (e) { sessionStorage.removeItem('classifyTask'); alert('分类失败: ' + (e.response?.data?.error || e.message)); } setClassifying(false); } - async function handleUpdateTx(id, updates) { - try { await updateTransaction(id, updates); setEditingTx(null); loadTransactions(); } catch (err) {} - } - return (
-
-

📋 交易明细

-

查看、筛选、排序、对账和分类所有交易记录

-
+

📋 交易明细

查看、筛选、排序、对账和分类

- - - -
- -
-
-
updateFilter('start_date', e.target.value)} />
-
updateFilter('end_date', e.target.value)} />
-
-
-
-
-
updateFilter('keyword', e.target.value)} />
-
+ + +
+ {/* 筛选+表格 合体卡片 */}
-
+
+
updateFilter('start_date', e.target.value)} />
+
updateFilter('end_date', e.target.value)} />
+
+
+
updateFilter('keyword', e.target.value)} />
+
+ +

共 {data.total} 笔交易

第 {data.page}/{data.totalPages} 页 · 点击列头排序
@@ -178,50 +127,32 @@ export default function Transactions() {
加载中...
) : data.transactions.length > 0 ? ( <> -
- - - - - - - - - - +
日期时间来源对方描述金额方向操作
+ + + + + + + + + + {data.transactions.map(tx => ( + + + + + + + - - - {data.transactions.map(tx => ( - - - - - - - - - - - ))} - -
日期时间来源对方描述金额方向
{fmtDatetime(tx.transaction_time) || fmtDatetime(tx.transaction_date)}{SOURCE_TYPE_MAP[tx.source_type] || tx.source_type}{tx.counterparty || '--'}{tx.description || '--'}{formatAmount(tx.amount)}{tx.direction === 'income' ? '收入' : '支出'}
- {fmtDatetime(tx.transaction_time) || fmtDatetime(tx.transaction_date)} - {SOURCE_TYPE_MAP[tx.source_type] || tx.source_type}{tx.counterparty || '--'} - {tx.ai_description || tx.description || '--'} - {tx.category ? {tx.category} : 未分类} - {formatAmount(tx.amount)} - {tx.direction === 'income' ? '收入' : '支出'}
-
- + ))} + +
{data.totalPages > 1 && (
- {Array.from({ length: Math.min(data.totalPages, 7) }, (_, i) => { - const p = i + Math.max(1, data.page - 3); - if (p > data.totalPages) return null; - return ; - })} + {Array.from({ length: Math.min(data.totalPages, 7) }, (_, i) => { const p = i + Math.max(1, data.page - 3); if (p > data.totalPages) return null; return ; })}
)} @@ -231,24 +162,24 @@ export default function Transactions() { )}
- {/* 关联交易 */} {deals && deals.length > 0 && (
-

🔗 关联交易(Deal)

共 {deals.length} 条对账关联
+

🔗 关联交易(Deal)

共 {deals.length} 条
- + {deals.map(d => ( - - - - + + + + @@ -258,27 +189,6 @@ export default function Transactions() {
日期金额对方关联明细
日期金额分类对方关联明细
{d.deal_date}= 0 ? "stat-value income" : "stat-value expense"}>{formatAmount(d.amount)}{d.counterparty || "--"} - {d.transactions?.map((t,i) => ( + {d.deal_date}= 0 ? 'stat-value income' : 'stat-value expense'}>{formatAmount(d.amount)}{d.category || 未分类}{d.counterparty || '--'} + {d.transactions?.map((t, i) => ( - {SOURCE_TYPE_MAP[t.source_type]} - {" "}{t.description?.substring(0,20)}↔ + {SOURCE_TYPE_MAP[t.source_type]} + {' '}{t.description?.substring(0, 20)} ))}
)} -{editingTx && ( -
setEditingTx(null)}> -
e.stopPropagation()}> -

编辑交易

-
- {editingTx.transaction_code} | {fmtDatetime(editingTx.transaction_time)} -
-
-
setEditingTx({ ...editingTx, ai_description: e.target.value })} />
-
-
- - -
-
-
- )}
); }