Update frontend/src/components/TransactionsTable.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Elisiário Couto
2025-12-08 21:22:15 +00:00
parent 504f78aa85
commit edbc1cb39e

View File

@@ -356,28 +356,31 @@ export default function TransactionsTable() {
); );
} }
// Calculate stats from current page transactions // Calculate stats from current page transactions, memoized for performance
const totalIncome = transactions const { totalIncome, totalExpenses, displayCurrency, stats } = useMemo(() => {
.filter((t: Transaction) => t.transaction_value > 0) const totalIncome = transactions
.reduce((sum: number, t: Transaction) => sum + t.transaction_value, 0); .filter((t: Transaction) => t.transaction_value > 0)
.reduce((sum: number, t: Transaction) => sum + t.transaction_value, 0);
const totalExpenses = Math.abs( const totalExpenses = Math.abs(
transactions transactions
.filter((t: Transaction) => t.transaction_value < 0) .filter((t: Transaction) => t.transaction_value < 0)
.reduce((sum: number, t: Transaction) => sum + t.transaction_value, 0) .reduce((sum: number, t: Transaction) => sum + t.transaction_value, 0)
); );
// Get currency from first transaction, fallback to EUR // Get currency from first transaction, fallback to EUR
const displayCurrency = transactions.length > 0 ? transactions[0].transaction_currency : "EUR"; const displayCurrency = transactions.length > 0 ? transactions[0].transaction_currency : "EUR";
const stats = { const stats = {
totalCount: pagination?.total || 0, totalCount: pagination?.total || 0,
pageCount: transactions.length, pageCount: transactions.length,
totalIncome, totalIncome,
totalExpenses, totalExpenses,
netChange: totalIncome - totalExpenses, netChange: totalIncome - totalExpenses,
}; };
return { totalIncome, totalExpenses, displayCurrency, stats };
}, [transactions, pagination]);
return ( return (
<div className="space-y-6 max-w-full"> <div className="space-y-6 max-w-full">
{/* New FilterBar */} {/* New FilterBar */}