okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d51d71e7c
main
1import { useEffect , useState } from 'react' ; 2import { useStore } from './store' ; 3import { useNative } from './native' ; 4import { appVersionLabel } from './app-version' ; 5import { shownEngineVersion } from './engine-version' ; 6import { readUpdateCache , saveUpdateCache , type InstalledVersions } from './update-cache' ; 7import type { UpdateCheckResult } from './types' ; 8 9/** The installed versions the cached check belongs to. Same values the Settings version rows show. */ 10export function useInstalledVersions () :InstalledVersions { 11const native = useNative (); 12const engineVersion = useStore ( s => s . engine ?. engine . version ); 13const appVersion = appVersionLabel ( useStore ( s => s . setup ?. app . version ), native ?. version ); 14return { appVersion :appVersion ?? null , engineVersion :shownEngineVersion ( native , engineVersion ) ?? null , controlVersion :native ?. version ?? null }; 15} 16 17/** The one place the app asks the host to check for updates. Called only from a button press. */ 18export async function fetchUpdateCheck ( signal ?:AbortSignal ) :Promise < UpdateCheckResult > { 19const response = await fetch ( '/api/updates/check' , { signal}); 20if ( ! response . ok ) throw new Error ( 'Could not check for updates. Try again.' ); 21const data = await response . json () as UpdateCheckResult ; 22if ( ! data . strata ?. status || ! data . engine ?. status || ! data . checkedAt ) throw new Error ( 'The update service returned an incomplete result.' ); 23return data ; 24} 25 26const EVENT = 'strata:update-check' ; 27 28/** Remember a finished check and tell every Settings panel that shows it. */ 29export function publishUpdateCheck ( result :UpdateCheckResult , now :InstalledVersions ) { 30saveUpdateCache ( result , now ); 31try { window . dispatchEvent ( new Event ( EVENT )); } catch { /* No window, nothing to refresh. */ } 32} 33 34/** The last known check result, read from this computer. Never touches the network. */ 35export function useLastUpdateCheck ( now :InstalledVersions ) :UpdateCheckResult | null { 36const [, tick ] = useState ( 0 ); 37useEffect (() => { 38const refresh = () => tick ( n => n + 1 ); 39window . addEventListener ( EVENT , refresh ); 40return () => window . removeEventListener ( EVENT , refresh ); 41}, []); 42return readUpdateCache ( now ); 43}