import {useEffect, useState} from 'react'; import {check, Update} from '@tauri-apps/plugin-updater'; import {relaunch} from '@tauri-apps/plugin-process'; export interface UpdateState { available: boolean; version: string; notes: string; downloading: boolean; progress: number; install: () => Promise; dismiss: () => void; } export function useAutoUpdate(): UpdateState { const [available, setAvailable] = useState(false); const [version, setVersion] = useState(''); const [notes, setNotes] = useState(''); const [downloading, setDownloading] = useState(false); const [progress, setProgress] = useState(0); const [update, setUpdate] = useState(null); useEffect(() => { let cancelled = false; async function checkForUpdate() { try { const result = await check(); if (cancelled || !result) return; setUpdate(result); setAvailable(true); setVersion(result.version); setNotes(result.body ?? ''); } catch (_) { // silently fail — no update or offline } } checkForUpdate(); return () => { cancelled = true; }; }, []); async function install() { if (!update) return; setDownloading(true); try { await update.downloadAndInstall((event) => { if (event.event === 'Started' && event.data.contentLength) { setProgress(0); } else if (event.event === 'Progress') { setProgress((prev) => prev + event.data.chunkLength); } else if (event.event === 'Finished') { setProgress(100); } }); await relaunch(); } catch (_) { setDownloading(false); } } function dismiss() { setAvailable(false); } return {available, version, notes, downloading, progress, install, dismiss}; }