feat(ui): Dashboard 基礎表格化重構,實裝行展開狀態管理

This commit is contained in:
kennethcheng 2026-04-28 23:58:25 +08:00
parent cdea2ce608
commit 1c6c36b147
3 changed files with 262 additions and 82 deletions

View File

@ -79,6 +79,15 @@
- 新增累計盈虧指標:`cumulativePnlNative = floatingPnlNative + realizedPnlNative + accumulatedDividendsNative`、`cumulativePnlPercent = cumulativePnlNative / totalBuyCostNative * 100`。 - 新增累計盈虧指標:`cumulativePnlNative = floatingPnlNative + realizedPnlNative + accumulatedDividendsNative`、`cumulativePnlPercent = cumulativePnlNative / totalBuyCostNative * 100`。
- SELL 交易的已實現盈虧計算從 CNY 基準改為 Native 基準CNY 計算保留用於前端兼容展示。 - SELL 交易的已實現盈虧計算從 CNY 基準改為 Native 基準CNY 計算保留用於前端兼容展示。
## Dashboard 表格化基礎重構 (Task 41)
- Dashboard 完成表格化基礎重構,徹底移除原有的卡片網格佈局,改用 shadcn/ui Table 組件構建高密度專業券商風格主表。
- 實裝了基於 ID 的行展開狀態管理邏輯:`useState<Record<string, boolean>>` 控制每行的展開/收起狀態,點擊行或展開按鈕觸發 `toggleExpand`
- 表頭嚴格對齊專業標準:名稱/代碼 | 現價 | 市值 | 持倉 | 攤薄/成本 | 浮動盈虧 | 累計盈虧 | 操作。
- 操作列包含展開/收起指示器與「添加」按鈕,點擊打開 `AddTransactionDialog` 並預選對應資產。
- 展開行內嵌交易記錄子表格,展示每筆交易的類型、數量、價格、手續費、幣種與日期。
- 重構 `AddTransactionDialog` 組件支持外部控制開關(`open`/`onOpenChange`/`defaultAssetId` props同時保持向後兼容內部狀態管理模式。
- Dashboard 頁面轉換為客戶端組件,使用 `useEffect` 在客戶端加載組合數據。
## 全面重構資產展示 UI (Task 39) ## 全面重構資產展示 UI (Task 39)
- UI 全面升級,復刻專業券商級數據排版,合併攤薄/成本,引入原生幣種盈虧百分比展示。 - UI 全面升級,復刻專業券商級數據排版,合併攤薄/成本,引入原生幣種盈虧百分比展示。
- 徹底清理所有帶 (CNY) 和 (USD) 混雜的舊布局,所有 Native 金額根據 `baseCurrency` 渲染正確貨幣符號USD→$、CNY/HKD→HK$、JPY→¥ - 徹底清理所有帶 (CNY) 和 (USD) 混雜的舊布局,所有 Native 金額根據 `baseCurrency` 渲染正確貨幣符號USD→$、CNY/HKD→HK$、JPY→¥

View File

@ -1,14 +1,31 @@
'use client';
import { useState, useEffect } from 'react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Button } from '@/components/ui/button';
import { getPortfolioSummary } from '@/actions/portfolio'; import { getPortfolioSummary } from '@/actions/portfolio';
import { getAssets } from '@/actions/asset';
import { formatQuantity, formatAmount } from '@/lib/formatters'; import { formatQuantity, formatAmount } from '@/lib/formatters';
import AllocationChart from '@/components/dashboard/allocation-chart'; import AllocationChart from '@/components/dashboard/allocation-chart';
import { SyncButton } from '@/components/assets/sync-button'; import { SyncButton } from '@/components/assets/sync-button';
import { AddTransactionDialog } from '@/components/transactions/add-transaction-dialog';
import { ChevronDown, ChevronUp, Plus, Edit3 } from 'lucide-react';
import Big from 'big.js'; import Big from 'big.js';
function getCurrencySymbol(currency: string): string { function getCurrencySymbol(currency: string): string {
if (currency === 'USD') return '$'; if (currency === 'USD') return '$';
if (currency === 'HKD') return 'HK$'; if (currency === 'HKD') return 'HK$';
return '¥'; if (currency === 'CNY') return '¥';
if (currency === 'JPY') return '¥';
return currency + ' ';
} }
function formatNative(value: string, baseCurrency: string): string { function formatNative(value: string, baseCurrency: string): string {
@ -28,8 +45,48 @@ function formatPnl(value: string, percent: string, baseCurrency: string): { text
return { text, className }; return { text, className };
} }
export default async function DashboardPage() { interface Asset {
const { positions, totalCnyValue, chartData, totalPnlCny, unrealizedPnlCny, marketAllocation } = await getPortfolioSummary(); id: string;
symbol: string;
name: string | null;
type: string;
baseCurrency: string;
exchange: string | null;
}
export default function DashboardPage() {
const [positions, setPositions] = useState<any[]>([]);
const [totalCnyValue, setTotalCnyValue] = useState('0');
const [totalPnlCny, setTotalPnlCny] = useState('0');
const [unrealizedPnlCny, setUnrealizedPnlCny] = useState('0');
const [marketAllocation, setMarketAllocation] = useState<any[]>([]);
const [assets, setAssets] = useState<Asset[]>([]);
const [expandedIds, setExpandedIds] = useState<Record<string, boolean>>({});
const [dialogOpen, setDialogOpen] = useState(false);
const [selectedAssetId, setSelectedAssetId] = useState<string>('');
useEffect(() => {
async function loadData() {
const summary = await getPortfolioSummary();
const allAssets = await getAssets();
setPositions(summary.positions);
setTotalCnyValue(summary.totalCnyValue);
setTotalPnlCny(summary.totalPnlCny);
setUnrealizedPnlCny(summary.unrealizedPnlCny);
setMarketAllocation(summary.marketAllocation);
setAssets(allAssets as Asset[]);
}
loadData();
}, []);
const toggleExpand = (id: string) => {
setExpandedIds(prev => ({ ...prev, [id]: !prev[id] }));
};
const handleOpenDialog = (assetId: string) => {
setSelectedAssetId(assetId);
setDialogOpen(true);
};
const formattedTotal = formatAmount(totalCnyValue); const formattedTotal = formatAmount(totalCnyValue);
const formattedTotalPnl = formatAmount(totalPnlCny); const formattedTotalPnl = formatAmount(totalPnlCny);
@ -37,15 +94,6 @@ export default async function DashboardPage() {
const totalPnlIsPositive = new Big(totalPnlCny).gte(0); const totalPnlIsPositive = new Big(totalPnlCny).gte(0);
const unrealizedIsPositive = new Big(unrealizedPnlCny).gte(0); const unrealizedIsPositive = new Big(unrealizedPnlCny).gte(0);
function DataRow({ label, value, valueClass = 'font-medium' }: { label: string; value: string; valueClass?: string }) {
return (
<div className="flex justify-between items-center">
<span className="text-sm text-muted-foreground">{label}</span>
<span className={valueClass}>{value}</span>
</div>
);
}
return ( return (
<div className="space-y-6"> <div className="space-y-6">
<div> <div>
@ -81,77 +129,178 @@ export default async function DashboardPage() {
</CardContent> </CardContent>
</Card> </Card>
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> <Card>
{positions.length === 0 ? ( <CardHeader>
<Card> <CardTitle></CardTitle>
<CardContent className="pt-6"> </CardHeader>
<p className="text-muted-foreground"></p> <CardContent>
</CardContent> {positions.length === 0 ? (
</Card> <div className="text-center py-8 text-muted-foreground">
) : (
positions.map((pos) => { </div>
const symbol = getCurrencySymbol(pos.baseCurrency); ) : (
const latestPriceFormatted = `${symbol}${new Big(pos.latestPrice || '0').toFixed(2)}`; <Table>
const marketValueFormatted = formatNative(pos.marketValueNative, pos.baseCurrency); <TableHeader>
const quantityFormatted = formatQuantity(pos.quantity, pos.type); <TableRow>
<TableHead className="w-[200px]">/</TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right">/</TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right w-[120px]"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{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 dilutedCostStr = new Big(pos.dilutedCostNative).toFixed(2);
const avgCostStr = new Big(pos.avgCostNative).toFixed(2); const avgCostStr = new Big(pos.avgCostNative).toFixed(2);
const costCombined = `${dilutedCostStr} / ${avgCostStr}`;
const floatingPnl = formatPnl(pos.floatingPnlNative, pos.floatingPnlPercent, pos.baseCurrency); let costDisplay = '-';
const cumulativePnl = formatPnl(pos.cumulativePnlNative, pos.cumulativePnlPercent, pos.baseCurrency); if (!new Big(pos.dilutedCostNative).eq('0') || !new Big(pos.avgCostNative).eq('0')) {
costDisplay = `${dilutedCostStr} / ${avgCostStr}`;
}
return ( const floatingPnl = formatPnl(pos.floatingPnlNative, pos.floatingPnlPercent, pos.baseCurrency);
<Card key={pos.assetId} className="flex flex-col"> const cumulativePnl = formatPnl(pos.cumulativePnlNative, pos.cumulativePnlPercent, pos.baseCurrency);
<CardHeader className="pb-3">
<CardTitle className="flex items-center justify-between"> const isExpanded = !!expandedIds[pos.assetId];
<div className="flex flex-col">
<span className="text-base font-semibold">{pos.name || pos.symbol}</span> return (
<span className="text-sm font-normal text-muted-foreground">{pos.symbol}</span> <>
</div> <TableRow key={pos.assetId} className="cursor-pointer" onClick={() => toggleExpand(pos.assetId)}>
<span className="text-xs font-normal text-muted-foreground bg-muted px-2 py-0.5 rounded"> <TableCell>
{pos.baseCurrency} <div className="flex flex-col">
</span> <span className="font-semibold">{pos.name || pos.symbol}</span>
</CardTitle> <span className="text-xs text-muted-foreground">{pos.symbol}</span>
</CardHeader> </div>
<CardContent className="pt-2 flex-1"> </TableCell>
<div className="space-y-3"> <TableCell className="text-right">{latestPriceFormatted}</TableCell>
<DataRow label="现价" value={latestPriceFormatted} /> <TableCell className="text-right">{marketValueFormatted}</TableCell>
<DataRow label="市值" value={marketValueFormatted} /> <TableCell className="text-right">{quantityFormatted}</TableCell>
<DataRow label="持仓" value={quantityFormatted} /> <TableCell className="text-right">{costDisplay}</TableCell>
<DataRow label="摊薄 / 成本" value={costCombined} /> <TableCell className={`text-right ${floatingPnl.className}`}>{floatingPnl.text}</TableCell>
<DataRow <TableCell className={`text-right ${cumulativePnl.className}`}>{cumulativePnl.text}</TableCell>
label="浮动盈亏" <TableCell className="text-right" onClick={(e) => e.stopPropagation()}>
value={floatingPnl.text} <div className="flex items-center justify-end gap-2">
valueClass={`text-sm font-semibold ${floatingPnl.className}`} <span className="text-xs text-muted-foreground">
/> {isExpanded ? '收起' : '展开'}
<DataRow </span>
label="累计盈亏" {isExpanded ? (
value={cumulativePnl.text} <ChevronUp className="h-4 w-4" />
valueClass={`text-sm font-semibold ${cumulativePnl.className}`} ) : (
/> <ChevronDown className="h-4 w-4" />
<DataRow )}
label="持仓天数" <Button
value={`${pos.holdingDays}`} size="sm"
valueClass="text-sm text-muted-foreground" variant="ghost"
/> className="h-7 px-2 text-xs"
</div> onClick={(e) => {
</CardContent> e.stopPropagation();
</Card> handleOpenDialog(pos.assetId);
); }}
}) >
)} <Plus className="h-3 w-3 mr-1" />
</div>
</Button>
</div>
</TableCell>
</TableRow>
{isExpanded && (
<TableRow>
<TableCell colSpan={8} className="bg-muted/30 p-4">
<div className="space-y-2">
<div className="flex items-center justify-between">
<span className="text-sm font-medium text-muted-foreground"></span>
<Button
size="sm"
variant="outline"
className="h-7 px-2 text-xs"
onClick={(e) => {
e.stopPropagation();
handleOpenDialog(pos.assetId);
}}
>
<Plus className="h-3 w-3 mr-1" />
</Button>
</div>
{pos.transactions && pos.transactions.length > 0 ? (
<Table>
<TableHeader>
<TableRow>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{pos.transactions.map((tx: any, idx: number) => {
const txDate = tx.executedAt
? new Date(tx.executedAt).toLocaleString('zh-TW')
: '-';
return (
<TableRow key={idx}>
<TableCell>
<span className={`text-xs font-medium px-1.5 py-0.5 rounded ${
tx.txType === 'BUY' ? 'bg-red-100 text-red-700' :
tx.txType === 'SELL' ? 'bg-green-100 text-green-700' :
tx.txType === 'DIVIDEND' ? 'bg-blue-100 text-blue-700' :
'bg-gray-100 text-gray-700'
}`}>
{tx.txType}
</span>
</TableCell>
<TableCell className="text-right">{tx.quantity}</TableCell>
<TableCell className="text-right">{tx.price}</TableCell>
<TableCell className="text-right">{tx.fee || '0'}</TableCell>
<TableCell className="text-right">{tx.txCurrency}</TableCell>
<TableCell className="text-right text-xs text-muted-foreground">{txDate}</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
) : (
<p className="text-sm text-muted-foreground"></p>
)}
</div>
</TableCell>
</TableRow>
)}
</>
);
})}
</TableBody>
</Table>
)}
</CardContent>
</Card>
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle></CardTitle> <CardTitle></CardTitle>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
<AllocationChart data={marketAllocation} /> <AllocationChart data={marketAllocation} />
</CardContent> </CardContent>
</Card> </Card>
<AddTransactionDialog
assets={assets}
open={dialogOpen}
onOpenChange={setDialogOpen}
defaultAssetId={selectedAssetId}
/>
</div> </div>
); );
} }

View File

@ -32,6 +32,7 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from '@/components/ui/select'; } from '@/components/ui/select';
import { Plus } from 'lucide-react'; import { Plus } from 'lucide-react';
import { toast } from 'sonner'; import { toast } from 'sonner';
import { createTransaction } from '@/actions/transaction'; import { createTransaction } from '@/actions/transaction';
@ -60,6 +61,9 @@ interface Asset {
interface AddTransactionDialogProps { interface AddTransactionDialogProps {
assets: Asset[]; assets: Asset[];
open?: boolean;
onOpenChange?: (open: boolean) => void;
defaultAssetId?: string;
} }
const txTypeLabels: Record<string, string> = { const txTypeLabels: Record<string, string> = {
@ -77,15 +81,23 @@ const exchangeToCurrencyMap: Record<string, string> = {
'SZSE': 'CNY', 'SZSE': 'CNY',
}; };
export function AddTransactionDialog({ assets }: AddTransactionDialogProps) { export function AddTransactionDialog({ assets, open: openProp, onOpenChange, defaultAssetId }: AddTransactionDialogProps) {
const [open, setOpen] = useState(false); const [internalOpen, setInternalOpen] = useState(false);
const [isPending, startTransition] = useTransition(); const [isPending, startTransition] = useTransition();
const router = useRouter(); const router = useRouter();
const open = openProp !== undefined ? openProp : internalOpen;
const setOpen = (value: boolean) => {
if (openProp === undefined) {
setInternalOpen(value);
}
onOpenChange?.(value);
};
const form = useForm<AddTransactionForm>({ const form = useForm<AddTransactionForm>({
resolver: zodResolver(addTransactionSchema), resolver: zodResolver(addTransactionSchema),
defaultValues: { defaultValues: {
assetId: '', assetId: defaultAssetId || '',
txType: 'BUY' as const, txType: 'BUY' as const,
quantity: '', quantity: '',
price: '', price: '',
@ -97,6 +109,12 @@ export function AddTransactionDialog({ assets }: AddTransactionDialogProps) {
const selectedAssetId = useWatch({ control: form.control, name: 'assetId' }); const selectedAssetId = useWatch({ control: form.control, name: 'assetId' });
useEffect(() => {
if (defaultAssetId) {
form.setValue('assetId', defaultAssetId, { shouldValidate: true });
}
}, [defaultAssetId, form]);
useEffect(() => { useEffect(() => {
if (!selectedAssetId) return; if (!selectedAssetId) return;
const selectedAsset = assets.find((a) => a.id === selectedAssetId); const selectedAsset = assets.find((a) => a.id === selectedAssetId);
@ -128,13 +146,16 @@ export function AddTransactionDialog({ assets }: AddTransactionDialogProps) {
} }
return ( return (
<Dialog open={open} onOpenChange={setOpen}> <>
<DialogTrigger asChild> {!openProp && (
<Button> <DialogTrigger asChild>
<Plus className="h-4 w-4 mr-2" /> <Button>
<Plus className="h-4 w-4 mr-2" />
</Button>
</DialogTrigger> </Button>
</DialogTrigger>
)}
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-[500px]"> <DialogContent className="sm:max-w-[500px]">
<DialogHeader> <DialogHeader>
<DialogTitle></DialogTitle> <DialogTitle></DialogTitle>
@ -322,5 +343,6 @@ export function AddTransactionDialog({ assets }: AddTransactionDialogProps) {
</Form> </Form>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</>
); );
} }