import { useEffect, useState } from 'react'; import { useBillingStore } from '../../store/billingStore'; import ComponentCard from '../common/ComponentCard'; export default function UsageChartWidget() { const { usageSummary, loading, loadUsageSummary } = useBillingStore(); const [dateRange, setDateRange] = useState<'week' | 'month' | 'year'>('month'); useEffect(() => { const now = new Date(); let startDate: string; if (dateRange === 'week') { const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); startDate = weekAgo.toISOString().split('T')[0]; } else if (dateRange === 'month') { startDate = new Date(now.getFullYear(), now.getMonth(), 1).toISOString().split('T')[0]; } else { startDate = new Date(now.getFullYear(), 0, 1).toISOString().split('T')[0]; } const endDate = now.toISOString().split('T')[0]; loadUsageSummary(startDate, endDate); }, [dateRange, loadUsageSummary]); if (loading && !usageSummary) { return ( Loading usage data... ); } if (!usageSummary) return null; return ( setDateRange(e.target.value as 'week' | 'month' | 'year')} > Last 7 Days This Month This Year Total Credits Used {usageSummary.total_credits_used} Total Cost ${(Number(usageSummary.total_cost_usd) || 0).toFixed(2)} By Operation {Object.entries(usageSummary.by_operation).map(([op, stats]) => ( {op.replace('_', ' ')} {stats.credits} credits (${(Number(stats.cost) || 0).toFixed(2)}) ))} {Object.keys(usageSummary.by_operation).length === 0 && ( No usage data available )} ); }