import { useEffect, useRef, useState } from 'react' import type { AppUpdateInfo, Task } from '../types' import { api } from '../wails' export type UpdatePhase = 'none' | 'available' | 'downloading' | 'ready' | 'failed' const CHECK_EVERY_MS = 6 * 60 * 60 * 1000 const DISMISSED_KEY = 'reims-go-update-dismissed' interface Options { /** Checks start once the app is up. */ ready: boolean tasks: Record runningVMCount: number onError: (e: unknown) => void } /** * The in-app update: one task per release, whose state drives the sidebar * capsule and the About page alike. */ export function useAppUpdate({ ready, tasks, runningVMCount, onError }: Options) { const [info, setInfo] = useState(null) const [dismissed, setDismissed] = useState(() => { try { return localStorage.getItem(DISMISSED_KEY) ?? '' } catch { return '' } }) const [confirming, setConfirming] = useState(false) const [installing, setInstalling] = useState(false) const reqSeqRef = useRef(0) // Check once the app is up, then every 6 hours, so the sidebar's update // button shows up without opening the About page. useEffect(() => { if (!ready) return const check = () => { const seq = ++reqSeqRef.current api.CheckAppUpdate(false) .then((next) => { if (reqSeqRef.current === seq) setInfo(next) }) .catch((e) => console.warn('[app] CheckAppUpdate failed:', e)) } check() const id = window.setInterval(check, CHECK_EVERY_MS) return () => window.clearInterval(id) }, [ready]) const task = info?.has_update ? Object.values(tasks) .filter((x) => x.kind === 'app-update' && x.subject === info.latest) .sort((a, b) => b.updated_at - a.updated_at)[0] ?? null : null const phase: UpdatePhase = !info?.has_update ? 'none' : task?.status === 'running' ? 'downloading' : task?.status === 'done' ? 'ready' : task?.status === 'failed' ? 'failed' : 'available' const dismiss = () => { if (!info) return setDismissed(info.latest) try { localStorage.setItem(DISMISSED_KEY, info.latest) } catch (e) { console.warn('[app] persist dismissed update failed:', e) } } /** Forgets a dismissed release and asks again, e.g. after the update channel changed. */ const recheck = async (force = false) => { const seq = ++reqSeqRef.current setDismissed('') try { localStorage.removeItem(DISMISSED_KEY) } catch (e) { console.warn('[app] clear dismissed update failed:', e) } try { const next = await api.CheckAppUpdate(force) if (reqSeqRef.current === seq) setInfo(next) } catch (e) { if (reqSeqRef.current === seq) { setInfo(null) throw e } } } const download = () => { api.DownloadAppUpdate().catch(onError) } const confirmInstall = () => { setConfirming(false) setInstalling(true) api.InstallAppUpdate().catch((e) => { setInstalling(false) onError(e) }) } // Restarting leaves VMs running but pauses the host-guest bridges: say so first. const install = () => { if (runningVMCount > 0) setConfirming(true); else confirmInstall() } return { info, setInfo, task, phase, installing, confirming, shown: phase !== 'none' && !(phase === 'available' && dismissed === info?.latest), dismiss, recheck, download, install, confirmInstall, cancelInstall: () => setConfirming(false), } }