okami.mom/strata

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

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

main
1.3 KiB31 linesraw
1import { api } from './client';
2import { appModeKnown, loadAppMode, type AppMode } from './app-mode';
3
4export interface ExternalDeps {
5  known: () => AppMode | null;
6  load: () => Promise<AppMode>;
7  post: (path: string, body: unknown) => Promise<unknown>;
8  open: (url: string) => void;
9}
10
11/**
12 * Opens a web address in the person's own browser (#4721). The app window has no handler for a new window, so window.open does nothing
13 * there: the host hands the address to the default browser. In a browser tab the browser opens it itself. The answer to "which window am I
14 * in" is read once at start, so the click can open the tab at once (a popup opened late can be blocked); if it has not arrived yet, the
15 * click waits for it.
16 */
17export async function openExternal(url: string, deps: Partial<ExternalDeps> = {}): Promise<void> {
18  const d: ExternalDeps = {
19    known: appModeKnown,
20    load: loadAppMode,
21    post: (path, body) => api.post(path, body),
22    open: u => { window.open(u, '_blank', 'noopener,noreferrer'); },
23    ...deps,
24  };
25  const mode = d.known() ?? await d.load();
26  if (mode.mode === 'window') {
27    try { await d.post('/api/artifacts/open', { action: 'open', url }); return; }
28    catch { /* the host could not: the page tries as well */ }
29  }
30  d.open(url);
31}