({ })
| 28 | * Optimized UsageDashboard component with caching and progressive loading |
| 29 | */ |
| 30 | export const UsageDashboard: React.FC<UsageDashboardProps> = ({ }) => { |
| 31 | const [loading, setLoading] = useState(true); |
| 32 | const [error, setError] = useState<string | null>(null); |
| 33 | const [stats, setStats] = useState<UsageStats | null>(null); |
| 34 | const [sessionStats, setSessionStats] = useState<ProjectUsage[] | null>(null); |
| 35 | const [selectedDateRange, setSelectedDateRange] = useState<"all" | "7d" | "30d">("7d"); |
| 36 | const [activeTab, setActiveTab] = useState("overview"); |
| 37 | const [hasLoadedTabs, setHasLoadedTabs] = useState<Set<string>>(new Set(["overview"])); |
| 38 | |
| 39 | // Pagination states |
| 40 | const [projectsPage, setProjectsPage] = useState(1); |
| 41 | const [sessionsPage, setSessionsPage] = useState(1); |
| 42 | const ITEMS_PER_PAGE = 10; |
| 43 | |
| 44 | // Memoized formatters to prevent recreation on each render |
| 45 | const formatCurrency = useMemo(() => (amount: number): string => { |
| 46 | return new Intl.NumberFormat('en-US', { |
| 47 | style: 'currency', |
| 48 | currency: 'USD', |
| 49 | minimumFractionDigits: 2, |
| 50 | maximumFractionDigits: 2 |
| 51 | }).format(amount); |
| 52 | }, []); |
| 53 | |
| 54 | const formatNumber = useMemo(() => (num: number): string => { |
| 55 | return new Intl.NumberFormat('en-US').format(num); |
| 56 | }, []); |
| 57 | |
| 58 | const formatTokens = useMemo(() => (num: number): string => { |
| 59 | if (num >= 1_000_000) { |
| 60 | return `${(num / 1_000_000).toFixed(2)}M`; |
| 61 | } else if (num >= 1_000) { |
| 62 | return `${(num / 1_000).toFixed(1)}K`; |
| 63 | } |
| 64 | return formatNumber(num); |
| 65 | }, [formatNumber]); |
| 66 | |
| 67 | const getModelDisplayName = useCallback((model: string): string => { |
| 68 | const modelMap: Record<string, string> = { |
| 69 | "claude-4-opus": "Opus 4", |
| 70 | "claude-4-sonnet": "Sonnet 4", |
| 71 | "claude-3.5-sonnet": "Sonnet 3.5", |
| 72 | "claude-3-opus": "Opus 3", |
| 73 | }; |
| 74 | return modelMap[model] || model; |
| 75 | }, []); |
| 76 | |
| 77 | // Function to get cached data or null |
| 78 | const getCachedData = useCallback((key: string) => { |
| 79 | const cached = dataCache.get(key); |
| 80 | if (cached && Date.now() - cached.timestamp < CACHE_DURATION) { |
| 81 | return cached.data; |
| 82 | } |
| 83 | return null; |
| 84 | }, []); |
| 85 | |
| 86 | // Function to set cached data |
| 87 | const setCachedData = useCallback((key: string, data: any) => { |
nothing calls this directly
no test coverage detected