import React, { useState, useEffect } from 'react'; import { Box, Container, Grid, Typography, Alert, CircularProgress, ToggleButton, ToggleButtonGroup, Tooltip, Chip, IconButton, } from '@mui/material'; import { motion, AnimatePresence } from 'framer-motion'; import { Grid3X3, List, Info, RefreshCw } from 'lucide-react'; // Services import { billingService } from '../../services/billingService'; import { monitoringService } from '../../services/monitoringService'; import { onApiEvent } from '../../utils/apiEvents'; // Types import { DashboardData } from '../../types/billing'; import { SystemHealth } from '../../types/monitoring'; // Components import CompactBillingDashboard from './CompactBillingDashboard'; import BillingOverview from './BillingOverview'; import SystemHealthIndicator from '../monitoring/SystemHealthIndicator'; import CostBreakdown from './CostBreakdown'; import UsageTrends from './UsageTrends'; import UsageAlerts from './UsageAlerts'; import ComprehensiveAPIBreakdown from './ComprehensiveAPIBreakdown'; interface EnhancedBillingDashboardProps { userId?: string; } type ViewMode = 'compact' | 'detailed'; const EnhancedBillingDashboard: React.FC = ({ userId }) => { const [dashboardData, setDashboardData] = useState(null); const [systemHealth, setSystemHealth] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [viewMode, setViewMode] = useState('compact'); const fetchDashboardData = async () => { try { const [billingData, healthData] = await Promise.all([ billingService.getDashboardData(), monitoringService.getSystemHealth() ]); setDashboardData(billingData); setSystemHealth(healthData); } catch (error) { setError(error instanceof Error ? error.message : 'Failed to fetch dashboard data'); } finally { setLoading(false); } }; useEffect(() => { fetchDashboardData(); }, [userId]); // Event-driven refresh: refresh only when non-billing/monitoring APIs complete useEffect(() => { const unsubscribe = onApiEvent((detail) => { if (detail.source && detail.source !== 'other') return; Promise.all([billingService.getDashboardData(), monitoringService.getSystemHealth()]) .then(([billingData, health]) => { setDashboardData(billingData); setSystemHealth(health); }) .catch(() => {/* ignore */}); }); return unsubscribe; }, []); // Refetch when tab becomes visible again (cheap, avoids polling) useEffect(() => { const onVisible = () => { if (document.visibilityState === 'visible') { fetchDashboardData(); } }; document.addEventListener('visibilitychange', onVisible); return () => document.removeEventListener('visibilitychange', onVisible); }, []); const handleViewModeChange = ( event: React.MouseEvent, newViewMode: ViewMode | null, ) => { if (newViewMode !== null) { setViewMode(newViewMode); } }; if (loading) { return ( ); } if (error) { return ( {error} ); } if (!dashboardData) { return ( No billing data available. Please check your subscription status. ); } return ( {/* Header */} Billing & Usage Dashboard AI Usage Monitoring Track your AI API costs, usage patterns, and system performance in real-time } arrow placement="top" > {/* Active Providers Chips */} {dashboardData && ( {Object.entries(dashboardData.current_usage.provider_breakdown) .filter(([_, data]) => data.cost > 0) .map(([provider, data]) => ( {provider.toUpperCase()} Usage Cost: ${data.cost.toFixed(4)} Calls: {data.calls.toLocaleString()} Tokens: {data.tokens.toLocaleString()} } arrow placement="top" > ))} )} {/* View Mode Toggle and Refresh */} View Modes Compact: Essential metrics only Detailed: Full breakdown with charts } arrow placement="top" > Compact Detailed {/* Dashboard Content */} {viewMode === 'compact' ? ( ) : ( {/* Top Row */} { // TODO: Implement mark as read functionality console.log('Mark alert as read:', alertId); }} /> {/* Middle Row */} {/* Bottom Row - Comprehensive API Breakdown */} )} ); }; export default EnhancedBillingDashboard;