|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { Button } from '@/components/ui/button'; |
| 5 | +import { LedgerList } from '@/components/ledger/ledger-list'; |
| 6 | +import { LedgerFilters } from '@/components/ledger/ledger-filters'; |
| 7 | +import { Navbar } from '@/components/layout/navbar'; |
| 8 | +import { trpc } from '@/trpc/client'; |
| 9 | + |
| 10 | +const DEMO_USER_ID = 'demo-user-id'; |
| 11 | + |
| 12 | +type Currency = 'USD' | 'EUR' | 'HKD' | 'GBP' | 'JPY'; |
| 13 | +type PaymentMethod = 'card' | 'bank_transfer' | 'crypto_usdc' | 'crypto_usdt'; |
| 14 | + |
| 15 | +export default function LedgerPage() { |
| 16 | + const [startDate, setStartDate] = useState(''); |
| 17 | + const [endDate, setEndDate] = useState(''); |
| 18 | + const [currency, setCurrency] = useState(''); |
| 19 | + const [paymentMethod, setPaymentMethod] = useState(''); |
| 20 | + const [isExporting, setIsExporting] = useState(false); |
| 21 | + |
| 22 | + const ledgerQuery = trpc.ledger.list.useQuery({ |
| 23 | + userId: DEMO_USER_ID, |
| 24 | + startDate: startDate ? new Date(startDate) : undefined, |
| 25 | + endDate: endDate ? new Date(endDate) : undefined, |
| 26 | + currency: currency ? (currency as Currency) : undefined, |
| 27 | + paymentMethod: paymentMethod ? (paymentMethod as PaymentMethod) : undefined, |
| 28 | + }); |
| 29 | + |
| 30 | + const exportQuery = trpc.ledger.exportCSV.useQuery( |
| 31 | + { |
| 32 | + userId: DEMO_USER_ID, |
| 33 | + startDate: startDate ? new Date(startDate) : undefined, |
| 34 | + endDate: endDate ? new Date(endDate) : undefined, |
| 35 | + currency: currency ? (currency as Currency) : undefined, |
| 36 | + paymentMethod: paymentMethod ? (paymentMethod as PaymentMethod) : undefined, |
| 37 | + }, |
| 38 | + { enabled: false } |
| 39 | + ); |
| 40 | + |
| 41 | + const handleExport = async () => { |
| 42 | + setIsExporting(true); |
| 43 | + try { |
| 44 | + const result = await exportQuery.refetch(); |
| 45 | + if (result.data?.csv) { |
| 46 | + // Create and download CSV file |
| 47 | + const blob = new Blob([result.data.csv], { type: 'text/csv;charset=utf-8;' }); |
| 48 | + const link = document.createElement('a'); |
| 49 | + const url = URL.createObjectURL(blob); |
| 50 | + link.setAttribute('href', url); |
| 51 | + link.setAttribute('download', `ledger-export-${new Date().toISOString().split('T')[0]}.csv`); |
| 52 | + link.style.visibility = 'hidden'; |
| 53 | + document.body.appendChild(link); |
| 54 | + link.click(); |
| 55 | + document.body.removeChild(link); |
| 56 | + } |
| 57 | + } catch (error) { |
| 58 | + console.error('Export failed:', error); |
| 59 | + alert('Failed to export ledger data'); |
| 60 | + } finally { |
| 61 | + setIsExporting(false); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // Calculate totals |
| 66 | + const entries = ledgerQuery.data?.entries || []; |
| 67 | + const totalAmount = entries.reduce( |
| 68 | + (sum, entry) => sum + parseFloat(String(entry.amountInDefaultCurrency)), |
| 69 | + 0 |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div className="min-h-screen bg-gray-50"> |
| 74 | + <Navbar /> |
| 75 | + <div className="container mx-auto px-4 py-8"> |
| 76 | + <div className="flex justify-between items-center mb-6"> |
| 77 | + <h1 className="text-2xl font-bold text-gray-900">收入账本</h1> |
| 78 | + <Button onClick={handleExport} disabled={isExporting}> |
| 79 | + {isExporting ? 'Exporting...' : 'Export CSV'} |
| 80 | + </Button> |
| 81 | + </div> |
| 82 | + |
| 83 | + {/* Summary Card */} |
| 84 | + <div className="bg-white rounded-lg shadow p-6 mb-6"> |
| 85 | + <div className="grid grid-cols-1 md:grid-cols-3 gap-4"> |
| 86 | + <div> |
| 87 | + <p className="text-sm text-gray-500">Total Entries</p> |
| 88 | + <p className="text-2xl font-bold text-gray-900"> |
| 89 | + {ledgerQuery.data?.total || 0} |
| 90 | + </p> |
| 91 | + </div> |
| 92 | + <div> |
| 93 | + <p className="text-sm text-gray-500">Total Income (USD)</p> |
| 94 | + <p className="text-2xl font-bold text-green-600"> |
| 95 | + ${totalAmount.toFixed(2)} |
| 96 | + </p> |
| 97 | + </div> |
| 98 | + <div> |
| 99 | + <p className="text-sm text-gray-500">Current Page</p> |
| 100 | + <p className="text-2xl font-bold text-gray-900"> |
| 101 | + {ledgerQuery.data?.page || 1} / {ledgerQuery.data?.totalPages || 1} |
| 102 | + </p> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + |
| 107 | + {/* Filters */} |
| 108 | + <LedgerFilters |
| 109 | + startDate={startDate} |
| 110 | + endDate={endDate} |
| 111 | + currency={currency} |
| 112 | + paymentMethod={paymentMethod} |
| 113 | + onStartDateChange={setStartDate} |
| 114 | + onEndDateChange={setEndDate} |
| 115 | + onCurrencyChange={setCurrency} |
| 116 | + onPaymentMethodChange={setPaymentMethod} |
| 117 | + /> |
| 118 | + |
| 119 | + {/* Ledger List */} |
| 120 | + <div className="bg-white rounded-lg shadow"> |
| 121 | + <LedgerList |
| 122 | + entries={entries.map((entry) => ({ |
| 123 | + ...entry, |
| 124 | + amount: String(entry.amount), |
| 125 | + amountInDefaultCurrency: String(entry.amountInDefaultCurrency), |
| 126 | + }))} |
| 127 | + isLoading={ledgerQuery.isLoading} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + |
| 131 | + {/* Pagination */} |
| 132 | + {ledgerQuery.data && ledgerQuery.data.totalPages > 1 && ( |
| 133 | + <div className="mt-4 flex justify-center gap-2"> |
| 134 | + <Button |
| 135 | + variant="secondary" |
| 136 | + disabled={ledgerQuery.data.page <= 1} |
| 137 | + onClick={() => { |
| 138 | + // TODO: Implement pagination |
| 139 | + }} |
| 140 | + > |
| 141 | + Previous |
| 142 | + </Button> |
| 143 | + <span className="px-4 py-2 text-gray-600"> |
| 144 | + Page {ledgerQuery.data.page} of {ledgerQuery.data.totalPages} |
| 145 | + </span> |
| 146 | + <Button |
| 147 | + variant="secondary" |
| 148 | + disabled={ledgerQuery.data.page >= ledgerQuery.data.totalPages} |
| 149 | + onClick={() => { |
| 150 | + // TODO: Implement pagination |
| 151 | + }} |
| 152 | + > |
| 153 | + Next |
| 154 | + </Button> |
| 155 | + </div> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + ); |
| 160 | +} |
0 commit comments