okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e39a52e33
main
1import { localeTag } from '../lib/i18n' ; 2import { tr } from '../lib/i18n' ; 3import { useEffect , useRef , useState } from 'react' ; 4import { Btn , Dialog , Icon } from './ui' ; 5import { useStore } from '../lib/store' ; 6import type { SoftwareUpdate , UpdateCheckResult } from '../lib/types' ; 7import { nativeAction , useNative , type NativeJob } from '../lib/native' ; 8import { NativeProgress } from './NativeModels' ; 9import { EngineUpdateDialog } from './EngineUpdateDialog' ; 10import { currentUpdateFailure , jobErrorText , updateFinishedAfter } from '../lib/native-job-diagnostics' ; 11import { appVersionLabel } from '../lib/app-version' ; 12import { fetchUpdateCheck , publishUpdateCheck , useInstalledVersions , useLastUpdateCheck } from '../lib/update-check' ; 13import { shownEngineVersion } from '../lib/engine-version' ; 14/** A check from an earlier day shows its date too, because the result is kept between visits. */ 15function checkedLabel ( at :string ) { 16const when = new Date ( at ), sameDay = when . toDateString () === new Date (). toDateString (); 17return sameDay ?when . toLocaleTimeString ( localeTag (), { hour :'2-digit' , minute :'2-digit' }) :when . toLocaleString ( localeTag (), { dateStyle :'medium' , timeStyle :'short' }); 18} 19/** Installed versions are real host values. Installation needs a host updater. */ 20export function SettingsUpdates () { 21const native = useNative (); 22const [ confirm , setConfirm ] = useState ( '' ); 23const appVersion = appVersionLabel ( useStore ( s => s . setup ?. app . version ), native ?. version ); 24const engineVersion = useStore ( s => s . engine ?. engine . version ); 25const [ checking , setChecking ] = useState ( false ); 26// Opening Settings shows the last check kept on this computer. The network is touched only when "Check for updates" is pressed. 27const versions = useInstalledVersions (); 28const last = useLastUpdateCheck ( versions ); 29const [ fresh , setFresh ] = useState < UpdateCheckResult | null >( null ); 30const [ error , setError ] = useState ( '' ); 31const request = useRef < AbortController | null >( null ); 32useEffect (() => () => request . current ?. abort (), []); 33const active = native ?. jobs . find ( j => j . state === 'running' ); 34const result = checking || error ?null :( fresh ?? last ); 35// An update that finished after the check shown makes it out of date: check again, once per update (lane 3 finding 6, 11-09). 36const finished = updateFinishedAfter ( native ?. jobs ?? [], result ?. checkedAt ), rechecked = useRef ( '' ); 37useEffect (() => { if ( finished && ! checking && rechecked . current !== finished ) { rechecked . current = finished ; void check (); } }, [ finished , checking ]); 38// Update engine with a model loaded: its own dialog (warns naming the model, stops it with the person's yes, offers to start it again). 39const withModel = ( native ?. instances . length ?? 0 ) > 0 && !! native ?. capabilities . componentUpdates ; 40async function operate ( kind :string ) { 41setError ( '' ); 42try { 43await nativeAction ( 'operations' , { kind}); 44setConfirm ( '' ); 45} 46catch ( e ) { 47setError ( e instanceof Error ?e . message :String ( e )); 48} 49} 50async function check () { 51request . current ?. abort (); 52const controller = new AbortController (); 53request . current = controller ; 54setChecking ( true ); 55setError ( '' ); 56setFresh ( null ); 57try { 58const data = await fetchUpdateCheck ( controller . signal ); 59if ( ! controller . signal . aborted ) { 60setFresh ( data ); 61publishUpdateCheck ( data , versions ); 62} 63} 64catch ( e ) { 65if ( ! controller . signal . aborted ) 66setError ( e instanceof Error ?e . message :'Could not check for updates.' ); 67} 68finally { 69if ( ! controller . signal . aborted ) 70setChecking ( false ); 71} 72} 73function versionRow ( name :string , version :string | undefined , update ?:SoftwareUpdate ) { 74const labels :Record < SoftwareUpdate [ 'status' ], string > = { available :'Update available' , current :'Up to date' , unavailable :'Check unavailable' , error :'Check failed' , 'no-releases' :'No release yet' }; 75return < div className = "settings-version" data-update = { update ?. status }> 76< Icon name = { name === 'Strata' ?'layers' :'engine' } size = { 20 }/> 77< span >< strong >{ tr ( name )}</ strong >< small >{ tr ( version ?? 'Version unavailable' )}</ small >{ tr ( ! checking && update ?< small className = "settings-version__message" >{ tr ( update . message )}</ small > :null )}{ tr ( ! checking && name === 'Strata' && update ?. details ?< details className = "native-settings-diagnostic" >< summary className = "small" >{ tr ( "Details" )}</ summary >< p className = "small muted" >{ update . details }</ p ></ details > :null )}</ span > 78{ tr ( ! checking && update ?. status === 'available' ?( native ?. available ?< Btn size = "sm" variant = "primary" disabled = { !! active || ( name === 'Engine' && ! native . installed ?. models . length && ! native . capabilities . componentUpdates )} onClick = {() => setConfirm ( name === 'Strata' ?'update-strata' :'update-engine' )}>{ tr ( "Update available" )}</ Btn > :update . releaseUrl ?< a className = "st-btn st-btn--secondary st-btn--sm" href = { update . releaseUrl } target = "_blank" rel = "noreferrer" title = { tr ( "Open the release page to update this installation" )}>{ tr ( "Update available" )}< Icon name = "download" size = { 15 }/></ a > :< span className = "settings-version__label" >{ tr ( labels [ update . status ])}</ span >) :< span className = "settings-version__label" >{ tr ( checking ?'Checking…' :update ?labels [ update . status ] :version === 'Not installed' ?'Not installed' :'Installed' )}</ span >)} 79</ div >; 80} 81return <> 82< div className = "settings-versions" aria-busy = { checking }> 83{ tr ( versionRow ( 'Strata' , appVersion , result ?. strata ))} 84{ tr ( versionRow ( 'Engine' , native ?. available ?( native . installed ?. engine ?. version ?? 'Not installed' ) :shownEngineVersion ( native , engineVersion ), result ?. engine ))} 85< div className = "settings-update-actions" >< Btn size = "sm" icon = "refresh" disabled = { checking } className = { checking ?'settings-update-checking' :undefined } onClick = {() => void check ()}>{ tr ( checking ?'Checking…' :error || result ?. strata . status === 'error' ?'Try again' :result ?'Check again' :'Check for updates' )}</ Btn ></ div > 86< div className = "settings-update-inline" role = "status" aria-live = "polite" >{ tr ( checking ?'Checking for new versions…' :error ?< span className = "inline-error" >{ tr ( error )}</ span > :result ?<>< span className = "sr-only" >{ tr ( result . strata . message )} { tr ( result . engine . message )} </ span >{ tr ( "Checked at " )}{ tr ( checkedLabel ( result . checkedAt ))}</> :null )}</ div > 87</ div > 88{ tr ( active ?< NativeProgress job = { active }/> :null )}{ tr ([ currentUpdateFailure ( native ?. jobs ?? [])]. filter (( j ) :j isNativeJob => !! j ). map ( j => < p role = "alert" key = { j . id } className = "small inline-error" >{ tr ( jobErrorText ( j , native ?. jobs ?? []))}</ p >))} 89< EngineUpdateDialog open = { confirm === 'update-engine' && withModel } onClose = {() => setConfirm ( '' )}/> 90< Dialog open = { !! confirm && ! ( confirm === 'update-engine' && withModel )} compact title = { tr ( confirm === 'update-strata' ?'Update Strata?' :'Update engine?' )} onClose = {() => setConfirm ( '' )} actions = {<>< Btn onClick = {() => setConfirm ( '' )}>{ tr ( "Cancel" )}</ Btn >< Btn variant = "primary" disabled = { !! active || ( native ?. instances . length ?? 0 ) > 0 } onClick = {() => void operate ( confirm )}>{ tr ( "Update" )}</ Btn ></>}>< p >{ tr ( "The native installer updates the installation. Model files and settings are kept. Unload native models before updating." )}</ p >{ tr (( native ?. instances . length ?? 0 ) > 0 ?< p className = "small muted" >{ tr ( "Open Models and unload the running native models first." )}</ p > :null )}</ Dialog > 91</>; 92} 93 94/** "Strata 0.2.15 · Engine 0.1.60": the app's own version first, the engine's beside it (About). */ 95export function AboutVersions () { 96const native = useNative (); 97const app = appVersionLabel ( useStore ( s => s . setup ?. app . version ), native ?. version ); 98const engineVersion = useStore ( s => s . engine ?. engine . version ); 99const engine = shownEngineVersion ( native , engineVersion ); 100return < p className = "settings-about-version" >< strong >{ tr ( "Strata" )} { app ?? '' }</ strong >{ engine ?< span className = "muted" > · { tr ( "Engine" )} { engine }</ span > :null }</ p >; 101}