'use client'; import { useState, useTransition } from 'react'; import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { zodResolver } from '@hookform/resolvers/zod'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; import { Edit, Trash2 } from 'lucide-react'; import Big from 'big.js'; import { updateAsset, deleteAsset } from '@/actions/asset'; import { AddAssetDialog } from '@/components/assets/add-asset-dialog'; import { SyncButton } from '@/components/assets/sync-button'; interface Asset { id: string; symbol: string; name: string | null; type: string; exchange: string | null; baseCurrency: string; latestPrice: string; createdAt: Date | null; } export default function AssetsPageClient({ assets }: { assets: Asset[] }) { const [isPending, startTransition] = useTransition(); const [deleteTarget, setDeleteTarget] = useState(null); const [editTarget, setEditTarget] = useState(null); const typeLabels: Record = { STOCK: '股票', CRYPTO: '加密貨幣', CASH: '現金', }; const editForm = useForm<{ symbol: string; name: string; baseCurrency: string }>({ resolver: zodResolver(z.object({ symbol: z.string().min(1, '资产代码不能为空'), name: z.string(), baseCurrency: z.string().min(2, '基础币种至少2个字符'), })), defaultValues: { symbol: '', name: '', baseCurrency: '' }, }); function handleEditClick(asset: Asset) { setEditTarget(asset); editForm.reset({ symbol: asset.symbol, name: asset.name || '', baseCurrency: asset.baseCurrency }); } function handleEditSubmit(values: { symbol: string; name: string; baseCurrency: string }) { if (!editTarget) return; startTransition(async () => { const result = await updateAsset({ id: editTarget.id, symbol: values.symbol, baseCurrency: values.baseCurrency, }); if (result.success) { setEditTarget(null); editForm.reset(); window.location.reload(); } }); } function handleDelete(asset: Asset) { startTransition(async () => { await deleteAsset(asset.id); setDeleteTarget(null); window.location.reload(); }); } return (

资产列表

数据库中所有已录入的资产 名称 资产代码 类型 基础币种 当前市价 (Latest Price) 创建时间 操作 {assets.length === 0 ? ( 暂无资产,点击"添加资产"按钮录入第一个资产 ) : ( assets.map((asset) => ( {asset.name || '-'} {asset.symbol} {typeLabels[asset.type] || asset.type} {asset.baseCurrency} {asset.latestPrice ? new Big(asset.latestPrice).toString() : '-'} {asset.createdAt ? new Date(asset.createdAt).toLocaleString('zh-CN') : '-'}
)) )}
!open && setEditTarget(null)}> 编辑资产 修改资产的基本信息
( 资产代码 )} /> ( 股票名称 )} /> ( 基础币种 )} />
!open && setDeleteTarget(null)}> 确认删除 确定要删除资产 {deleteTarget?.symbol} 吗?此操作不可撤销。
); }