/** * Application updates through Tauri's signed updater. The feed * (`updates/latest.json`) lists a build per platform, so the same flow serves * Windows, macOS and Linux. */ import { create } from 'zustand'; import { isNative } from '../platform/native'; import { toast } from '../ui/overlays'; interface UpdaterState { checking: boolean; installing: boolean; progress: number; checkedAt: number; error: string; available: { version: string; notes: string } | null; } export const useUpdater = create(() => ({ checking: false, installing: false, progress: 0, checkedAt: 0, error: '', available: null, })); let pending: import('@tauri-apps/plugin-updater').Update | null = null; export async function checkForUpdate(manual = false) { if (!isNative || useUpdater.getState().checking) return; useUpdater.setState({ checking: true, error: '' }); try { const { check } = await import('@tauri-apps/plugin-updater'); const update = await check({ timeout: 15000 }); pending = update; useUpdater.setState({ available: update ? { version: update.version, notes: update.body ?? '' } : null, checkedAt: Date.now() }); if (update && !manual) toast(`Kaleido ${update.version} is available`, 'info', { label: 'Install', onClick: installUpdate }); } catch (e: any) { if (manual) useUpdater.setState({ error: `Couldn’t check for updates: ${e?.message ?? e}` }); } finally { useUpdater.setState({ checking: false }); } } export async function installUpdate() { if (!pending) return; useUpdater.setState({ installing: true, progress: 0 }); try { let total = 0; let received = 0; await pending.downloadAndInstall((event) => { if (event.event === 'Started') total = event.data.contentLength ?? 0; else if (event.event === 'Progress') { received += event.data.chunkLength; if (total) useUpdater.setState({ progress: received / total }); } }); const { relaunch } = await import('@tauri-apps/plugin-process'); await relaunch(); } catch (e: any) { useUpdater.setState({ installing: false, error: `The update couldn’t be installed: ${e?.message ?? e}` }); } }