-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (123 loc) · 5.5 KB
/
script.js
File metadata and controls
134 lines (123 loc) · 5.5 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
document.addEventListener('DOMContentLoaded', () => {
// --- All Elements ---
const themeToggleButton = document.getElementById('theme-toggle');
const sunIcon = document.getElementById('theme-icon-sun');
const moonIcon = document.getElementById('theme-icon-moon');
const navLinks = document.querySelectorAll('.nav-link');
const pages = document.querySelectorAll('.page-content');
const connectButton = document.getElementById('connect-button');
const statusIndicator = document.getElementById('status-indicator');
const statusText = document.getElementById('status-text');
const deviceList = document.getElementById('device-list');
// --- State & Config ---
const SERVER_URL = "https://securewipe-backend.onrender.com";
const POLLING_INTERVAL = 3000;
let isConnected = false;
let pollingId = null;
// --- Page Navigation Logic ---
const showPage = (pageId) => {
pages.forEach(page => page.classList.toggle('page-hidden', page.id !== `page-${pageId}`));
window.scrollTo(0, 0);
if (pageId !== 'dashboard' && isConnected) {
stopPolling();
}
};
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
showPage(link.dataset.page);
});
});
// --- Theme Toggle Logic (Defaults to LIGHT) ---
const applyTheme = (isDark) => {
if (isDark) {
document.body.classList.remove('light-mode');
sunIcon.classList.add('hidden');
moonIcon.classList.remove('hidden');
localStorage.setItem('theme', 'dark');
} else {
document.body.classList.add('light-mode');
sunIcon.classList.remove('hidden');
moonIcon.classList.add('hidden');
localStorage.setItem('theme', 'light');
}
};
themeToggleButton.addEventListener('click', () => applyTheme(document.body.classList.contains('light-mode')));
const savedTheme = localStorage.getItem('theme');
applyTheme(savedTheme === 'dark'); // Load saved theme, or default to light if 'dark' is not saved
// --- Your Dashboard Functions ---
async function fetchDevices() {
try {
const response = await fetch(`${SERVER_URL}/api/devices`);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const devices = await response.json();
updateDeviceList(devices);
updateConnectionStatus(true);
} catch (error) {
console.error("Could not fetch devices:", error);
deviceList.innerHTML = `<p class="loading" style="color: #ef4444;">Connection to backend failed. Is the server running?</p>`;
stopPolling();
}
}
function updateDeviceList(devices) {
if (Object.keys(devices).length === 0) {
deviceList.innerHTML = `<p class="loading">Connected. Waiting for agents to register...</p>`;
return;
}
deviceList.innerHTML = '';
for (const machineId in devices) {
const device = devices[machineId];
let statusClass = (device.status || '').toLowerCase().split(':')[0]; // Get base status like 'completed'
const deviceCard = document.createElement('div');
// ADDED dashboard-card class for hover effect
deviceCard.className = 'device-card dashboard-card';
deviceCard.innerHTML = `
<div class="device-info">
<h3>${machineId}</h3>
<p class="text-sm">Status: <span class="status ${statusClass}">${device.status || 'Unknown'}</span></p>
<p class="text-xs">Last Seen: ${new Date(device.last_seen).toLocaleString()}</p>
</div>
<button class="wipe-button" onclick="triggerWipe('${machineId}')">WIPE</button>`;
deviceList.appendChild(deviceCard);
}
}
window.triggerWipe = async function(machineId) {
if (!isConnected) {
alert("You must be connected to the server to issue a wipe command.");
return;
}
if (confirm(`ARE YOU ABSOLUTELY SURE?\n\nThis will issue a WIPE command to: ${machineId}`)) {
try {
await fetch(`${SERVER_URL}/api/wipe/${machineId}`, { method: 'POST' });
alert("Wipe command issued successfully!");
fetchDevices();
} catch (error) {
alert("Error: Could not issue wipe command.");
}
}
}
function startPolling() {
isConnected = true;
updateConnectionStatus(true, "Connecting...");
fetchDevices();
pollingId = setInterval(fetchDevices, POLLING_INTERVAL);
}
function stopPolling() {
isConnected = false;
clearInterval(pollingId);
updateConnectionStatus(false);
deviceList.innerHTML = `<p class="loading">Press "Connect" to fetch agent status.</p>`;
}
function updateConnectionStatus(connected, text = null) {
statusText.textContent = text || (connected ? 'Connected' : 'Disconnected');
statusIndicator.className = `status-light ${connected ? 'connected' : 'disconnected'}`;
connectButton.textContent = connected ? 'Disconnect' : 'Connect';
connectButton.classList.toggle('active', connected);
}
connectButton.addEventListener('click', () => {
if (isConnected) stopPolling();
else startPolling();
});
// --- Initial Page Load ---
showPage('home');
});