import {Alert, Badge, Button, Card, CardContent, CardDescription, CardHeader, CardTitle, IconSpinner} from '@plunk/ui'; import {AlertCircle, Download, ExternalLink, FileText} from 'lucide-react'; import {useBillingInvoices} from '../lib/hooks/useBillingInvoices'; interface BillingInvoicesProps { projectId: string; hasSubscription: boolean; onManageBilling?: () => void; } export function BillingInvoices({projectId, hasSubscription, onManageBilling}: BillingInvoicesProps) { const {invoicesData, isLoading, error} = useBillingInvoices(projectId, hasSubscription); if (!hasSubscription) { return ( Past Invoices View and download your billing history

Invoice history is only available with an active subscription. Start a subscription to view your invoices.

); } if (isLoading) { return ( Past Invoices View and download your billing history
); } if (error) { return ( Past Invoices View and download your billing history

Failed to load invoices. Please try again later.

); } if (!invoicesData) { 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', }); }; const getStatusBadge = (status: string, paid: boolean) => { // Stripe invoices have status 'paid' when paid, or paid boolean is true if (paid || status === 'paid') { return Paid; } switch (status) { case 'open': return Unpaid; case 'draft': return Draft; case 'uncollectible': return Uncollectible; case 'void': return Void; default: return {status}; } }; const isPaid = (invoice: {status: string; paid: boolean; amountDue: number}) => { // Check multiple conditions: paid flag, status, or zero amount due return ( invoice.paid === true || invoice.status === 'paid' || (invoice.amountDue === 0 && invoice.status !== 'draft') ); }; // Show only the 5 most recent invoices const recentInvoices = invoicesData.invoices.slice(0, 5); const hasMoreInvoices = invoicesData.invoices.length > 5; return ( Recent Invoices View your latest invoices (showing {recentInvoices.length} most recent) {invoicesData.invoices.length === 0 ? (

No invoices found. Invoices will appear here after your first billing period.

) : (
{recentInvoices.map(invoice => (

{invoice.number || `Invoice ${invoice.id.slice(-8)}`}

{getStatusBadge(invoice.status, invoice.paid)}

Date: {formatDate(invoice.created)}

{invoice.periodStart && invoice.periodEnd && (

Period: {formatDate(invoice.periodStart)} -{' '} {formatDate(invoice.periodEnd)}

)} {isPaid(invoice) ? (

Amount Paid:{' '} {formatCurrency(invoice.amountPaid, invoice.currency)}

) : ( <>

Total:{' '} {formatCurrency(invoice.total, invoice.currency)}

Amount Due:{' '} {formatCurrency(invoice.amountDue, invoice.currency)}

)}
{/* Actions */}
{invoice.hostedInvoiceUrl && ( )} {invoice.invoicePdf && ( )}
))}
{/* Message for viewing full history */} {hasMoreInvoices && (

Showing 5 most recent invoices. For complete billing history,{' '} {onManageBilling ? ( <> visit the{' '} . ) : ( 'use the Manage Billing button above to access the Customer Portal.' )}

)}
)}
); }