import {Card, CardContent, CardDescription, CardHeader, CardTitle, Alert} from '@plunk/ui'; import {AlertCircle, TrendingUp} from 'lucide-react'; import {useBillingConsumption} from '../lib/hooks/useBillingConsumption'; import {useConfig} from '../lib/hooks/useConfig'; interface BillingConsumptionProps { projectId: string; hasSubscription: boolean; } export function BillingConsumption({projectId, hasSubscription}: BillingConsumptionProps) { const {data: config} = useConfig(); const billingEnabled = config?.features.billing.enabled ?? false; // Always call the hook to satisfy Rules of Hooks const {consumptionData, isLoading, error} = useBillingConsumption(projectId, hasSubscription && billingEnabled); if (!billingEnabled) { // If billing is globally disabled, hide the card entirely return null; } if (!hasSubscription) { return ( Usage This Month View your current month email consumption and costs

Usage tracking is only available with an active subscription. Start a subscription to track your email consumption.

); } if (isLoading) { return ( Usage This Month View your current month email consumption and costs

Loading...

); } if (error) { return ( Usage This Month View your current month email consumption and costs

Failed to load consumption data. Please try again later.

); } if (!consumptionData) { return null; } const formatCurrency = (amount: number, currency: string) => { return new Intl.NumberFormat('en-US', { style: 'currency', currency: currency.toUpperCase(), }).format(amount / 100); }; const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', }); }; return ( Usage This Month Billing period: {formatDate(consumptionData.period.start)} - {formatDate(consumptionData.period.end)}
{/* Total Usage */}

Total Emails Sent

{consumptionData.usage.total.toLocaleString()}

{/* Upcoming Invoice */} {consumptionData.upcomingInvoice && (

Upcoming Invoice

Billing Period {formatDate(consumptionData.upcomingInvoice.periodStart)} -{' '} {formatDate(consumptionData.upcomingInvoice.periodEnd)}
Subtotal {formatCurrency(consumptionData.upcomingInvoice.subtotal, consumptionData.upcomingInvoice.currency)}
Amount Due {formatCurrency( consumptionData.upcomingInvoice.amountDue, consumptionData.upcomingInvoice.currency, )}
)} {/* No upcoming invoice message */} {!consumptionData.upcomingInvoice && consumptionData.usage.total === 0 && (

No usage recorded yet for this billing period.

)}
); }