okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
1.2 KiB28 linesraw
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 {
6  mode: 'window' | 'browser' | null;
7  reason: string | null;
8  canQuit: boolean;
9  appUpdate: '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> {
20  loading ??= api.get<AppMode>('/api/app/mode').catch(() => NONE).then(m => (known = m));
21  return loading;
22}
23
24export function useAppMode(): AppMode {
25  const [mode, setMode] = useState<AppMode>(NONE);
26  useEffect(() => { let live = true; void loadAppMode().then(m => { if (live) setMode(m); }); return () => { live = false; }; }, []);
27  return mode;
28}