forked from ruvnet/RuView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
296 lines (246 loc) · 8.61 KB
/
app.js
File metadata and controls
296 lines (246 loc) · 8.61 KB
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
// WiFi DensePose Application - Main Entry Point
import { TabManager } from './components/TabManager.js';
import { DashboardTab } from './components/DashboardTab.js';
import { HardwareTab } from './components/HardwareTab.js';
import { LiveDemoTab } from './components/LiveDemoTab.js';
import { apiService } from './services/api.service.js';
import { wsService } from './services/websocket.service.js';
import { healthService } from './services/health.service.js';
import { backendDetector } from './utils/backend-detector.js';
class WiFiDensePoseApp {
constructor() {
this.components = {};
this.isInitialized = false;
}
// Initialize application
async init() {
try {
console.log('Initializing WiFi DensePose UI...');
// Set up error handling
this.setupErrorHandling();
// Initialize services
await this.initializeServices();
// Initialize UI components
this.initializeComponents();
// Set up global event listeners
this.setupEventListeners();
this.isInitialized = true;
console.log('WiFi DensePose UI initialized successfully');
} catch (error) {
console.error('Failed to initialize application:', error);
this.showGlobalError('Failed to initialize application. Please refresh the page.');
}
}
// Initialize services
async initializeServices() {
// Add request interceptor for error handling
apiService.addResponseInterceptor(async (response, url) => {
if (!response.ok && response.status === 401) {
console.warn('Authentication required for:', url);
// Handle authentication if needed
}
return response;
});
// Detect backend availability and initialize accordingly
const useMock = await backendDetector.shouldUseMockServer();
if (useMock) {
console.log('🧪 Initializing with mock server for testing');
// Import and start mock server only when needed
const { mockServer } = await import('./utils/mock-server.js');
mockServer.start();
// Show notification to user
this.showBackendStatus('Mock server active - testing mode', 'warning');
} else {
console.log('🔌 Initializing with real backend');
// Verify backend is actually working
try {
const health = await healthService.checkLiveness();
console.log('✅ Backend is available and responding:', health);
this.showBackendStatus('Connected to real backend', 'success');
} catch (error) {
console.error('❌ Backend check failed:', error);
this.showBackendStatus('Backend connection failed', 'error');
// Don't throw - let the app continue and retry later
}
}
}
// Initialize UI components
initializeComponents() {
const container = document.querySelector('.container');
if (!container) {
throw new Error('Main container not found');
}
// Initialize tab manager
this.components.tabManager = new TabManager(container);
this.components.tabManager.init();
// Initialize tab components
this.initializeTabComponents();
// Set up tab change handling
this.components.tabManager.onTabChange((newTab, oldTab) => {
this.handleTabChange(newTab, oldTab);
});
}
// Initialize individual tab components
initializeTabComponents() {
// Dashboard tab
const dashboardContainer = document.getElementById('dashboard');
if (dashboardContainer) {
this.components.dashboard = new DashboardTab(dashboardContainer);
this.components.dashboard.init().catch(error => {
console.error('Failed to initialize dashboard:', error);
});
}
// Hardware tab
const hardwareContainer = document.getElementById('hardware');
if (hardwareContainer) {
this.components.hardware = new HardwareTab(hardwareContainer);
this.components.hardware.init();
}
// Live demo tab
const demoContainer = document.getElementById('demo');
if (demoContainer) {
this.components.demo = new LiveDemoTab(demoContainer);
this.components.demo.init();
}
// Architecture tab - static content, no component needed
// Performance tab - static content, no component needed
// Applications tab - static content, no component needed
}
// Handle tab changes
handleTabChange(newTab, oldTab) {
console.log(`Tab changed from ${oldTab} to ${newTab}`);
// Stop demo if leaving demo tab
if (oldTab === 'demo' && this.components.demo) {
this.components.demo.stopDemo();
}
// Update components based on active tab
switch (newTab) {
case 'dashboard':
// Dashboard auto-updates when visible
break;
case 'hardware':
// Hardware visualization is always active
break;
case 'demo':
// Demo starts manually
break;
}
}
// Set up global event listeners
setupEventListeners() {
// Handle window resize
window.addEventListener('resize', () => {
this.handleResize();
});
// Handle visibility change
document.addEventListener('visibilitychange', () => {
this.handleVisibilityChange();
});
// Handle before unload
window.addEventListener('beforeunload', () => {
this.cleanup();
});
}
// Handle window resize
handleResize() {
// Update canvas sizes if needed
const canvases = document.querySelectorAll('canvas');
canvases.forEach(canvas => {
const rect = canvas.parentElement.getBoundingClientRect();
if (canvas.width !== rect.width || canvas.height !== rect.height) {
canvas.width = rect.width;
canvas.height = rect.height;
}
});
}
// Handle visibility change
handleVisibilityChange() {
if (document.hidden) {
// Pause updates when page is hidden
console.log('Page hidden, pausing updates');
healthService.stopHealthMonitoring();
} else {
// Resume updates when page is visible
console.log('Page visible, resuming updates');
healthService.startHealthMonitoring();
}
}
// Set up error handling
setupErrorHandling() {
window.addEventListener('error', (event) => {
if (event.error) {
console.error('Global error:', event.error);
this.showGlobalError('An unexpected error occurred');
}
});
window.addEventListener('unhandledrejection', (event) => {
if (event.reason) {
console.error('Unhandled promise rejection:', event.reason);
this.showGlobalError('An unexpected error occurred');
}
});
}
// Show backend status notification
showBackendStatus(message, type) {
// Create status notification if it doesn't exist
let statusToast = document.getElementById('backendStatusToast');
if (!statusToast) {
statusToast = document.createElement('div');
statusToast.id = 'backendStatusToast';
statusToast.className = 'backend-status-toast';
document.body.appendChild(statusToast);
}
statusToast.textContent = message;
statusToast.className = `backend-status-toast ${type}`;
statusToast.classList.add('show');
// Auto-hide success messages, keep warnings and errors longer
const timeout = type === 'success' ? 3000 : 8000;
setTimeout(() => {
statusToast.classList.remove('show');
}, timeout);
}
// Show global error message
showGlobalError(message) {
// Create error toast if it doesn't exist
let errorToast = document.getElementById('globalErrorToast');
if (!errorToast) {
errorToast = document.createElement('div');
errorToast.id = 'globalErrorToast';
errorToast.className = 'error-toast';
document.body.appendChild(errorToast);
}
errorToast.textContent = message;
errorToast.classList.add('show');
setTimeout(() => {
errorToast.classList.remove('show');
}, 5000);
}
// Clean up resources
cleanup() {
console.log('Cleaning up application resources...');
// Dispose all components
Object.values(this.components).forEach(component => {
if (component && typeof component.dispose === 'function') {
component.dispose();
}
});
// Disconnect all WebSocket connections
wsService.disconnectAll();
// Stop health monitoring
healthService.dispose();
}
// Public API
getComponent(name) {
return this.components[name];
}
isReady() {
return this.isInitialized;
}
}
// Initialize app when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
window.wifiDensePoseApp = new WiFiDensePoseApp();
window.wifiDensePoseApp.init();
});
// Export for testing
export { WiFiDensePoseApp };