okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { useEffect , useState } from 'react' ; 2import { api } from './client' ; 3 4/** Which window shows the page: the app window ('window'), a browser tab ('browser'), or neither (a host nobody started: null). */ 5export interface AppMode { 6mode :'window' | 'browser' | null ; 7reason :string | null ; 8canQuit :boolean ; 9appUpdate :'available' | 'unavailable' ; 10} 11 12const NONE :AppMode = { mode :null , reason :null , canQuit :false , appUpdate :'unavailable' }; 13let loading :Promise < AppMode > | null = null ; 14let known :AppMode | null = null ; 15/** The answer if it has already arrived (a click handler that must act at once uses it), otherwise null. */ 16export const appModeKnown = () :AppMode | null => known ; 17 18/** Asked once per page load; an older host without the route (404) or a failed call means "not the app": nothing extra is shown. */ 19export function loadAppMode () :Promise < AppMode > { 20loading ??= api . get < AppMode >( '/api/app/mode' ). catch (() => NONE ). then ( m => ( known = m )); 21return loading ; 22} 23 24export function useAppMode () :AppMode { 25const [ mode , setMode ] = useState < AppMode >( NONE ); 26useEffect (() => { let live = true ; void loadAppMode (). then ( m => { if ( live ) setMode ( m ); }); return () => { live = false ; }; }, []); 27return mode ; 28}