/** * Account Info Widget * Displays account-related billing information: * - Credits consumed/remaining this billing period * - Reset day (when credits refresh) * - Last payment date * - Next payment due date * - Current plan/package type */ import React from 'react'; import { useNavigate } from 'react-router-dom'; import ComponentCard from '../common/ComponentCard'; import Button from '../ui/button/Button'; import Badge from '../ui/badge/Badge'; import { CreditBalance, Subscription, Plan } from '../../services/billing.api'; import { CalendarIcon, CreditCardIcon, UserCircleIcon, ArrowRightIcon, } from '../../icons'; interface AccountInfoWidgetProps { balance: CreditBalance | null; subscription?: Subscription | null; plan?: Plan | null; userPlan?: any; // Plan from user.account.plan loading?: boolean; } // Helper to format dates function formatDate(dateStr: string | undefined): string { if (!dateStr) return '—'; try { const date = new Date(dateStr); return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); } catch { return '—'; } } // Helper to get days until date function getDaysUntil(dateStr: string | undefined): number | null { if (!dateStr) return null; try { const date = new Date(dateStr); const now = new Date(); const diff = date.getTime() - now.getTime(); return Math.ceil(diff / (1000 * 60 * 60 * 24)); } catch { return null; } } export default function AccountInfoWidget({ balance, subscription, plan, userPlan, loading = false, }: AccountInfoWidgetProps) { const navigate = useNavigate(); if (loading) { return (
{[1, 2, 3, 4].map((i) => (
))}
); } const periodEnd = subscription?.current_period_end; const daysUntilReset = getDaysUntil(periodEnd); // Resolve plan - prefer explicit plan, then subscription plan, then userPlan const currentPlan = plan || (subscription?.plan && typeof subscription.plan === 'object' ? subscription.plan : null) || userPlan; return ( navigate('/account/settings')} endIcon={} > Manage } >
{/* Current Plan */}

Current Plan

{currentPlan?.name || 'Free Plan'}

{subscription?.status && ( {subscription.status} )}
{/* Info Grid */}
{/* Credits This Period */}

Used This Period

{balance?.credits_used_this_month?.toLocaleString() || '0'}

of {balance?.plan_credits_per_month?.toLocaleString() || '0'} credits

{/* Credits Remaining */}

Remaining

{balance?.credits_remaining?.toLocaleString() || '0'}

credits available

{/* Reset Date */}

Credits Reset

{formatDate(periodEnd)}

{daysUntilReset !== null && daysUntilReset > 0 && (

in {daysUntilReset} day{daysUntilReset !== 1 ? 's' : ''}

)}
{/* Next Payment */}

Next Payment

{formatDate(periodEnd)}

{currentPlan?.price && (

${currentPlan.price}/mo

)}
{/* Upgrade CTA (if on free or lower plan) */} {(!currentPlan || currentPlan.slug === 'free' || currentPlan.slug === 'starter') && ( )}
); }