feat(ui): 增加历史价格批量导入解析功能与底层 Upsert 接口
This commit is contained in:
+116
-13
@@ -30,7 +30,8 @@ import { SyncButton } from '@/components/assets/sync-button';
|
||||
import { AddTransactionDialog } from '@/components/transactions/add-transaction-dialog';
|
||||
import { UpdateTransactionDialog } from '@/components/transactions/update-transaction-dialog';
|
||||
import { deleteTransaction } from '@/actions/transaction';
|
||||
import { ChevronDown, ChevronUp, Plus, Edit3, Trash2 } from 'lucide-react';
|
||||
import { importHistoricalPrices } from '@/actions/market';
|
||||
import { ChevronDown, ChevronUp, Plus, Edit3, Trash2, Upload } from 'lucide-react';
|
||||
import Big from 'big.js';
|
||||
|
||||
const txTypeMap: Record<string, string> = {
|
||||
@@ -87,6 +88,9 @@ export default function DashboardPage() {
|
||||
const [updateTarget, setUpdateTarget] = useState<any>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<any>(null);
|
||||
const [snapshots, setSnapshots] = useState<any[]>([]);
|
||||
const [importDialogOpen, setImportDialogOpen] = useState(false);
|
||||
const [importAssetId, setImportAssetId] = useState<string>('');
|
||||
const [importText, setImportText] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
async function loadData() {
|
||||
@@ -141,6 +145,46 @@ export default function DashboardPage() {
|
||||
});
|
||||
}
|
||||
|
||||
function handleOpenImportDialog(assetId: string) {
|
||||
setImportAssetId(assetId);
|
||||
setImportText('');
|
||||
setImportDialogOpen(true);
|
||||
}
|
||||
|
||||
function handleImportSubmit() {
|
||||
startTransition(async () => {
|
||||
const lines = importText.split('\n').filter(l => l.trim());
|
||||
const data: Array<{ date: string; price: string }> = [];
|
||||
|
||||
for (const line of lines) {
|
||||
const parts = line.split(',');
|
||||
if (parts.length >= 2) {
|
||||
const date = parts[0].trim();
|
||||
const price = parts[1].trim();
|
||||
if (date && price) {
|
||||
data.push({ date, price });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data.length === 0) {
|
||||
toast.error('未解析到有效數據,請檢查格式');
|
||||
setImportDialogOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await importHistoricalPrices(importAssetId, data);
|
||||
if (result.success) {
|
||||
toast.success(`成功導入 ${result.imported} 條價格記錄` + (result.errors ? `,${result.errors} 條失敗` : ''));
|
||||
setImportText('');
|
||||
setImportDialogOpen(false);
|
||||
window.location.reload();
|
||||
} else {
|
||||
toast.error(result.error || '導入失敗');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const formattedTotal = formatAmount(totalCnyValue);
|
||||
const formattedTotalPnl = formatAmount(totalPnlCny);
|
||||
const formattedUnrealized = formatAmount(unrealizedPnlCny);
|
||||
@@ -271,6 +315,18 @@ export default function DashboardPage() {
|
||||
<Plus className="h-3 w-3 mr-1" />
|
||||
添加
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-7 px-2 text-xs"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleOpenImportDialog(pos.assetId);
|
||||
}}
|
||||
>
|
||||
<Upload className="h-3 w-3 mr-1" />
|
||||
导入价格
|
||||
</Button>
|
||||
</div>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
@@ -281,18 +337,32 @@ export default function DashboardPage() {
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center justify-between pb-2 border-b border-border/50">
|
||||
<span className="text-sm font-semibold text-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 className="flex items-center gap-1">
|
||||
<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>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
className="h-7 px-2 text-xs"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleOpenImportDialog(pos.assetId);
|
||||
}}
|
||||
>
|
||||
<Upload className="h-3 w-3 mr-1" />
|
||||
导入价格
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{pos.transactions && pos.transactions.length > 0 ? (
|
||||
<Table>
|
||||
@@ -437,6 +507,39 @@ export default function DashboardPage() {
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
||||
<Dialog open={importDialogOpen} onOpenChange={setImportDialogOpen}>
|
||||
<DialogContent className="sm:max-w-[550px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>导入历史价格</DialogTitle>
|
||||
<DialogDescription>
|
||||
從 Excel 複製粘貼價格數據,格式:日期, 價格(每行一條)
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="space-y-3 py-2">
|
||||
<textarea
|
||||
value={importText}
|
||||
onChange={(e) => setImportText(e.target.value)}
|
||||
placeholder={`2026-04-01, 150.5\n2026-04-02, 151.2\n2026-04-03, 149.8`}
|
||||
className="flex w-full rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring min-h-[180px] font-mono text-sm resize-y"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
支持從 Excel 直接複製粘貼。每行格式:`日期, 價格`,日期格式為 `YYYY-MM-DD`。
|
||||
</p>
|
||||
</div>
|
||||
<DialogFooter className="gap-2 sm:gap-0">
|
||||
<Button variant="outline" onClick={() => setImportDialogOpen(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleImportSubmit}
|
||||
disabled={isPending || !importText.trim()}
|
||||
>
|
||||
{isPending ? '導入中...' : '開始導入'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user