// Main Dashboard Controller class Dashboard { constructor() { this.currentPage = 'overview'; this.pages = {}; this.init(); } init() { // Initialize only if authenticated if (!window.authManager || !window.authManager.isAuthenticated) { return; } this.setupNavigation(); this.setupSidebar(); this.setupRefresh(); this.updateTime(); this.loadPage(this.currentPage); // Start time updates setInterval(() => this.updateTime(), 1000); } setupNavigation() { // Handle menu item clicks const menuItems = document.querySelectorAll('.menu-item'); menuItems.forEach(item => { item.addEventListener('click', (e) => { e.preventDefault(); // Get page from data attribute or href const page = item.getAttribute('data-page') || item.getAttribute('href').substring(1); // Update active state menuItems.forEach(i => i.classList.remove('active')); item.classList.add('active'); // Load page this.loadPage(page); }); }); // Handle hash changes (browser back/forward) window.addEventListener('hashchange', () => { const page = window.location.hash.substring(1) || 'overview'; this.loadPage(page); }); } setupSidebar() { const toggleBtn = document.getElementById('sidebar-toggle'); const sidebar = document.querySelector('.sidebar'); if (toggleBtn && sidebar) { toggleBtn.addEventListener('click', () => { sidebar.classList.toggle('collapsed'); // Save preference const isCollapsed = sidebar.classList.contains('collapsed'); localStorage.setItem('sidebar_collapsed', isCollapsed); }); // Load saved preference const savedState = localStorage.getItem('sidebar_collapsed'); if (savedState === 'true') { sidebar.classList.add('collapsed'); } } } setupRefresh() { const refreshBtn = document.getElementById('refresh-btn'); if (refreshBtn) { refreshBtn.addEventListener('click', () => { this.refreshCurrentPage(); }); } } updateTime() { const timeElement = document.getElementById('current-time'); if (!timeElement) return; const now = luxon.DateTime.now(); timeElement.textContent = now.toFormat('HH:mm:ss'); } async loadPage(page) { // Update current page this.currentPage = page; // Update URL hash window.location.hash = page; // Update page title this.updatePageTitle(page); // Show loading state this.showLoading(); try { // Load page content await this.loadPageContent(page); // Initialize page-specific functionality await this.initializePage(page); } catch (error) { console.error(`Error loading page ${page}:`, error); this.showError(`Failed to load ${page} page`); } finally { // Hide loading state this.hideLoading(); } } updatePageTitle(page) { const titleElement = document.getElementById('page-title'); if (!titleElement) return; const titles = { 'overview': 'Dashboard Overview', 'analytics': 'Usage Analytics', 'costs': 'Cost Management', 'clients': 'Client Management', 'providers': 'Provider Configuration', 'monitoring': 'Real-time Monitoring', 'settings': 'System Settings', 'logs': 'System Logs' }; titleElement.textContent = titles[page] || 'Dashboard'; } showLoading() { const content = document.getElementById('page-content'); if (!content) return; content.classList.add('loading'); } hideLoading() { const content = document.getElementById('page-content'); if (!content) return; content.classList.remove('loading'); } async loadPageContent(page) { const content = document.getElementById('page-content'); if (!content) return; // For now, we'll generate content dynamically // In a real app, you might fetch HTML templates or use a framework let html = ''; switch (page) { case 'overview': html = await this.getOverviewContent(); break; case 'analytics': html = await this.getAnalyticsContent(); break; case 'costs': html = await this.getCostsContent(); break; case 'clients': html = await this.getClientsContent(); break; case 'providers': html = await this.getProvidersContent(); break; case 'monitoring': html = await this.getMonitoringContent(); break; case 'settings': html = await this.getSettingsContent(); break; case 'logs': html = await this.getLogsContent(); break; default: html = '

Page not found

'; } content.innerHTML = html; } async initializePage(page) { // Initialize page-specific JavaScript switch (page) { case 'overview': if (typeof window.initOverview === 'function') { await window.initOverview(); } break; case 'analytics': if (typeof window.initAnalytics === 'function') { await window.initAnalytics(); } break; case 'costs': if (typeof window.initCosts === 'function') { await window.initCosts(); } break; case 'clients': if (typeof window.initClients === 'function') { await window.initClients(); } break; case 'providers': if (typeof window.initProviders === 'function') { await window.initProviders(); } break; case 'monitoring': if (typeof window.initMonitoring === 'function') { await window.initMonitoring(); } break; case 'settings': if (typeof window.initSettings === 'function') { await window.initSettings(); } break; case 'logs': if (typeof window.initLogs === 'function') { await window.initLogs(); } break; } } refreshCurrentPage() { this.loadPage(this.currentPage); // Show refresh animation const refreshBtn = document.getElementById('refresh-btn'); if (refreshBtn) { refreshBtn.classList.add('fa-spin'); setTimeout(() => { refreshBtn.classList.remove('fa-spin'); }, 1000); } // Show toast notification if (window.authManager) { window.authManager.showToast('Page refreshed', 'success'); } } showError(message) { const content = document.getElementById('page-content'); if (!content) return; content.innerHTML = `

Error

${message}

`; } // Page content generators async getOverviewContent() { return `

Request Volume (Last 24 Hours)

Provider Distribution

System Health

Recent Requests

Last 50 requests

Time Client Provider Model Tokens Status
`; } async getAnalyticsContent() { return `

Usage Analytics

Filter and analyze usage data

Request Trends

Top Clients

Top Models

Detailed Usage Data

Date Client Provider Model Requests Tokens Cost
`; } async getCostsContent() { return `

Cost Breakdown

Budget Tracking

Cost Projections

Pricing Configuration

Current provider pricing

Provider Model Input Price Output Price Last Updated
`; } async getClientsContent() { return `

Client Management

Manage API clients and tokens

Client ID Name Token Created Last Used Requests Status Actions

Client Usage Summary

Rate Limit Status

`; } async getProvidersContent() { return `

Provider Configuration

Model Availability

Connection Tests

`; } async getMonitoringContent() { return `

Real-time Monitoring

Live request stream and system metrics

Live Request Stream

System Metrics

Response Time (ms)

Error Rate (%)

Rate Limit Usage

System Logs (Live)

`; } async getSettingsContent() { return `

System Settings

General Configuration

Database Settings

Security Settings

Database Management

System Information

`; } async getLogsContent() { return `

System Logs

View and filter system logs

Timestamp Level Source Message
`; } } // Initialize dashboard when DOM is loaded document.addEventListener('DOMContentLoaded', () => { window.initDashboard = () => { window.dashboard = new Dashboard(); }; // If already authenticated, initialize immediately if (window.authManager && window.authManager.isAuthenticated) { window.initDashboard(); } }); // Export for use in other modules if (typeof module !== 'undefined' && module.exports) { module.exports = Dashboard; }