/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
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 { FadeIn } from '@/components/page-transition'
import { ModelsChartPreferences } from './components/models/models-chart-preferences'
import { ModelsFilter } from './components/models/models-filter-dialog'
import { OverviewDashboard } from './components/overview/overview-dashboard'
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 LazyPerformanceOverview = lazy(() =>
import('./components/models/performance-overview').then((m) => ({
default: m.PerformanceOverview,
}))
)
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 (
)
}
function PerformanceOverviewFallback() {
return (
{Array.from({ length: 3 }).map((_, i) => (
))}
{Array.from({ length: 2 }).map((_, i) => (
))}
)
}
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' && (
<>
}>
{isAdmin && (
}>
)}
}>
}>
>
)}
{activeSection === 'users' && (
}>
)}
)
}