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 -30
View File
@@ -1,3 +1,5 @@
import { showToast } from "./toast";
async function refreshQueue(): Promise<void> {
const token = localStorage.getItem("token");
if (!token) return;
@@ -27,16 +29,12 @@ async function processPendingItems(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Processing queue items");
}
showToast("Processing queue items", "success");
refreshQueue();
}
} catch (error) {
console.error("Failed to process queue:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to process queue");
}
showToast("Failed to process queue", "error");
}
}
@@ -53,16 +51,12 @@ async function clearFailedItems(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Failed items cleared");
}
showToast("Failed items cleared", "success");
refreshQueue();
}
} catch (error) {
console.error("Failed to clear failed items:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to clear items");
}
showToast("Failed to clear items", "error");
}
}
@@ -79,16 +73,12 @@ async function clearAllItems(): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Queue cleared");
}
showToast("Queue cleared", "success");
refreshQueue();
}
} catch (error) {
console.error("Failed to clear queue:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to clear queue");
}
showToast("Failed to clear queue", "error");
}
}
@@ -103,16 +93,12 @@ async function retryQueueItem(itemId: string): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Item queued for retry");
}
showToast("Item queued for retry", "success");
refreshQueue();
}
} catch (error) {
console.error("Failed to retry item:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to retry item");
}
showToast("Failed to retry item", "error");
}
}
@@ -127,16 +113,12 @@ async function deleteQueueItem(itemId: string): Promise<void> {
});
if (response.ok) {
if ((window as any).showToast?.success) {
(window as any).showToast.success("Item deleted");
}
showToast("Item deleted", "success");
refreshQueue();
}
} catch (error) {
console.error("Failed to delete item:", error);
if ((window as any).showToast?.error) {
(window as any).showToast.error("Failed to delete item");
}
showToast("Failed to delete item", "error");
}
}