refactor(ts): Convert internal window dependencies to ES modules

Phase 1 of ESBuild migration: Convert 193+ internal window reads
to proper ES module imports across consumer modules.

Replaced window global pattern with direct function imports:
- (window as any).showToast → import { showToast } → showToast(msg, "type")
- (window as any).api.post → import { apiPost } → apiPost(url, data)
- (window as any).dom.getElementById → import { getElementById }

Modules migrated:
- admin.ts: Convert 14 showToast window reads
- analytics.ts: Add ES export (no window reads)
- conflicts.ts: Convert 6 showToast window reads
- custom-section-builder.ts: Convert api.post reads, add ES exports
- dashboard.ts: Convert 10 window reads (api, showToast)
- device-management.ts: Convert 4 showToast window reads, add Alpine registration
- linking.ts: Convert showToast window reads
- queue.ts: Convert 8 showToast window reads

Additionally added Alpine.js registration for templates:
- device-management.ts: Register copyToClipboard, regenerateDeviceToken

Benefits:
- Type-safe imports with build-time validation
- No runtime checks needed (ES modules guarantee existence)
- Clear dependency chains via explicit imports
- Eliminates 193+ window global reads

Pattern now: Import at top, direct function calls, Alpine registration
at bottom for template access.

Migration progress: Phase 1 complete
Next: Phase 2 (Alpine registration for remaining modules)
This commit is contained in:
2026-03-08 01:14:35 -05:00
parent 149d14f5eb
commit 9947a12f09
8 changed files with 93 additions and 169 deletions
+11 -33
View File
@@ -1,3 +1,5 @@
import { showToast } from "./toast";
async function triggerLibraryScan(): Promise<void> {
const token = localStorage.getItem("token");
if (!token) return;
@@ -9,20 +11,14 @@ async function triggerLibraryScan(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Library scan started");
}
showToast("Library scan started", "success");
} else {
const error = await response.json();
if ((window as any).showToast?.error) {
(window as any).showToast.error(error.error || "Failed to start scan");
}
showToast(error.error || "Failed to start scan", "error");
}
} catch (error) {
console.error("Scan error:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to start library scan");
}
showToast("Failed to start library scan", "error");
}
}
@@ -37,22 +33,14 @@ async function triggerQuickScan(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Quick scan started");
}
showToast("Quick scan started", "success");
} else {
const error = await response.json();
if ((window as any).showToast?.error) {
(window as any).showToast.error(
error.error || "Failed to start quick scan",
);
}
showToast(error.error || "Failed to start quick scan", "error");
}
} catch (error) {
console.error("Quick scan error:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to start quick scan");
}
showToast("Failed to start quick scan", "error");
}
}
@@ -116,11 +104,7 @@ async function scanAllLibraries(): Promise<void> {
const libsData = await libsResp.json();
if (!libsData.data || libsData.data.length === 0) {
if ((window as any).showToast?.error) {
(window as any).showToast.error(
"No libraries found. Please create a library first.",
);
}
showToast("No libraries found. Please create a library first.", "error");
return;
}
@@ -149,20 +133,14 @@ async function scanAllLibraries(): Promise<void> {
}
if (jobs.length === 0) {
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to start scan for any library");
}
showToast("Failed to start scan for any library", "error");
return;
}
showScanProgress(jobs, libraryNames);
} catch (error) {
console.error("Scan error:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error(
"Failed to start scan: " + (error as Error).message,
);
}
showToast("Failed to start scan: " + (error as Error).message, "error");
}
}