Spaces:
Sleeping
Sleeping
Create static/js/dashboard.js
Browse files- static/js/dashboard.js +85 -0
static/js/dashboard.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// static/js/dashboard.js
|
| 2 |
+
|
| 3 |
+
async function initDashboard() {
|
| 4 |
+
refreshDashboardFiles();
|
| 5 |
+
simulateSystemStatus();
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
async function refreshDashboardFiles() {
|
| 9 |
+
if (!currentToken) return;
|
| 10 |
+
|
| 11 |
+
const list = document.getElementById('dash-recent-files');
|
| 12 |
+
if(!list) return;
|
| 13 |
+
|
| 14 |
+
try {
|
| 15 |
+
// Reuse the files API route we built for the editor!
|
| 16 |
+
const res = await fetch('/api/files', {
|
| 17 |
+
method: 'POST',
|
| 18 |
+
headers: {'Content-Type': 'application/json'},
|
| 19 |
+
body: JSON.stringify({token: currentToken})
|
| 20 |
+
});
|
| 21 |
+
const data = await res.json();
|
| 22 |
+
|
| 23 |
+
list.innerHTML = '';
|
| 24 |
+
|
| 25 |
+
if (data.files && data.files.length > 0) {
|
| 26 |
+
// Get up to 4 files to display in the widget
|
| 27 |
+
const recentFiles = data.files.slice(0, 4);
|
| 28 |
+
|
| 29 |
+
recentFiles.forEach(f => {
|
| 30 |
+
const ext = f.split('.').pop();
|
| 31 |
+
let icon = "📄";
|
| 32 |
+
if(ext === 'py') icon = "🐍";
|
| 33 |
+
else if(ext === 'js') icon = "⚡";
|
| 34 |
+
else if(ext === 'html') icon = "🌐";
|
| 35 |
+
else if(ext === 'css') icon = "🎨";
|
| 36 |
+
|
| 37 |
+
const html = `
|
| 38 |
+
<div class="recent-file-item" onclick="openFileFromDashboard('${f}')">
|
| 39 |
+
<div class="recent-file-icon">${icon}</div>
|
| 40 |
+
<div class="recent-file-info">
|
| 41 |
+
<h4>${f}</h4>
|
| 42 |
+
<p>Workspace File</p>
|
| 43 |
+
</div>
|
| 44 |
+
</div>
|
| 45 |
+
`;
|
| 46 |
+
list.insertAdjacentHTML('beforeend', html);
|
| 47 |
+
});
|
| 48 |
+
} else {
|
| 49 |
+
list.innerHTML = `<div style="color: var(--text-muted); font-size: 12px; text-align: center; padding: 20px 0;">No files found in workspace.</div>`;
|
| 50 |
+
}
|
| 51 |
+
} catch(e) {
|
| 52 |
+
console.error("Dashboard failed to load files", e);
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
// Navigates directly to the editor and opens the selected file
|
| 57 |
+
function openFileFromDashboard(filename) {
|
| 58 |
+
// Switch to Editor view
|
| 59 |
+
switchView('editor-view', document.querySelectorAll('.nav-item')[2]);
|
| 60 |
+
// Call the openFile function from editor.js
|
| 61 |
+
if (typeof openFile === "function") {
|
| 62 |
+
openFile(filename);
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// Simulates slight fluctuations in CPU/RAM to make the dashboard feel alive
|
| 67 |
+
function simulateSystemStatus() {
|
| 68 |
+
setInterval(() => {
|
| 69 |
+
const cpuEl = document.getElementById('stat-cpu');
|
| 70 |
+
const cpuBar = document.getElementById('bar-cpu');
|
| 71 |
+
if(cpuEl && cpuBar) {
|
| 72 |
+
const val = Math.floor(Math.random() * (25 - 5 + 1) + 5); // Random between 5-25%
|
| 73 |
+
cpuEl.innerText = `${val}%`;
|
| 74 |
+
cpuBar.style.width = `${val}%`;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
const ramEl = document.getElementById('stat-ram');
|
| 78 |
+
const ramBar = document.getElementById('bar-ram');
|
| 79 |
+
if(ramEl && ramBar) {
|
| 80 |
+
const val = Math.floor(Math.random() * (45 - 30 + 1) + 30); // Random between 30-45%
|
| 81 |
+
ramEl.innerText = `${val}%`;
|
| 82 |
+
ramBar.style.width = `${val}%`;
|
| 83 |
+
}
|
| 84 |
+
}, 3000); // Update every 3 seconds
|
| 85 |
+
}
|