import { useCallback, useEffect, useState } from "react"; import { apiFetch } from "../api/client"; export type FundsSummary = { ok: boolean; exchange: string; mode?: 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 fmtUsdc(n: number | null | undefined) { if (n == null || Number.isNaN(n)) return "—"; return `${n.toFixed(2)} USDC`; } function pnlClass(n: number | null | undefined) { if (n == null || Number.isNaN(n) || n === 0) return ""; return n > 0 ? "pos-pnl-profit" : "pos-pnl-loss"; } function DualCcy({ usdt, usdc, }: { usdt: number | null | undefined; usdc: number | null | undefined; }) { return (
{fmtU(usdt)}
{fmtUsdc(usdc)}
); } export default function FundsBar() { const [s, setS] = useState(null); const [err, setErr] = useState(""); const load = useCallback(() => { apiFetch("/api/funds/summary") .then((r) => { setS(r); setErr(r.ok ? "" : r.detail || "资金摘要不可用"); }) .catch((e) => { setErr(e instanceof Error ? e.message : String(e)); }); }, []); useEffect(() => { load(); const t = window.setInterval(load, 5000); const onRefresh = () => load(); window.addEventListener("funds-refresh", onRefresh); return () => { window.clearInterval(t); window.removeEventListener("funds-refresh", onRefresh); }; }, [load]); if (!s?.ok) { if (!err) return null; return (
{err}
); } 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.realtime_pnl == null ? "—" : `${s.realtime_pnl > 0 ? "+" : ""}${fmtU(s.realtime_pnl)}`}
); }