|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useMemo, useState } from 'react' |
| 4 | +import { formatRupees } from '@/lib/format' |
| 5 | +import { TableExportMenu } from '@/components/table-export' |
| 6 | +import { PrDataTable, type PrColumn } from '@/components/ui/pr/data-table' |
| 7 | +import type { Cell } from '@/lib/table-export' |
| 8 | + |
| 9 | +export type CurrentMonthRow = { |
| 10 | + id: string |
| 11 | + transaction_id: string |
| 12 | + amount: number | string |
| 13 | + transaction_date: string |
| 14 | + member_name?: string | null |
| 15 | + bank_transaction_id?: string | null |
| 16 | +} |
| 17 | + |
| 18 | +/** Flattened fields baked onto each row so the DataTable can sort / filter / |
| 19 | + * globally search on flat string/number values. */ |
| 20 | +type CurrentMonthRowAug = CurrentMonthRow & { |
| 21 | + _date_ts: number |
| 22 | + _amount: number |
| 23 | + _member: string |
| 24 | + _search_blob: string |
| 25 | +} |
| 26 | + |
| 27 | +function formatDate(iso: string): string { |
| 28 | + const d = new Date(iso) |
| 29 | + const dd = String(d.getUTCDate()).padStart(2, '0') |
| 30 | + const mm = String(d.getUTCMonth() + 1).padStart(2, '0') |
| 31 | + const yyyy = d.getUTCFullYear() |
| 32 | + return `${dd}-${mm}-${yyyy}` |
| 33 | +} |
| 34 | + |
| 35 | +export function CurrentMonthContributionsTable({ |
| 36 | + rows, |
| 37 | +}: { |
| 38 | + rows: CurrentMonthRow[] |
| 39 | +}) { |
| 40 | + const augmented = useMemo<CurrentMonthRowAug[]>( |
| 41 | + () => |
| 42 | + rows.map((r) => { |
| 43 | + const member = r.member_name ?? '' |
| 44 | + return { |
| 45 | + ...r, |
| 46 | + _date_ts: new Date(r.transaction_date).getTime(), |
| 47 | + _amount: Number(r.amount) || 0, |
| 48 | + _member: member, |
| 49 | + _search_blob: [ |
| 50 | + member, |
| 51 | + r.transaction_id, |
| 52 | + r.bank_transaction_id ?? '', |
| 53 | + formatDate(r.transaction_date), |
| 54 | + String(r.amount), |
| 55 | + ].join(' '), |
| 56 | + } |
| 57 | + }), |
| 58 | + [rows], |
| 59 | + ) |
| 60 | + |
| 61 | + // The DataTable reports its current filtered+sorted rows here so the export + |
| 62 | + // footer summary reflect what's on screen. `null` until the first |
| 63 | + // onValueChange fires → fall back to the full set. |
| 64 | + const [processed, setProcessed] = useState<CurrentMonthRowAug[] | null>(null) |
| 65 | + const [searchQuery, setSearchQuery] = useState('') |
| 66 | + const visible = processed ?? augmented |
| 67 | + |
| 68 | + const total = visible.reduce((s, r) => s + r._amount, 0) |
| 69 | + |
| 70 | + const exportColumns = ['#', 'Person name', 'Contribution (₹)', 'Date', 'Transaction ID'] |
| 71 | + const exportRows: Cell[][] = visible.map((t, i) => [ |
| 72 | + i + 1, |
| 73 | + t._member, |
| 74 | + t._amount, |
| 75 | + formatDate(t.transaction_date), |
| 76 | + t.transaction_id, |
| 77 | + ]) |
| 78 | + const exportFooter: Cell[] = ['', '', total, '', 'Total'] |
| 79 | + |
| 80 | + const columns: PrColumn<CurrentMonthRowAug>[] = [ |
| 81 | + { |
| 82 | + field: 'id', |
| 83 | + header: '#', |
| 84 | + align: 'right', |
| 85 | + bodyClassName: 'w-10 text-right tabular-nums text-gray-500', |
| 86 | + body: (_t, { rowIndex }) => rowIndex + 1, |
| 87 | + }, |
| 88 | + { |
| 89 | + field: '_member', |
| 90 | + header: 'Person name', |
| 91 | + sortable: true, |
| 92 | + dataType: 'text', |
| 93 | + bodyClassName: 'font-medium text-gray-900', |
| 94 | + body: (t) => t.member_name ?? <span className="text-gray-400">—</span>, |
| 95 | + }, |
| 96 | + { |
| 97 | + field: '_amount', |
| 98 | + header: 'Contribution', |
| 99 | + sortable: true, |
| 100 | + align: 'right', |
| 101 | + dataType: 'numeric', |
| 102 | + bodyClassName: 'whitespace-nowrap text-right font-semibold tabular-nums text-gray-900', |
| 103 | + body: (t) => formatRupees(t.amount), |
| 104 | + }, |
| 105 | + { |
| 106 | + field: '_date_ts', |
| 107 | + header: 'Date', |
| 108 | + sortable: true, |
| 109 | + dataType: 'numeric', |
| 110 | + bodyClassName: 'whitespace-nowrap text-gray-600', |
| 111 | + body: (t) => formatDate(t.transaction_date), |
| 112 | + }, |
| 113 | + { |
| 114 | + field: 'transaction_id', |
| 115 | + header: 'Transaction ID', |
| 116 | + sortable: true, |
| 117 | + bodyClassName: 'whitespace-nowrap font-mono text-xs text-gray-500', |
| 118 | + body: (t) => ( |
| 119 | + <span> |
| 120 | + <span>{t.transaction_id}</span> |
| 121 | + {t.bank_transaction_id && ( |
| 122 | + <span className="ml-2 text-[11px] text-gray-400" title="Bank reference"> |
| 123 | + {t.bank_transaction_id} |
| 124 | + </span> |
| 125 | + )} |
| 126 | + </span> |
| 127 | + ), |
| 128 | + }, |
| 129 | + ] |
| 130 | + |
| 131 | + const exportMenu = ( |
| 132 | + <TableExportMenu |
| 133 | + filename="current-month-contributions" |
| 134 | + title="Current month contributions" |
| 135 | + columns={exportColumns} |
| 136 | + rows={exportRows} |
| 137 | + footer={exportFooter} |
| 138 | + /> |
| 139 | + ) |
| 140 | + |
| 141 | + return ( |
| 142 | + <div className="overflow-clip rounded-2xl border border-gray-200 bg-white"> |
| 143 | + <PrDataTable<CurrentMonthRowAug> |
| 144 | + value={augmented} |
| 145 | + columns={columns} |
| 146 | + dataKey="id" |
| 147 | + emptyMessage={ |
| 148 | + searchQuery |
| 149 | + ? `No matches for "${searchQuery}"` |
| 150 | + : 'No contributions recorded this month yet' |
| 151 | + } |
| 152 | + globalFilterFields={rows.length > 0 ? ['_search_blob'] : undefined} |
| 153 | + globalSearchPlaceholder="Search rows…" |
| 154 | + header={rows.length > 0 ? exportMenu : undefined} |
| 155 | + onValueChange={setProcessed} |
| 156 | + onGlobalFilterChange={setSearchQuery} |
| 157 | + /> |
| 158 | + |
| 159 | + {visible.length > 0 && ( |
| 160 | + <div className="flex items-center justify-between border-t border-gray-200 bg-gray-50/30 px-5 py-3 text-xs text-gray-500"> |
| 161 | + <span> |
| 162 | + Showing <span className="font-medium text-gray-900">{visible.length}</span>{' '} |
| 163 | + {visible.length === 1 ? 'contribution' : 'contributions'} |
| 164 | + {augmented.length !== visible.length && ( |
| 165 | + <span className="text-gray-400"> · filtered from {augmented.length}</span> |
| 166 | + )} |
| 167 | + </span> |
| 168 | + <span className="font-medium text-gray-400"> |
| 169 | + Total{' '} |
| 170 | + <span className="ml-1 tabular-nums text-gray-900">{formatRupees(total)}</span> |
| 171 | + </span> |
| 172 | + </div> |
| 173 | + )} |
| 174 | + </div> |
| 175 | + ) |
| 176 | +} |
0 commit comments