import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { getPortfolioSummary } from '@/actions/portfolio'; import { formatQuantity, formatAmount } from '@/lib/formatters'; import AllocationChart from '@/components/dashboard/allocation-chart'; import { SyncButton } from '@/components/assets/sync-button'; import Big from 'big.js'; function getCurrencySymbol(currency: string): string { if (currency === 'USD') return '$'; if (currency === 'HKD') return 'HK$'; return '¥'; } function formatNative(value: string, baseCurrency: string): string { const symbol = getCurrencySymbol(baseCurrency); const formatted = new Big(value).toFixed(2); return `${symbol}${formatted}`; } function formatPnl(value: string, percent: string, baseCurrency: string): { text: string; className: string } { const isPositive = new Big(value).gte(0); const symbol = getCurrencySymbol(baseCurrency); const absValue = new Big(value).abs().toFixed(2); const absPercent = new Big(percent).abs().toFixed(2); const sign = isPositive ? '+' : ''; const text = `${sign}${symbol}${absValue} (${sign}${absPercent}%)`; const className = isPositive ? 'text-red-500' : 'text-green-500'; return { text, className }; } export default async function DashboardPage() { const { positions, totalCnyValue, chartData, totalPnlCny, unrealizedPnlCny, marketAllocation } = await getPortfolioSummary(); const formattedTotal = formatAmount(totalCnyValue); const formattedTotalPnl = formatAmount(totalPnlCny); const formattedUnrealized = formatAmount(unrealizedPnlCny); const totalPnlIsPositive = new Big(totalPnlCny).gte(0); const unrealizedIsPositive = new Big(unrealizedPnlCny).gte(0); function DataRow({ label, value, valueClass = 'font-medium' }: { label: string; value: string; valueClass?: string }) { return (
{label} {value}
); } return (

欢迎来到 Omniledger

您的跨界记账中枢。

总资产概览
¥ {formattedTotal} 总资产 (CNY)
持仓盈亏: {unrealizedIsPositive ? '+' : ''}{formattedUnrealized}
总盈亏: {totalPnlIsPositive ? '+' : ''}{formattedTotalPnl}
{positions.length === 0 ? (

暂无持仓,请先添加资产和交易记录。

) : ( positions.map((pos) => { const symbol = getCurrencySymbol(pos.baseCurrency); const latestPriceFormatted = `${symbol}${new Big(pos.latestPrice || '0').toFixed(2)}`; const marketValueFormatted = formatNative(pos.marketValueNative, pos.baseCurrency); const quantityFormatted = formatQuantity(pos.quantity, pos.type); const dilutedCostStr = new Big(pos.dilutedCostNative).toFixed(2); const avgCostStr = new Big(pos.avgCostNative).toFixed(2); const costCombined = `${dilutedCostStr} / ${avgCostStr}`; const floatingPnl = formatPnl(pos.floatingPnlNative, pos.floatingPnlPercent, pos.baseCurrency); const cumulativePnl = formatPnl(pos.cumulativePnlNative, pos.cumulativePnlPercent, pos.baseCurrency); return (
{pos.name || pos.symbol} {pos.symbol}
{pos.baseCurrency}
); }) )}
资产分布
); }