Files
bookhoard/web/src/device-management.ts
T
john-okeefe 9947a12f09 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)
2026-03-08 01:14:35 -05:00

96 lines
2.6 KiB
TypeScript

import { Alpine } from "./alpine";
import { showToast } from "./toast";
// Device Management - Token copy and regeneration
// Procedural style with proper types (no OOP)
interface RegenerateTokenResponse {
message: string;
auth_token: string;
device: {
id: string;
device_name: string;
device_type: string;
auth_token: string;
sync_enabled: boolean;
auto_sync: boolean;
sync_frequency_minutes: number;
};
sync_urls?: {
sync_url?: string;
markup?: string;
bookmark?: string;
init?: string;
progress?: string;
metadata?: string;
bookmarks?: string;
};
}
// Copy sync URL or auth token to clipboard
function copyToClipboard(text: string, label: string): void {
navigator.clipboard
.writeText(text)
.then(() => {
showToast(`${label} copied to clipboard`, "success");
})
.catch((err: unknown) => {
console.error("Failed to copy:", err);
showToast("Failed to copy to clipboard", "error");
});
}
// Regenerate device token with confirmation
function regenerateDeviceToken(deviceId: string, event: Event): void {
const confirmation =
"⚠️ This will revoke current token and generate a new one.\n\n" +
"The old token will immediately stop working.\n\n" +
"You will need to update your device configuration with new token.\n\n" +
"Continue?";
if (!confirm(confirmation)) {
return;
}
const btn = event.target as HTMLButtonElement;
const originalText = btn.innerHTML;
btn.disabled = true;
btn.innerHTML = "🔄 Regenerating...";
const token = localStorage.getItem("token");
fetch(`/api/devices/${deviceId}/regenerate-token`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
})
.then((response: Response) => {
if (!response.ok) {
throw new Error("Failed to regenerate token");
}
return response.json() as Promise<RegenerateTokenResponse>;
})
.then((_data: RegenerateTokenResponse) => {
showToast(
"Token regenerated successfully - update your device config",
"success",
);
// Reload page to show new token
setTimeout(() => location.reload(), 1500);
})
.catch((error: unknown) => {
console.error("Error:", error);
showToast("Failed to regenerate token", "error");
if (btn) {
btn.disabled = false;
btn.innerHTML = originalText;
}
});
}
// Export functions for global access (called from template onclick attributes)
Alpine.global("devices", {
copyToClipboard,
regenerateDeviceToken,
});