diff --git a/app/dashboard/transactions/page.tsx b/app/dashboard/transactions/page.tsx index 1de16fe..441486e 100644 --- a/app/dashboard/transactions/page.tsx +++ b/app/dashboard/transactions/page.tsx @@ -1,6 +1,7 @@ import { getAssets } from '@/actions/asset'; import { getTransactions } from '@/actions/transaction'; import { AddTransactionDialog } from '@/components/transactions/add-transaction-dialog'; +import { formatAmount, formatQuantity } from '@/lib/formatters'; import { Table, TableBody, @@ -25,7 +26,7 @@ export default async function TransactionsPage() { FEE: '手续费', }; - const assetMap = new Map(assets.map((a) => [a.id, a.symbol])); + const assetMap = new Map(assets.map((a) => [a.id, { symbol: a.symbol, type: a.type }])); return (
@@ -62,14 +63,16 @@ export default async function TransactionsPage() { ) : ( transactions.map((tx) => { - const symbol = assetMap.get(tx.assetId) || tx.assetId; + const assetInfo = assetMap.get(tx.assetId); + const symbol = assetInfo?.symbol || tx.assetId; + const assetType = assetInfo?.type || 'CASH'; return ( {symbol} {typeLabels[tx.txType] || tx.txType} - {tx.quantity} - {tx.price} - {tx.fee} + {formatQuantity(tx.quantity, assetType)} + {formatAmount(tx.price)} + {formatAmount(tx.fee)} {tx.txCurrency} {tx.executedAt diff --git a/package-lock.json b/package-lock.json index 5973516..dd35ad1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@radix-ui/react-select": "^2.2.6", "@radix-ui/react-slot": "^1.2.4", "bcryptjs": "^3.0.3", + "big.js": "^7.0.1", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "dotenv": "^17.4.2", @@ -3913,6 +3914,19 @@ "bcrypt": "bin/bcrypt" } }, + "node_modules/big.js": { + "version": "7.0.1", + "resolved": "https://registry.npmmirror.com/big.js/-/big.js-7.0.1.tgz", + "integrity": "sha512-iFgV784tD8kq4ccF1xtNMZnXeZzVuXWWM+ERFzKQjv+A5G9HC8CY3DuV45vgzFFcW+u2tIvmF95+AzWgs6BjCg==", + "license": "MIT", + "engines": { + "node": "*" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/bigjs" + } + }, "node_modules/binary-extensions": { "version": "2.3.0", "resolved": "https://registry.npmmirror.com/binary-extensions/-/binary-extensions-2.3.0.tgz", diff --git a/package.json b/package.json index b4695dc..b9fea1f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@radix-ui/react-select": "^2.2.6", "@radix-ui/react-slot": "^1.2.4", "bcryptjs": "^3.0.3", + "big.js": "^7.0.1", "class-variance-authority": "^0.7.1", "clsx": "^2.1.1", "dotenv": "^17.4.2", diff --git a/src/lib/formatters.ts b/src/lib/formatters.ts new file mode 100644 index 0000000..e8b5965 --- /dev/null +++ b/src/lib/formatters.ts @@ -0,0 +1,16 @@ +import Big from 'big.js'; + +export function formatAmount(value: string): string { + return new Big(value).toFixed(2); +} + +export function formatQuantity(value: string, assetType: string): string { + switch (assetType) { + case 'STOCK': + return new Big(value).round(0).toString(); + case 'CRYPTO': + return new Big(value).round(8).toString(); + default: + return new Big(value).toFixed(2); + } +}