import React from 'react'; import { Link } from 'react-router-dom'; import { FaArrowRight, FaCoins } from 'react-icons/fa'; interface CreditBalanceBannerProps { used: number; granted: number; /** Remaining balance; falls back to granted - used when absent. */ balance?: number; historyTo?: string; } /** * Slim always-on credit indicator for the top of the dashboard: used / granted * with a usage bar and a link to the credit history. Always rendered — falls * back to "0 credits remaining" when the balance is empty or errored. * * @param {CreditBalanceBannerProps} props - Credit figures + history link. * @return {React.ReactElement} The banner. */ export function CreditBalanceBanner({ used, granted, balance, historyTo = '/pricing#credit-history', }: CreditBalanceBannerProps) { // Always rendered. With no allowance/balance it shows "0 credits remaining". const hasAllowance = granted > 0; const hasBalance = typeof balance === 'number'; const safeUsed = Math.max(0, used); const remaining = hasBalance ? (balance as number) : Math.max(0, granted - safeUsed); const usageRatio = hasAllowance ? Math.min(1, safeUsed / granted) : 0; const remainingRatio = hasAllowance ? Math.min(1, Math.max(0, remaining / granted)) : 1; const barClass = remainingRatio <= 0.2 ? 'bg-gradient-to-r from-rose-500 to-red-400' : remainingRatio <= 0.5 ? 'bg-gradient-to-r from-amber-500 to-yellow-400' : 'bg-gradient-to-r from-emerald-500 to-green-400'; return (
{safeUsed.toLocaleString()}{' '} / {granted.toLocaleString()} credits used · {remaining.toLocaleString()} remaining
) : ({remaining.toLocaleString()}{' '} credits remaining
)} {hasAllowance && (