One-click backups for AT Protocol
1import { invoke } from "@tauri-apps/api/core";
2import { listen } from "@tauri-apps/api/event";
3import { BackupAgent } from "./backup";
4import { settingsManager } from "./settings";
5import { toast } from "sonner";
6import { info } from "@tauri-apps/plugin-log";
7
8export class BackgroundBackupService {
9 private static instance: BackgroundBackupService;
10 private isInitialized = false;
11
12 private constructor() {}
13
14 static getInstance(): BackgroundBackupService {
15 if (!BackgroundBackupService.instance) {
16 BackgroundBackupService.instance = new BackgroundBackupService();
17 }
18 return BackgroundBackupService.instance;
19 }
20
21 async initialize(): Promise<void> {
22 if (this.isInitialized) return;
23
24 // Listen for backup events from the background scheduler
25 await listen("perform-backup", async () => {
26 console.log("Background backup event received");
27 info("Background backup event received");
28 await this.performBackgroundBackup();
29 });
30
31 // const window = getCurrentWindow();
32 // await window.listen("perform-backup", async () => {
33 // console.log("Background backup event received");
34 // await this.performBackgroundBackup();
35 // });
36
37 console.log("Background backup service initialized");
38
39 //Start the background scheduler
40 try {
41 await invoke("start_background_scheduler");
42 console.log("Background backup scheduler started");
43 } catch (error) {
44 console.error("Failed to start background scheduler:", error);
45 }
46
47 this.isInitialized = true;
48 }
49
50 private async performBackgroundBackup(): Promise<void> {
51 try {
52 // Get the agent from the auth context
53 // This is a bit tricky since we need to access the agent from the React context
54 // For now, we'll emit an event that the main app can listen to
55 window.dispatchEvent(new CustomEvent("background-backup-requested"));
56
57 console.log("Background backup request dispatched");
58 } catch (error) {
59 console.error("Background backup failed:", error);
60 }
61 }
62
63 async stop(): Promise<void> {
64 try {
65 await invoke("stop_background_scheduler");
66 console.log("Background backup scheduler stopped");
67 } catch (error) {
68 console.error("Failed to stop background scheduler:", error);
69 }
70 }
71}
72
73// Export a function to handle background backup requests
74export async function handleBackgroundBackup(agent: any): Promise<void> {
75 try {
76 console.log("Performing background backup...");
77
78 const manager = new BackupAgent(agent);
79 await manager.startBackup();
80
81 // Update the last backup date
82 await settingsManager.setLastBackupDate(new Date().toISOString());
83
84 console.log("Background backup completed successfully");
85
86 // Show a notification (if the app is minimized, this might not be visible)
87 toast("Automatic backup completed", {
88 description: "Your data has been backed up automatically",
89 });
90 } catch (error) {
91 console.error("Background backup failed:", error);
92 toast("Automatic backup failed", {
93 description: "Check the console for details",
94 });
95 }
96}