okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1import { api } from './client' ; 2import { appModeKnown , loadAppMode , type AppMode } from './app-mode' ; 3 4export interface ExternalDeps { 5known :() => AppMode | null ; 6load :() => Promise < AppMode >; 7post :( path :string , body :unknown ) => Promise < unknown >; 8open :( 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 > { 18const d :ExternalDeps = { 19known :appModeKnown , 20load :loadAppMode , 21post :( path , body ) => api . post ( path , body ), 22open :u => { window . open ( u , '_blank' , 'noopener,noreferrer' ); }, 23 ...deps , 24}; 25const mode = d . known () ?? await d . load (); 26if ( mode . mode === 'window' ) { 27try { await d . post ( '/api/artifacts/open' , { action :'open' , url}); return ; } 28catch { /* the host could not: the page tries as well */ } 29} 30d . open ( url ); 31}