File size: 12,668 Bytes
bc2f725 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
/**
* Tab Navigation Manager
* Crypto Monitor HF - Enterprise Edition
*/
class TabManager {
constructor() {
this.currentTab = 'market';
this.tabs = {};
this.onChangeCallbacks = [];
}
/**
* Initialize tab system
*/
init() {
// Register all tabs
this.registerTab('market', 'π', 'Market', this.loadMarketTab.bind(this));
this.registerTab('api-monitor', 'π‘', 'API Monitor', this.loadAPIMonitorTab.bind(this));
this.registerTab('advanced', 'β‘', 'Advanced', this.loadAdvancedTab.bind(this));
this.registerTab('admin', 'βοΈ', 'Admin', this.loadAdminTab.bind(this));
this.registerTab('huggingface', 'π€', 'HuggingFace', this.loadHuggingFaceTab.bind(this));
this.registerTab('pools', 'π', 'Pools', this.loadPoolsTab.bind(this));
this.registerTab('providers', 'π§©', 'Providers', this.loadProvidersTab.bind(this));
this.registerTab('logs', 'π', 'Logs', this.loadLogsTab.bind(this));
this.registerTab('reports', 'π', 'Reports', this.loadReportsTab.bind(this));
// Set up event listeners
this.setupEventListeners();
// Load initial tab from URL hash or default
const hash = window.location.hash.slice(1);
const initialTab = hash && this.tabs[hash] ? hash : 'market';
this.switchTab(initialTab);
// Handle browser back/forward
window.addEventListener('popstate', () => {
const tabId = window.location.hash.slice(1) || 'market';
this.switchTab(tabId, false);
});
console.log('[TabManager] Initialized with', Object.keys(this.tabs).length, 'tabs');
}
/**
* Register a tab
*/
registerTab(id, icon, label, loadFn) {
this.tabs[id] = {
id,
icon,
label,
loadFn,
loaded: false,
};
}
/**
* Set up event listeners for tab buttons
*/
setupEventListeners() {
// Desktop navigation
document.querySelectorAll('.nav-tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
const tabId = btn.dataset.tab;
if (tabId && this.tabs[tabId]) {
this.switchTab(tabId);
}
});
// Keyboard navigation
btn.addEventListener('keydown', (e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
const tabId = btn.dataset.tab;
if (tabId && this.tabs[tabId]) {
this.switchTab(tabId);
}
}
});
});
// Mobile navigation
document.querySelectorAll('.mobile-nav-tab-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
e.preventDefault();
const tabId = btn.dataset.tab;
if (tabId && this.tabs[tabId]) {
this.switchTab(tabId);
}
});
});
}
/**
* Switch to a different tab
*/
switchTab(tabId, updateHistory = true) {
if (!this.tabs[tabId]) {
console.warn(`[TabManager] Tab ${tabId} not found`);
return;
}
// Check if feature flag disables this tab
if (window.featureFlagsManager && this.isTabDisabled(tabId)) {
this.showFeatureDisabledMessage(tabId);
return;
}
console.log(`[TabManager] Switching to tab: ${tabId}`);
// Update active state on buttons
document.querySelectorAll('[data-tab]').forEach(btn => {
if (btn.dataset.tab === tabId) {
btn.classList.add('active');
btn.setAttribute('aria-selected', 'true');
} else {
btn.classList.remove('active');
btn.setAttribute('aria-selected', 'false');
}
});
// Hide all tab content
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.remove('active');
content.setAttribute('aria-hidden', 'true');
});
// Show current tab content
const tabContent = document.getElementById(`${tabId}-tab`);
if (tabContent) {
tabContent.classList.add('active');
tabContent.setAttribute('aria-hidden', 'false');
}
// Load tab content if not already loaded
const tab = this.tabs[tabId];
if (!tab.loaded && tab.loadFn) {
tab.loadFn();
tab.loaded = true;
}
// Update URL hash
if (updateHistory) {
window.location.hash = tabId;
}
// Update current tab
this.currentTab = tabId;
// Notify listeners
this.notifyChange(tabId);
// Announce to screen readers
this.announceTabChange(tab.label);
}
/**
* Check if tab is disabled by feature flags
*/
isTabDisabled(tabId) {
if (!window.featureFlagsManager) return false;
const flagMap = {
'market': 'enableMarketOverview',
'huggingface': 'enableHFIntegration',
'pools': 'enablePoolManagement',
'advanced': 'enableAdvancedCharts',
};
const flagName = flagMap[tabId];
if (flagName) {
return !window.featureFlagsManager.isEnabled(flagName);
}
return false;
}
/**
* Show feature disabled message
*/
showFeatureDisabledMessage(tabId) {
const tab = this.tabs[tabId];
alert(`The "${tab.label}" feature is currently disabled. Enable it in Admin > Feature Flags.`);
}
/**
* Announce tab change to screen readers
*/
announceTabChange(label) {
const liveRegion = document.getElementById('sr-live-region');
if (liveRegion) {
liveRegion.textContent = `Switched to ${label} tab`;
}
}
/**
* Register change callback
*/
onChange(callback) {
this.onChangeCallbacks.push(callback);
}
/**
* Notify change callbacks
*/
notifyChange(tabId) {
this.onChangeCallbacks.forEach(callback => {
try {
callback(tabId);
} catch (error) {
console.error('[TabManager] Error in change callback:', error);
}
});
}
// ===== Tab Load Functions =====
async loadMarketTab() {
console.log('[TabManager] Loading Market tab');
try {
const marketData = await window.apiClient.getMarket();
this.renderMarketData(marketData);
} catch (error) {
console.error('[TabManager] Error loading market data:', error);
this.showError('market-tab', 'Failed to load market data');
}
}
async loadAPIMonitorTab() {
console.log('[TabManager] Loading API Monitor tab');
try {
const providers = await window.apiClient.getProviders();
this.renderAPIMonitor(providers);
} catch (error) {
console.error('[TabManager] Error loading API monitor:', error);
this.showError('api-monitor-tab', 'Failed to load API monitor data');
}
}
async loadAdvancedTab() {
console.log('[TabManager] Loading Advanced tab');
try {
const stats = await window.apiClient.getStats();
this.renderAdvanced(stats);
} catch (error) {
console.error('[TabManager] Error loading advanced data:', error);
this.showError('advanced-tab', 'Failed to load advanced data');
}
}
async loadAdminTab() {
console.log('[TabManager] Loading Admin tab');
try {
const flags = await window.apiClient.getFeatureFlags();
this.renderAdmin(flags);
} catch (error) {
console.error('[TabManager] Error loading admin data:', error);
this.showError('admin-tab', 'Failed to load admin data');
}
}
async loadHuggingFaceTab() {
console.log('[TabManager] Loading HuggingFace tab');
try {
const hfHealth = await window.apiClient.getHFHealth();
this.renderHuggingFace(hfHealth);
} catch (error) {
console.error('[TabManager] Error loading HuggingFace data:', error);
this.showError('huggingface-tab', 'Failed to load HuggingFace data');
}
}
async loadPoolsTab() {
console.log('[TabManager] Loading Pools tab');
try {
const pools = await window.apiClient.getPools();
this.renderPools(pools);
} catch (error) {
console.error('[TabManager] Error loading pools data:', error);
this.showError('pools-tab', 'Failed to load pools data');
}
}
async loadProvidersTab() {
console.log('[TabManager] Loading Providers tab');
try {
const providers = await window.apiClient.getProviders();
this.renderProviders(providers);
} catch (error) {
console.error('[TabManager] Error loading providers data:', error);
this.showError('providers-tab', 'Failed to load providers data');
}
}
async loadLogsTab() {
console.log('[TabManager] Loading Logs tab');
try {
const logs = await window.apiClient.getRecentLogs();
this.renderLogs(logs);
} catch (error) {
console.error('[TabManager] Error loading logs:', error);
this.showError('logs-tab', 'Failed to load logs');
}
}
async loadReportsTab() {
console.log('[TabManager] Loading Reports tab');
try {
const discoveryReport = await window.apiClient.getDiscoveryReport();
const modelsReport = await window.apiClient.getModelsReport();
this.renderReports({ discoveryReport, modelsReport });
} catch (error) {
console.error('[TabManager] Error loading reports:', error);
this.showError('reports-tab', 'Failed to load reports');
}
}
// ===== Render Functions (Delegated to dashboard.js) =====
renderMarketData(data) {
if (window.dashboardApp && window.dashboardApp.renderMarketTab) {
window.dashboardApp.renderMarketTab(data);
}
}
renderAPIMonitor(data) {
if (window.dashboardApp && window.dashboardApp.renderAPIMonitorTab) {
window.dashboardApp.renderAPIMonitorTab(data);
}
}
renderAdvanced(data) {
if (window.dashboardApp && window.dashboardApp.renderAdvancedTab) {
window.dashboardApp.renderAdvancedTab(data);
}
}
renderAdmin(data) {
if (window.dashboardApp && window.dashboardApp.renderAdminTab) {
window.dashboardApp.renderAdminTab(data);
}
}
renderHuggingFace(data) {
if (window.dashboardApp && window.dashboardApp.renderHuggingFaceTab) {
window.dashboardApp.renderHuggingFaceTab(data);
}
}
renderPools(data) {
if (window.dashboardApp && window.dashboardApp.renderPoolsTab) {
window.dashboardApp.renderPoolsTab(data);
}
}
renderProviders(data) {
if (window.dashboardApp && window.dashboardApp.renderProvidersTab) {
window.dashboardApp.renderProvidersTab(data);
}
}
renderLogs(data) {
if (window.dashboardApp && window.dashboardApp.renderLogsTab) {
window.dashboardApp.renderLogsTab(data);
}
}
renderReports(data) {
if (window.dashboardApp && window.dashboardApp.renderReportsTab) {
window.dashboardApp.renderReportsTab(data);
}
}
/**
* Show error message in tab
*/
showError(tabId, message) {
const tabElement = document.getElementById(tabId);
if (tabElement) {
const contentArea = tabElement.querySelector('.tab-body') || tabElement;
contentArea.innerHTML = `
<div class="alert alert-error">
<strong>β Error:</strong> ${message}
</div>
`;
}
}
}
// Create global instance
window.tabManager = new TabManager();
// Auto-initialize on DOMContentLoaded
document.addEventListener('DOMContentLoaded', () => {
window.tabManager.init();
});
console.log('[TabManager] Module loaded');
|