import { useCallback, useEffect, useState } from "react"; import { apiFetch } from "../api/client"; export type FundsSummary = { ok: boolean; exchange: string; trading_day: string; total_trades: number; win_rate: number; profit_loss_ratio: number | null; total_funds: number | null; funding_usdt: number | null; trading_usdt: number | null; funding_usdc?: number | null; trading_usdc?: number | null; funding_label?: string; trading_label?: string; realtime_pnl: number | null; usdc_usdt_rate?: number; perp_inst_id?: string; detail?: string; }; function fmtU(n: number | null | undefined) { if (n == null || Number.isNaN(n)) return "—"; return `${n.toFixed(2)}U`; } function pnlClass(n: number | null | undefined) { if (n == null || Number.isNaN(n) || n === 0) return ""; return n > 0 ? "pos-pnl-profit" : "pos-pnl-loss"; } export default function FundsBar() { const [s, setS] = useState(null); const load = useCallback(() => { apiFetch("/api/funds/summary") .then(setS) .catch(() => { /* 顶栏静默失败,避免刷屏 */ }); }, []); useEffect(() => { load(); const t = window.setInterval(load, 5000); return () => window.clearInterval(t); }, [load]); if (!s?.ok) return null; return (
交易所
{s.exchange || "—"}
交易日
{s.trading_day}
总交易
{s.total_trades}
胜率
{(s.win_rate * 100).toFixed(0)}%
盈亏比
{s.profit_loss_ratio != null ? s.profit_loss_ratio.toFixed(2) : "—"}
总资金
{fmtU(s.total_funds)}
资金账户
{s.funding_label || fmtU(s.funding_usdt)}
交易账户
{s.trading_label || fmtU(s.trading_usdt)}
实时盈亏
{s.realtime_pnl == null ? "—" : fmtU(s.realtime_pnl)}
); }