diff --git a/.changes/help-back-to-game.md b/.changes/help-back-to-game.md new file mode 100644 index 0000000000..39aa2b25e8 --- /dev/null +++ b/.changes/help-back-to-game.md @@ -0,0 +1,6 @@ +--- +app: patch +crate: none +--- +## Player +- In the installed app, Back to Game in the Player's Manual now closes the manual and returns to your game, instead of opening a second copy of the game inside the manual's window. diff --git a/help-site/src/components/Header.astro b/help-site/src/components/Header.astro index cb614b4a8c..8dc1132d6f 100644 --- a/help-site/src/components/Header.astro +++ b/help-site/src/components/Header.astro @@ -8,8 +8,10 @@ // tab the game opened (?from=game, src/ui/fromGame.ts) that button and the // front page's Play button both read Back to game and close this tab instead // of loading a second copy of the game; the note below says so when the -// browser keeps the tab open. In an installed app the button first checks the -// game still runs, and loads it here when it doesn't (backToOpenGame). +// browser keeps the tab open. In an installed app the manual on its own host +// opens in a browser view over the app, so the button closes that view; a +// /help build in the app's own window first checks the game still runs, and +// loads it here when it doesn't (backToOpenGame). import Search from "@astrojs/starlight/components/Search.astro"; import ThemeSelect from "@astrojs/starlight/components/ThemeSelect.astro"; import TitleBar from "../../../docs/engine-site/src/components/TitleBar.astro"; diff --git a/src/ui/fromGame.test.ts b/src/ui/fromGame.test.ts index ce4d074027..764cc08dfb 100644 --- a/src/ui/fromGame.test.ts +++ b/src/ui/fromGame.test.ts @@ -87,10 +87,11 @@ describe("backToOpenGame", () => { }); /** Click the shell's Back to game link in an installed app whose game does or doesn't answer. */ - async function clickInstalled(gameAnswers: boolean): Promise<{ win: Window; e: MouseEvent; stillOpen: ReturnType }> { + async function clickInstalled(gameAnswers: boolean, pageAt: string = location.href): Promise<{ win: Window; e: MouseEvent; stillOpen: ReturnType }> { mountFromGame(); const replace = vi.fn(); - const win = { close: vi.fn(), closed: true, setTimeout: vi.fn(), document, location: { replace } } as unknown as Window; + const page = new URL(pageAt); + const win = { close: vi.fn(), closed: true, setTimeout: vi.fn(), document, location: { replace, href: page.href, origin: page.origin } } as unknown as Window; const stillOpen = vi.fn(() => Promise.resolve(gameAnswers)); const link = document.querySelector("a[data-act=back-to-game]")!; const e = new MouseEvent("click", { cancelable: true }); @@ -114,6 +115,15 @@ describe("backToOpenGame", () => { expect(win.close).toHaveBeenCalledTimes(1); expect(win.location.replace).not.toHaveBeenCalled(); }); + + it("closes the browser view the app opened for a manual on another host, without asking or loading the game there", async () => { + // The shell's link points at this test origin's "/", so a page on any other host sees the game elsewhere. + const { win, e, stillOpen } = await clickInstalled(false, "https://help.example.test/rooms/"); + expect(e.defaultPrevented).toBe(true); + expect(stillOpen).not.toHaveBeenCalled(); + expect(win.close).toHaveBeenCalledTimes(1); + expect(win.location.replace).not.toHaveBeenCalled(); + }); }); /** A stand-in channel that records what it sends and lets a test deliver a message. */ diff --git a/src/ui/fromGame.ts b/src/ui/fromGame.ts index 2117b65728..f6f9419422 100644 --- a/src/ui/fromGame.ts +++ b/src/ui/fromGame.ts @@ -165,21 +165,33 @@ const liveDeps: BackToGameDeps = { installed: isInstalledStandalone, stillOpen: * button loads the game here instead (its tower was saved when the page went * away, so the title screen offers Continue). When the game does answer, it * is in another window and this one closes as in a browser tab. + * + * When the game lives on another host than this page (the manual at + * help.verticopolis.com), this window is never the app's own: the app opens + * an address outside its scope in a browser view over itself, and the + * presence check cannot reach a game on another origin. Loading the game here + * would start a second copy inside that view, so the page closes it instead, + * which brings back the app with its game. */ export function backToOpenGame(e: MouseEvent, win: Window = window, deps: BackToGameDeps = liveDeps): void { e.preventDefault(); - if (!deps.installed()) { + const link = e.currentTarget as HTMLAnchorElement | null; + const gameHref = link?.href || "/"; + if (!deps.installed() || gameOnAnotherHost(gameHref, win)) { closeOrSay(win); return; } - const link = e.currentTarget as HTMLAnchorElement | null; - const gameHref = link?.href || "/"; void deps.stillOpen().then((open) => { if (open) closeOrSay(win); else win.location.replace(gameHref); }); } +/** True when `gameHref` points at another origin than this page. */ +function gameOnAnotherHost(gameHref: string, win: Window): boolean { + return new URL(gameHref, win.location.href).origin !== win.location.origin; +} + /** Close this tab, or say why it stayed open. */ function closeOrSay(win: Window): void { win.close();