Improve appearance of transactions table

This commit is contained in:
2025-05-21 13:47:10 +02:00
parent 9d2ac48e1a
commit 490d6aa650
3 changed files with 49 additions and 23 deletions

View File

@@ -1,7 +0,0 @@
.transactionTable tbody tr {
cursor: pointer;
}
.disabled td {
color: var(--bulma-text-light);
}

View File

@@ -0,0 +1,19 @@
.transactionTable {
tbody tr {
cursor: pointer;
}
td {
.date {
white-space: nowrap;
}
.amount {
font-family: monospace;
}
}
.disabled td {
color: var(--bulma-text-light);
}
}

View File

@@ -1,6 +1,6 @@
import { useCallback } from "react"; import { useCallback, useMemo } from "react";
import type { Transaction } from "../../types/api"; import type { Transaction } from "../../types/api";
import styles from "./TransactionTable.module.css"; import styles from "./TransactionsTable.module.css";
export type TransactionsTableProps = { export type TransactionsTableProps = {
transactions: Transaction[]; transactions: Transaction[];
@@ -10,6 +10,22 @@ export type TransactionsTableProps = {
readonly?: boolean; readonly?: boolean;
} }
const transactionEmoji = (transaction: Transaction): string => {
if (transaction.kind === 'transfer') {
return '➡️'
};
return transaction.amount > 0 ? '⬇️' : '⬆️';
};
const transactionType = (transaction: Transaction): string => {
if (transaction.kind === 'transfer') {
return 'Transfer'
};
return transaction.amount > 0 ? 'Inflow' : 'Outflow';
};
export default function TransactionsTable({ transactions, deselected, deselect, select, readonly }: TransactionsTableProps) { export default function TransactionsTable({ transactions, deselected, deselect, select, readonly }: TransactionsTableProps) {
const changeSelection = useCallback((index: number) => { const changeSelection = useCallback((index: number) => {
@@ -26,16 +42,16 @@ export default function TransactionsTable({ transactions, deselected, deselect,
} }
}, [deselected, deselect, select]); }, [deselected, deselect, select]);
const reversedTransactions = useMemo(() => transactions.slice().reverse(), [transactions]);
const renderRow = useCallback((transaction: Transaction, index: number) => ( const renderRow = useCallback((transaction: Transaction, index: number) => (
<tr key={index} onClick={() => changeSelection(index)} className={`${!readonly && deselected.includes(index) ? styles.disabled : ""}`}> <tr key={index} onClick={() => changeSelection(index)} className={`${!readonly && deselected.includes(index) ? styles.disabled : ""}`}>
<td>{transaction.kind}</td> <td><span title={transactionType(transaction)}>{transactionEmoji(transaction)}</span></td>
<td>{transaction.date}</td> <td className={styles.date}>{transaction.date}</td>
<td className={styles.amount}>{transaction.amount}</td>
<td>{transaction.id ? <abbr title={`ID: ${transaction.id}`}>{transaction.title}</abbr> : transaction.title}</td>
<td>{transaction.from}</td> <td>{transaction.from}</td>
<td>{transaction.to}</td> <td>{transaction.to}</td>
<td>{transaction.amount}</td>
<td>{transaction.title}</td>
<td>{transaction.id}</td>
</tr> </tr>
), [changeSelection]); ), [changeSelection]);
@@ -44,18 +60,16 @@ export default function TransactionsTable({ transactions, deselected, deselect,
<table className={`table is-hoverable ${!readonly ? styles.transactionTable : ""}`}> <table className={`table is-hoverable ${!readonly ? styles.transactionTable : ""}`}>
<thead> <thead>
<tr> <tr>
<th></th>
<th>Kind</th>
<th>Date</th> <th>Date</th>
<th>From</th>
<th>To</th>
<th>Amount</th> <th>Amount</th>
<th>Title</th> <th>Title</th>
<th>ID</th> <th>From</th>
<th>To</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{transactions.map(renderRow)} {reversedTransactions.map(renderRow)}
</tbody> </tbody>
</table> </table>
</div> </div>