import { useEffect, useState, useCallback } from "react"; import { checkForAppUpdate, installAppUpdate, skipUpdateVersion, isTauri } from "../utils/updater.js"; export default function UpdateNotifier() { const [update, setUpdate] = useState(null); const [state, setState] = useState("idle"); const [downloaded, setDownloaded] = useState(0); const [total, setTotal] = useState(0); const [error, setError] = useState(null); const runCheck = useCallback(async ({ silent }) => { if (!isTauri()) return; try { setState("checking"); setError(null); const res = await checkForAppUpdate({ respectSkip: silent }); if (!res.available) { setUpdate(null); setState("idle"); return; } setUpdate(res.update); setState("available"); } catch (e) { console.error("Update-Check fehlgeschlagen:", e); setError(String(e?.message || e)); setState("idle"); } }, []); useEffect(() => { const t = setTimeout(() => runCheck({ silent: true }), 1500); return () => clearTimeout(t); }, [runCheck]); // Settings → "Nach Updates suchen" feuert dieses Event; wir lassen das Modal aufpoppen, // falls etwas gefunden wird (Skip-Flag wird dabei ignoriert). useEffect(() => { const handler = () => runCheck({ silent: false }); window.addEventListener("rapport:check-update", handler); return () => window.removeEventListener("rapport:check-update", handler); }, [runCheck]); const install = async () => { if (!update) return; try { setState("downloading"); setDownloaded(0); setTotal(0); await installAppUpdate(update, (event) => { if (event.event === "Started") { setTotal(event.data.contentLength || 0); } else if (event.event === "Progress") { setDownloaded((d) => d + (event.data.chunkLength || 0)); } else if (event.event === "Finished") { setState("installing"); } }); } catch (e) { console.error("Update-Installation fehlgeschlagen:", e); setError(String(e?.message || e)); setState("available"); } }; const skipVersion = () => { skipUpdateVersion(update?.version); setUpdate(null); setState("idle"); }; const later = () => { setUpdate(null); setState("idle"); }; if (!isTauri() || !update || state === "idle" || state === "checking") return null; const isBusy = state === "downloading" || state === "installing"; const pct = total > 0 ? Math.min(100, Math.round((downloaded / total) * 100)) : null; return (
UPDATE VERFÜGBAR
Rapport {update.version}
{update.currentVersion && (
von {update.currentVersion}
)}
{update.body && (
{update.body}
)} {error && (
{error}
)} {isBusy && (
{state === "downloading" ? (pct !== null ? `Wird heruntergeladen … ${pct}%` : "Wird heruntergeladen …") : "Wird installiert …"}
)}
); }