"use client"; import { useState, useEffect, useRef } from "react"; import Image from "next/image"; import { Button } from "@heroui/react"; import { useApp } from "@/contexts/AppContext"; import * as echarts from "echarts"; export default function SupplyContent() { const { t } = useApp(); const [selectedPeriod, setSelectedPeriod] = useState<"1W" | "1M" | "1Y">("1W"); const chartRef = useRef(null); const chartInstance = useRef(null); // 模拟 APY 数据 const generateChartData = (period: "1W" | "1M" | "1Y") => { const dataPoints = period === "1W" ? 7 : period === "1M" ? 30 : 365; const data = []; let baseValue = 18 + Math.random() * 4; for (let i = 0; i < dataPoints; i++) { const change = (Math.random() - 0.45) * 2; baseValue = Math.max(15, Math.min(25, baseValue + change)); data.push(parseFloat(baseValue.toFixed(2))); } return data; }; const chartData = generateChartData(selectedPeriod); // 根据周期生成 X 轴标签 const getXAxisLabels = (period: "1W" | "1M" | "1Y") => { if (period === "1W") { return ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; } else if (period === "1M") { return Array.from({ length: 7 }, (_, i) => `${i * 4 + 1}d`); } else { return Array.from({ length: 7 }, (_, i) => `${i * 50 + 1}d`); } }; useEffect(() => { if (chartRef.current) { chartInstance.current = echarts.init(chartRef.current); updateChart(); } const handleResize = () => { chartInstance.current?.resize(); }; window.addEventListener("resize", handleResize); return () => { window.removeEventListener("resize", handleResize); chartInstance.current?.dispose(); }; }, [selectedPeriod]); const updateChart = () => { if (!chartInstance.current) return; const option: echarts.EChartsOption = { grid: { left: 0, right: 0, top: 10, bottom: 0, }, tooltip: { trigger: "axis", show: true, confine: true, backgroundColor: "rgba(17, 24, 39, 0.9)", borderColor: "#374151", textStyle: { color: "#f9fafb", fontSize: 12, fontWeight: 500, }, formatter: function(params: any) { const data = params[0]; return `
${data.axisValueLabel}
${data.value}%
`; }, }, xAxis: { type: "category", data: getXAxisLabels(selectedPeriod), axisLine: { show: false, }, axisTick: { show: false, }, axisLabel: { show: selectedPeriod === "1W", color: "#9ca3af", fontSize: 10, fontWeight: 500, }, }, yAxis: { type: "value", axisLine: { show: false, }, axisTick: { show: false, }, axisLabel: { show: false, }, splitLine: { show: false, }, }, series: [ { data: chartData, type: "line", smooth: true, symbol: "circle", symbolSize: 6, lineStyle: { color: "#10b981", width: 2, }, itemStyle: { color: "#10b981", }, areaStyle: { color: { type: "linear", x: 0, y: 0, x2: 0, y2: 1, colorStops: [ { offset: 0, color: "rgba(16, 185, 129, 0.3)" }, { offset: 1, color: "rgba(16, 185, 129, 0)" }, ], }, }, }, ], }; chartInstance.current.setOption(option); }; return (
{/* Header - USDC Lend Pool */}
USDC

{t("supply.usdcLendPool")}

{/* Historical APY Section */}
{/* Left - APY Display */}
{t("supply.historicalApy")}
20.5%
+2.4%
{/* Right - Period Selector */}
{/* Chart Section */}
{/* ECharts Chart */}
); }