import { useState, useEffect } from "react"; import "./App.css"; import { Button } from "./components/ui/button"; import LoginPage from "./routes/Login"; import { getCurrentWindow } from "@tauri-apps/api/window"; import { LoaderCircleIcon } from "lucide-react"; import { AuthProvider, useAuth } from "./Auth"; import { initializeLocalStorage } from "./localstorage_ployfill"; import { Home } from "./routes/Home"; import { ThemeProvider } from "./theme-provider"; import { toast, Toaster } from "sonner"; import { ScrollArea } from "./components/ui/scroll-area"; import { check, Update } from "@tauri-apps/plugin-updater"; import { relaunch } from "@tauri-apps/plugin-process"; import { BackgroundBackupService, handleBackgroundBackup, } from "./lib/backgroundBackup"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Progress } from "./components/ui/progress"; import { MarkdownRenderer } from "./components/ui/markdown-renderer"; function AppContent() { const { isLoading, isAuthenticated, profile, client, login, logout, agent } = useAuth(); const appWindow = getCurrentWindow(); const [isLocalStorageReady, setIsLocalStorageReady] = useState(false); const [update, setUpdate] = useState(null); const [downloadProgress, setDownloadProgress] = useState(null); useEffect(() => { const initStorage = async () => { try { await initializeLocalStorage(); setIsLocalStorageReady(true); } catch (error) { console.error("Failed to initialize localStorage:", error); setIsLocalStorageReady(true); // Continue anyway } }; initStorage(); }, []); // Background backup service initialization useEffect(() => { if (!isAuthenticated || !agent) return; const backgroundService = BackgroundBackupService.getInstance(); backgroundService.initialize(); // Listen for background backup requests const handleBackgroundBackupRequest = () => { handleBackgroundBackup(agent); }; window.addEventListener( "background-backup-requested", handleBackgroundBackupRequest ); return () => { window.removeEventListener( "background-backup-requested", handleBackgroundBackupRequest ); backgroundService.stop(); }; }, [isAuthenticated, agent]); // Auto-backup functionality (for when app is open) // useEffect(() => { // if (!isAuthenticated || !agent) return; // let intervalId: ReturnType | null = null; // const checkAndPerformBackup = async () => { // try { // const lastBackupDate = await settingsManager.getLastBackupDate(); // const frequency = await settingsManager.getBackupFrequency(); // if (!lastBackupDate) { // // No previous backup, so we should do one // await performBackup(); // return; // } // const lastBackup = new Date(lastBackupDate); // const now = new Date(); // const timeDiff = now.getTime() - lastBackup.getTime(); // if (frequency === "daily") { // // Check if 24 hours have passed // const oneDay = 24 * 60 * 60 * 1000; // if (timeDiff >= oneDay) { // await performBackup(); // } // } else if (frequency === "weekly") { // // Check if 7 days have passed // const oneWeek = 7 * 24 * 60 * 60 * 1000; // if (timeDiff >= oneWeek) { // await performBackup(); // } // } // } catch (error) { // console.error("Error in automatic backup check:", error); // } // }; // const performBackup = async () => { // try { // console.log("Automatic backup due, starting backup..."); // const manager = new BackupAgent(agent); // await manager.startBackup(); // // Update the last backup date // await settingsManager.setLastBackupDate(new Date().toISOString()); // console.log("Automatic backup completed successfully"); // } catch (error) { // console.error("Automatic backup failed:", error); // } // }; // // Check immediately when authenticated // checkAndPerformBackup(); // // Set up interval to check every hour // intervalId = setInterval(checkAndPerformBackup, 60 * 60 * 1000); // return () => { // if (intervalId) { // clearInterval(intervalId); // } // }; // }, [isAuthenticated, agent]); useEffect(() => { const checkUpdates = async () => { const update = await check(); if (update) { console.log( `found update ${update.version} from ${update.date} with notes ${update.body}` ); setUpdate(update); } else { console.log("no updates"); } }; checkUpdates(); const unlistenVisible = appWindow.listen("tauri://focus", () => { checkUpdates(); }); return () => { // Cleanup listeners unlistenVisible.then((unlisten) => unlisten()); }; }, []); return ( <>
{ if (it == false) setUpdate(null); }} > {/* Open */} New update available ({update?.currentVersion} ➜{" "} {update?.version}) {downloadProgress == null ? ( <> ) : ( )} {isLoading || !isLocalStorageReady ? (
) : isAuthenticated ? ( ) : ( )}
); } function App() { return ( ); } export default App;