refactor: remove inline WebSocket code from templates

collections.templ:
- Removed ~75 lines of inline WebSocket JS
- Added initializeCollectionWebSocket using websocket.ts utility
- Updated template to use x-init for WebSocket init

admin.templ:
- Removed ~55 lines of inline WebSocket JS
- Added initializeScanWebSocket using websocket.ts utility
- Updated template to use x-init for WebSocket init

Both now use the shared websocket.ts createWebSocket function
This commit is contained in:
2026-03-13 12:51:11 -04:00
parent 7068fabbee
commit 41e7445524
6 changed files with 110 additions and 155 deletions
+1 -58
View File
@@ -9,65 +9,8 @@ templ Admin(user User) {
<script src="/static/htmx.min.js"></script>
<script src="/static/main.js" defer></script>
<link href="/static/style.css" rel="stylesheet"/>
<script>
let scanWs = null;
function connectScanWebSocket() {
const protocol =
window.location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${protocol}//${window.location.host}/ws/sync?token={ user.Token }`;
scanWs = new WebSocket(wsUrl);
scanWs.onmessage = function (event) {
try {
const message = JSON.parse(event.data);
switch (message.type) {
case "scan_progress":
updateScanProgress(message.data);
break;
case "scan_complete":
showScanComplete(message.data);
stopScanStatusPolling();
break;
case "scan_error":
showScanError(message.data);
break;
}
} catch (error) {
console.error("Failed to parse WebSocket message:", error);
}
};
scanWs.onerror = function (error) {
console.error("WebSocket error:", error);
};
}
function updateScanProgress(data) {
const progressBar = document.getElementById("scan-progress-bar");
if (progressBar) {
progressBar.style.width = data.progress * 100 + "%";
}
const progressText = document.getElementById("scan-progress-text");
if (progressText) {
progressText.textContent = `${data.files_scanned} files scanned (${data.new_items} new)`;
}
}
function showScanComplete(data) {
console.log("Scan complete:", data);
}
function showScanError(data) {
console.error("Scan error:", data);
}
document.addEventListener("DOMContentLoaded", function () {
connectScanWebSocket();
});
</script>
</head>
<body x-data="admin" x-init="loadWatchStatus()" class="theme-tokyo-night" x-data="admin">
<body x-data="admin" x-init="loadWatchStatus(); initializeScanWebSocket()" class="theme-tokyo-night">
@Header(user, "/admin")
<div class="flex min-h-screen" style="background-color: var(--bg-primary)">
@AdminSidebar(user, "/admin")