"use client"; import { useEffect, useState } from "react"; import Image from "next/image"; import { useApp } from "@/contexts/AppContext"; import { useLTV, useBorrowBalance } from "@/hooks/useCollateral"; import { useSupplyAPY } from "@/hooks/useHealthFactor"; export default function RepayStats() { const { t } = useApp(); const [mounted, setMounted] = useState(false); const { ltvRaw } = useLTV(); const { formattedBalance: borrowedBalance } = useBorrowBalance(); const { apr: supplyApr } = useSupplyAPY(); useEffect(() => { setMounted(true); }, []); const LIQUIDATION_LTV = 75; const hasPosition = mounted && parseFloat(borrowedBalance) > 0; const healthPct = hasPosition ? Math.max(0, Math.min(100, (1 - ltvRaw / LIQUIDATION_LTV) * 100)) : 0; const getHealthLabel = () => { if (!hasPosition) return 'No Position'; if (ltvRaw < 50) return `Safe ${healthPct.toFixed(0)}%`; if (ltvRaw < 65) return `Warning ${healthPct.toFixed(0)}%`; return `At Risk ${healthPct.toFixed(0)}%`; }; const getHealthColor = () => { if (!hasPosition) return 'text-text-tertiary dark:text-gray-400'; if (ltvRaw < 50) return 'text-[#10b981] dark:text-green-400'; if (ltvRaw < 65) return 'text-[#ff6900] dark:text-orange-400'; return 'text-[#ef4444] dark:text-red-400'; }; const getBarGradient = () => { if (!hasPosition) return undefined; if (ltvRaw < 50) return 'linear-gradient(90deg, rgba(0, 213, 190, 1) 0%, rgba(0, 188, 125, 1) 100%)'; if (ltvRaw < 65) return 'linear-gradient(90deg, #ff6900 0%, #ff9900 100%)'; return 'linear-gradient(90deg, #ef4444 0%, #dc2626 100%)'; }; // Net APR = getSupplyRate() directly (already represents net yield) const netApr = supplyApr > 0 ? supplyApr.toFixed(4) : null; return (
{/* Stats Info */}
{/* NET APR */}
{t("repay.netApr")}
= 0 ? 'text-[#10b981] dark:text-green-400' : 'text-[#ef4444]'}`}> {netApr !== null ? `${parseFloat(netApr) >= 0 ? '+' : ''}${netApr}%` : '--'}
{/* Position Health */}
{t("repay.positionHealth")}
{!mounted ? '--' : getHealthLabel()}
{/* Progress Bar */}
); }