import { type ColumnDef } from '@tanstack/react-table'; import { ArrowLeftRight, ArrowRightFromLine, Banknote, Pencil, Trash2, TrendingDown, TrendingUp } from 'lucide-react'; import { type Transaction } from '@/lib/api'; import { formatAmount } from '@/lib/format'; import { cn } from '@/lib/utils'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { DataTableColumnHeader } from '@/components/ui/data-table-column-header'; export interface RowSelection { selected: Set; allSelected: boolean; onToggle: (id: number) => void; onToggleAll: () => void; } interface TransactionColumnOptions { showCategory: boolean; showSourceIcon?: boolean; onEdit: (tx: Transaction) => void; onDelete: (txId: number) => void; onToggleDeferred?: (tx: Transaction) => void; selection?: RowSelection; } export function getTransactionColumns({ showCategory, showSourceIcon, onEdit, onDelete, onToggleDeferred, selection, }: TransactionColumnOptions): ColumnDef[] { const columns: ColumnDef[] = []; if (selection) { columns.push({ id: 'select', size: 36, enableSorting: false, header: () => ( ), cell: ({ row }) => ( selection.onToggle(row.original.id)} /> ), }); } columns.push( { accessorKey: 'date', header: ({ column }) => , cell: ({ row }) => ( {new Date(row.original.date).toLocaleDateString('es-CR', { month: 'short', day: 'numeric', year: 'numeric', })} ), }, { accessorKey: 'merchant', header: ({ column }) => , cell: ({ row }) => { const tx = row.original; return (
{tx.transaction_type === 'COMPRA' ? ( ) : ( )}
{tx.merchant} {showSourceIcon && tx.source === 'CASH' && ( )} {showSourceIcon && tx.source === 'TRANSFER' && ( )} {tx.deferred_to_next_cycle && ( Diferida )}
); }, }, ); if (showCategory) { columns.push({ accessorFn: (row) => row.category?.name ?? '', id: 'category', header: ({ column }) => , cell: ({ row }) => { const category = row.original.category; return category ? ( {category.name} ) : ( ); }, }); } columns.push( { accessorKey: 'amount', meta: { className: 'text-right' }, header: ({ column }) => ( ), cell: ({ row }) => { const tx = row.original; return ( {tx.transaction_type === 'COMPRA' ? '-' : '+'} {formatAmount(tx.amount, tx.currency)} ); }, }, { id: 'actions', meta: { className: 'text-right' }, size: 80, enableSorting: false, cell: ({ row }) => { const tx = row.original; return (
{onToggleDeferred && ( )}
); }, }, ); return columns; }