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
+12 -32
View File
@@ -1,3 +1,5 @@
import { showToast } from "./toast";
async function refreshConflicts(): Promise<void> {
const token = localStorage.getItem("token");
if (!token) return;
@@ -36,23 +38,15 @@ async function resolveConflict(
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Conflict resolved");
}
showToast("Conflict resolved", "success");
refreshConflicts();
} else {
const error = await response.json();
if ((window as any).showToast?.error) {
(window as any).showToast.error(
error.error || "Failed to resolve conflict",
);
}
showToast(error.error || "Failed to resolve conflict", "error");
}
} catch (error) {
console.error("Failed to resolve conflict:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to resolve conflict");
}
showToast("Failed to resolve conflict", "error");
}
}
@@ -75,16 +69,12 @@ async function bulkResolve(
if (response.ok) {
const data: BulkResolveResponse = await response.json();
if ((window as any).showToast?.success) {
(window as any).showToast.success(`Resolved ${data.success} conflicts`);
}
showToast(`Resolved ${data.success} conflicts`, "success");
refreshConflicts();
}
} catch (error) {
console.error("Failed to bulk resolve:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to bulk resolve conflicts");
}
showToast("Failed to bulk resolve conflicts", "error");
}
}
@@ -103,16 +93,12 @@ async function bulkDismiss(conflictIds: string[]): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Conflicts dismissed");
}
showToast("Conflicts dismissed", "success");
refreshConflicts();
}
} catch (error) {
console.error("Failed to dismiss conflicts:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to dismiss conflicts");
}
showToast("Failed to dismiss conflicts", "error");
}
}
@@ -127,16 +113,12 @@ async function dismissAllResolved(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Resolved conflicts dismissed");
}
showToast("Resolved conflicts dismissed", "success");
refreshConflicts();
}
} catch (error) {
console.error("Failed to dismiss resolved:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to dismiss resolved conflicts");
}
showToast("Failed to dismiss resolved conflicts", "error");
}
}
@@ -214,9 +196,7 @@ function handleResolveSubmit(event: Event): void {
)?.value;
if (!conflictId || !winner) {
if ((window as any).showToast?.error) {
(window as any).showToast.error("Please select a winner");
}
showToast("Please select a winner", "error");
return;
}