import { useState, useCallback, useMemo, lazy, Suspense } from 'react' import { getRouteApi, useNavigate } from '@tanstack/react-router' import { useTranslation } from 'react-i18next' import { useAuthStore } from '@/stores/auth-store' import { ROLE } from '@/lib/roles' import { Skeleton } from '@/components/ui/skeleton' import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs' import { SectionPageLayout } from '@/components/layout' import { CardStaggerContainer, CardStaggerItem, FadeIn, } from '@/components/page-transition' import { ModelsChartPreferences } from './components/models/models-chart-preferences' import { ModelsFilter } from './components/models/models-filter-dialog' import { AnnouncementsPanel } from './components/overview/announcements-panel' import { ApiInfoPanel } from './components/overview/api-info-panel' import { FAQPanel } from './components/overview/faq-panel' import { SummaryCards } from './components/overview/summary-cards' import { UptimePanel } from './components/overview/uptime-panel' import { DEFAULT_TIME_GRANULARITY } from './constants' import { buildDefaultDashboardFilters, getSavedChartPreferences, saveChartPreferences, } from './lib' import { type DashboardSectionId, DASHBOARD_DEFAULT_SECTION, DASHBOARD_SECTION_IDS, } from './section-registry' import { type DashboardChartPreferences, type DashboardFilters, type QuotaDataItem, } from './types' const route = getRouteApi('/_authenticated/dashboard/$section') const LazyLogStatCards = lazy(() => import('./components/models/log-stat-cards').then((m) => ({ default: m.LogStatCards, })) ) const LazyModelCharts = lazy(() => import('./components/models/model-charts').then((m) => ({ default: m.ModelCharts, })) ) const LazyConsumptionDistributionChart = lazy(() => import('./components/models/consumption-distribution-chart').then((m) => ({ default: m.ConsumptionDistributionChart, })) ) const LazyUserCharts = lazy(() => import('./components/users/user-charts').then((m) => ({ default: m.UserCharts, })) ) function LogStatCardsFallback() { return (
{Array.from({ length: 5 }).map((_, i) => (
))}
) } function ModelChartsFallback() { return (
) } const SECTION_META: Record< DashboardSectionId, { titleKey: string; descriptionKey: string } > = { overview: { titleKey: 'Overview', descriptionKey: 'View dashboard overview and statistics', }, models: { titleKey: 'Model Call Analytics', descriptionKey: 'View model call count analytics and charts', }, users: { titleKey: 'User Analytics', descriptionKey: 'View user consumption statistics and charts', }, } export function Dashboard() { const { t } = useTranslation() const navigate = useNavigate() const params = route.useParams() const userRole = useAuthStore((state) => state.auth.user?.role) const activeSection = (params.section ?? DASHBOARD_DEFAULT_SECTION) as DashboardSectionId const [modelData, setModelData] = useState([]) const [dataLoading, setDataLoading] = useState(false) const [chartPreferences, setChartPreferences] = useState(() => getSavedChartPreferences()) const [modelFilters, setModelFilters] = useState(() => buildDefaultDashboardFilters(getSavedChartPreferences()) ) const handleFilterChange = useCallback((filters: DashboardFilters) => { setModelFilters(filters) }, []) const handleResetFilters = useCallback(() => { setModelFilters(buildDefaultDashboardFilters(chartPreferences)) }, [chartPreferences]) const handleDataUpdate = useCallback( (data: QuotaDataItem[], loading: boolean) => { setModelData(data) setDataLoading(loading) }, [] ) const handleChartPreferencesChange = useCallback( (preferences: DashboardChartPreferences) => { setChartPreferences(preferences) setModelFilters(buildDefaultDashboardFilters(preferences)) saveChartPreferences(preferences) }, [] ) const meta = SECTION_META[activeSection] ?? SECTION_META.overview const isAdmin = Boolean(userRole && userRole >= ROLE.ADMIN) const visibleSections = useMemo( () => DASHBOARD_SECTION_IDS.filter( (section) => section !== 'overview' && (section !== 'users' || isAdmin) ), [isAdmin] ) const handleSectionChange = useCallback( (section: string) => { void navigate({ to: '/dashboard/$section', params: { section: section as DashboardSectionId }, }) }, [navigate] ) const showSectionTabs = activeSection !== 'overview' && visibleSections.length > 1 const modelActions = activeSection === 'models' ? ( <> ) : null return ( {t(meta.titleKey)} {t(meta.descriptionKey)}
{activeSection !== 'overview' && (
{showSectionTabs ? ( {visibleSections.map((section) => ( {t(SECTION_META[section].titleKey)} ))} ) : (
)} {modelActions != null && (
{modelActions}
)}
)} {activeSection === 'overview' && ( <> )} {activeSection === 'models' && ( <> }> }> }> )} {activeSection === 'users' && ( }> )}
) }