-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
143 lines (124 loc) · 4.29 KB
/
Copy pathrenderer.js
File metadata and controls
143 lines (124 loc) · 4.29 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
const devicesContainer = document.getElementById('devices-container');
const noDevicesElement = document.getElementById('no-devices');
const deviceCountElement = document.getElementById('device-count');
const lastUpdateElement = document.getElementById('last-update');
const devices = new Map();
// Format time ago
function timeAgo(timestamp) {
const seconds = Math.floor((Date.now() - timestamp) / 1000);
if (seconds < 60) return `${seconds}s ago`;
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
return `${Math.floor(seconds / 86400)}d ago`;
}
// Format timestamp for display
function formatTimestamp(timestamp) {
return new Date(timestamp).toLocaleTimeString();
}
// Create or update device card
function updateDeviceCard(deviceId, deviceData) {
let card = document.getElementById(`device-${deviceId}`);
if (!card) {
card = document.createElement('div');
card.className = 'device-card';
card.id = `device-${deviceId}`;
devicesContainer.appendChild(card);
}
// Determine what fields to show
const excludeFields = ['id', 'ip', 'port', 'lastSeen'];
const deviceFields = Object.keys(deviceData)
.filter(key => !excludeFields.includes(key))
.map(key => ({
key,
value: deviceData[key]
}));
const deviceName = deviceData.name || deviceId;
const status = deviceData.status || 'online';
card.innerHTML = `
<div class="device-header">
<div class="device-title">
<div class="device-name">${deviceName}</div>
<div class="device-id">${deviceId}</div>
</div>
<div class="device-status ${status}">${status}</div>
</div>
<div class="device-info">
${deviceFields.map(field => `
<div class="info-row">
<span class="info-label">${field.key}</span>
<span class="info-value">${field.value}</span>
</div>
`).join('')}
</div>
<div class="device-footer">
<div class="last-seen">Last seen: ${timeAgo(deviceData.lastSeen)}</div>
<div>IP: ${deviceData.ip}</div>
</div>
`;
// Hide "no devices" message
noDevicesElement.style.display = 'none';
}
// Remove device card
function removeDeviceCard(deviceId) {
const card = document.getElementById(`device-${deviceId}`);
if (card) {
card.style.animation = 'slideOut 0.3s ease-out';
setTimeout(() => {
card.remove();
devices.delete(deviceId);
updateStats();
// Show "no devices" message if no devices left
if (devices.size === 0) {
noDevicesElement.style.display = 'block';
}
}, 300);
}
}
// Update statistics
function updateStats() {
deviceCountElement.textContent = devices.size;
lastUpdateElement.textContent = formatTimestamp(Date.now());
}
// Update timestamps periodically
function updateTimestamps() {
devices.forEach((device, deviceId) => {
const card = document.getElementById(`device-${deviceId}`);
if (card) {
const lastSeenElement = card.querySelector('.last-seen');
if (lastSeenElement) {
lastSeenElement.textContent = `Last seen: ${timeAgo(device.lastSeen)}`;
}
}
});
}
// Set up event listeners
window.electron.onDeviceUpdate((data) => {
const { deviceId, device } = data;
devices.set(deviceId, device);
updateDeviceCard(deviceId, device);
updateStats();
});
window.electron.onDeviceRemoved((deviceId) => {
removeDeviceCard(deviceId);
});
// Load initial devices
window.electron.getAllDevices().then((deviceList) => {
deviceList.forEach(({ deviceId, device }) => {
devices.set(deviceId, device);
updateDeviceCard(deviceId, device);
});
updateStats();
});
// Update timestamps every 5 seconds
setInterval(updateTimestamps, 5000);
// Add CSS for slide out animation
const style = document.createElement('style');
style.textContent = `
@keyframes slideOut {
to {
opacity: 0;
transform: translateX(100%);
}
}
`;
document.head.appendChild(style);