"use client"; import { useEffect, useState, useCallback } from "react"; import { api, RatesResponse, StatsResponse, HistoryResponse, HistoryPoint } from "@/lib/api"; import RateCard from "@/components/RateCard"; import StatsCard from "@/components/StatsCard"; import FundingChart from "@/components/FundingChart"; import { LineChart, Line, XAxis, YAxis, Tooltip, Legend, ResponsiveContainer, ReferenceLine } from "recharts"; export default function Dashboard() { const [rates, setRates] = useState(null); const [stats, setStats] = useState(null); const [history, setHistory] = useState(null); const [status, setStatus] = useState<"loading" | "running" | "error">("loading"); const [lastUpdate, setLastUpdate] = useState(""); const fetchRates = useCallback(async () => { try { const r = await api.rates(); setRates(r); setStatus("running"); setLastUpdate(new Date().toLocaleTimeString("zh-CN")); } catch { setStatus("error"); } }, []); const fetchAll = useCallback(async () => { try { const [s, h] = await Promise.all([api.stats(), api.history()]); setStats(s); setHistory(h); } catch {} }, []); useEffect(() => { fetchRates(); fetchAll(); const rateInterval = setInterval(fetchRates, 2_000); const slowInterval = setInterval(fetchAll, 120_000); return () => { clearInterval(rateInterval); clearInterval(slowInterval); }; }, [fetchRates, fetchAll]); // 历史费率表格数据 const btcMap = new Map((history?.BTC ?? []).map((p: HistoryPoint) => [p.timestamp.slice(0, 13), p.fundingRate * 100])); const ethMap = new Map((history?.ETH ?? []).map((p: HistoryPoint) => [p.timestamp.slice(0, 13), p.fundingRate * 100])); const allTimes = Array.from(new Set([...btcMap.keys(), ...ethMap.keys()])).sort(); const historyChartData = allTimes.slice(-42).map((t) => ({ time: t.slice(5).replace("T", " "), BTC: btcMap.get(t) ?? null, ETH: ethMap.get(t) ?? null, })); const tableData = allTimes.slice().reverse().slice(0, 50).map((t) => ({ time: t.replace("T", " "), btc: btcMap.get(t), eth: ethMap.get(t), })); return (
{/* Header */}

资金费率套利监控

实时监控 BTC / ETH 永续合约资金费率

{status === "running" ? "运行中" : status === "error" ? "连接失败" : "加载中..."} {lastUpdate && 更新于 {lastUpdate}}
{/* Rate Cards */}
{/* Stats Cards */} {stats && (
)} {/* 7天走势图(FundingChart) */} {history && (

过去7天资金费率走势

)} {/* 历史费率折线图 */} {allTimes.length > 0 && (

历史费率走势(过去7天,每8小时结算)

`${v.toFixed(3)}%`} width={60} /> [`${Number(v).toFixed(4)}%`]} contentStyle={{ background: "#fff", border: "1px solid #e2e8f0", borderRadius: 8, fontSize: 12 }} />
)} {/* 历史费率明细表 */} {tableData.length > 0 && (

历史费率明细(最近50条)

{tableData.map((row, i) => ( ))}
时间 BTC 费率 ETH 费率
{row.time} = 0 ? "text-emerald-600" : "text-red-500"}`}> {row.btc != null ? `${row.btc.toFixed(4)}%` : "--"} = 0 ? "text-emerald-600" : "text-red-500"}`}> {row.eth != null ? `${row.eth.toFixed(4)}%` : "--"}
)} {/* Strategy note */}
策略原理: 持有现货多头 + 永续空头,每8小时收取资金费率,赚取无方向风险的稳定收益。
); }