diff --git a/client/src/native/nativeApp.test.ts b/client/src/native/nativeApp.test.ts index 3284e5c4f..219c53a63 100644 --- a/client/src/native/nativeApp.test.ts +++ b/client/src/native/nativeApp.test.ts @@ -11,9 +11,12 @@ import { prepareKeyboardFields } from './keyboardFields'; import { scrollToTop, startNativeApp } from './nativeApp'; import { keepOfflineShellFresh } from './offlineShell'; import { isNativeApp, isNativeIos } from './platform'; +import { openPushTarget } from './push'; import { trackTouchInput } from './touchInput'; -vi.mock('@capacitor/app', () => ({ App: { addListener: vi.fn(), minimizeApp: vi.fn() } })); +vi.mock('@capacitor/app', () => ({ + App: { addListener: vi.fn(), minimizeApp: vi.fn(), getLaunchUrl: vi.fn(async () => undefined) }, +})); vi.mock('@capacitor/keyboard', () => ({ Keyboard: { setAccessoryBarVisible: vi.fn(), addListener: vi.fn(async () => ({ remove: vi.fn() })) }, })); @@ -193,4 +196,26 @@ describe('startNativeApp', () => { expect(report).not.toHaveBeenCalled(); vi.unstubAllGlobals(); }); + + it('follows the link a widget tap launched the app with, once per launch', async () => { + sessionStorage.clear(); + vi.mocked(App.getLaunchUrl).mockResolvedValue({ url: 'com.liketrek.trek://navigate/trips/5?tab=listen' }); + startNativeApp(); + await vi.waitFor(() => expect(openPushTarget).toHaveBeenCalledWith('/trips/5?tab=listen')); + // A reload of the page in the same launch does not follow it again. + vi.mocked(openPushTarget).mockClear(); + startNativeApp(); + await Promise.resolve(); + await Promise.resolve(); + expect(openPushTarget).not.toHaveBeenCalled(); + }); + + it('leaves the start page alone for a launch without a TREK link', async () => { + sessionStorage.clear(); + vi.mocked(App.getLaunchUrl).mockResolvedValue({ url: 'https://example.com/elsewhere' }); + startNativeApp(); + await Promise.resolve(); + await Promise.resolve(); + expect(openPushTarget).not.toHaveBeenCalled(); + }); }); diff --git a/client/src/native/nativeApp.ts b/client/src/native/nativeApp.ts index ea1237383..173ddec0b 100644 --- a/client/src/native/nativeApp.ts +++ b/client/src/native/nativeApp.ts @@ -59,6 +59,8 @@ export function startNativeApp(): void { const prefix = `${APP_URL_SCHEME}://navigate`; if (url.startsWith(prefix)) openPushTarget(url.slice(prefix.length) || '/'); }); + // A tap that launched the app comes as the launch URL, not as appUrlOpen. + void openLaunchLink(); // A tap on the status bar scrolls the list under it back to the top, as in // every iOS app (the status-bar plugin fires statusTap on iOS). @@ -114,6 +116,29 @@ async function fetchServerFeatures(): Promise<{ minNativeShellApi?: unknown }> { } /** The scrolled list in the middle of the screen, else the page, back to its top. */ +/** The launch link this web view has already followed, so a reload does not follow it again. */ +const LAUNCH_LINK_KEY = 'trek_launch_link_done'; + +/** + * A widget or Live Activity tap that starts the app from closed hands it + * com.liketrek.trek://navigate/ as the launch URL. appUrlOpen only fires + * for a running app, so the path was lost and the app opened its start page + * (a packing widget landed on the trip's plan, not its lists). Followed once + * per launch; the trip planner takes a ?tab= on a trip it already shows. + */ +async function openLaunchLink(): Promise { + const prefix = `${APP_URL_SCHEME}://navigate`; + try { + const url = (await App.getLaunchUrl())?.url; + if (!url || !url.startsWith(prefix)) return; + if (sessionStorage.getItem(LAUNCH_LINK_KEY) === url) return; + sessionStorage.setItem(LAUNCH_LINK_KEY, url); + openPushTarget(url.slice(prefix.length) || '/'); + } catch { + /* no launch URL here (an older app binary, or storage blocked): the start page stays */ + } +} + export function scrollToTop(): void { let el = document.elementFromPoint(window.innerWidth / 2, window.innerHeight / 2) as HTMLElement | null; while (el && el !== document.body) {