import { useState, useEffect } from 'react'; import { CurrentPeriod } from './CurrentPeriod'; import { HistoricalPeriods } from './HistoricalPeriods'; interface DashboardProps { apiKey: string; userId: string; onLogout: () => void; } interface Period { index: number; startSnapshotId: number | null; startAt: string | null; endAt: string | null; isCurrent: boolean; } export function Dashboard({ apiKey, userId, onLogout }: DashboardProps) { const [activeTab, setActiveTab] = useState<'current' | 'historical'>('current'); const [periods, setPeriods] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(''); useEffect(() => { fetchPeriods(); }, []); const fetchPeriods = async () => { setIsLoading(true); setError(''); try { const response = await fetch('/api/periods', { headers: { 'X-API-Key': apiKey, }, }); if (!response.ok) { throw new Error(`Failed to fetch periods: ${response.status}`); } const data = await response.json(); setPeriods(data.periods || []); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load periods'); } finally { setIsLoading(false); } }; const currentPeriod = periods.find(p => p.isCurrent); const historicalPeriods = periods.filter(p => !p.isCurrent); if (isLoading) { return (

Loading dashboard...

); } if (error) { return (

Error Loading Dashboard

{error}

); } return (
{activeTab === 'current' && currentPeriod && ( )} {activeTab === 'historical' && ( )} {activeTab === 'current' && !currentPeriod && (

No current period found. This happens when there are no billing snapshots yet.

)}
); }