"use client"; import { useState } from "react"; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ReferenceLine, ResponsiveContainer, } from "recharts"; import { formatCurrency } from "@/lib/format"; interface Deal { time: string; profit: number; swap: number; commission: number; } type Period = "1D" | "7D" | "30D" | "ALL"; const PERIODS: Period[] = ["1D", "7D", "30D", "ALL"]; function periodStart(p: Period): Date { const now = new Date(); if (p === "1D") return new Date(now.getTime() - 86_400_000); if (p === "7D") return new Date(now.getTime() - 7 * 86_400_000); if (p === "30D") return new Date(now.getTime() - 30 * 86_400_000); return new Date("2000-01-01"); } function fmtTick(iso: string, p: Period) { const d = new Date(iso); if (p === "1D") return d.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", hour12: false }); return d.toLocaleDateString("en-US", { month: "short", day: "numeric" }); } export default function EquityChart({ deals, currency = "USD" }: { deals: Deal[]; currency?: string; }) { const [period, setPeriod] = useState("ALL"); const since = periodStart(period); const sorted = deals .filter(d => new Date(d.time) >= since) .sort((a, b) => +new Date(a.time) - +new Date(b.time)); let cum = 0; const pts = [ { label: "Start", pnl: 0 }, ...sorted.map(d => { cum += d.profit + d.swap + d.commission; return { label: fmtTick(d.time, period), pnl: parseFloat(cum.toFixed(2)) }; }), ]; const netPnl = pts[pts.length - 1]?.pnl ?? 0; const isPos = netPnl >= 0; const minPnl = Math.min(0, ...pts.map(d => d.pnl)); const maxPnl = Math.max(0, ...pts.map(d => d.pnl)); const range = maxPnl - minPnl || 1; const zeroPct = `${((maxPnl / range) * 100).toFixed(1)}%`; if (pts.length < 2) { return (
No trades in this period.
); } return (
{/* Period tabs + net P&L */}
{PERIODS.map(p => ( ))}
{isPos ? "+" : ""}{formatCurrency(netPnl, currency)}
`$${v.toFixed(0)}`} width={60} /> [`${v >= 0 ? "+" : ""}${formatCurrency(v, currency)}`, "P&L"]} contentStyle={{ background: "#141516", border: "1px solid #34343a", borderRadius: "8px", fontSize: "12px", fontFamily: "'JetBrains Mono', monospace", color: "#f7f8f8", }} labelStyle={{ color: "#8a8f98", marginBottom: 4 }} cursor={{ stroke: "#34343a", strokeWidth: 1 }} />
); }