fix(native): a widget tap that starts the app opens what it points at

A tap on a widget or a Live Activity hands the app
com.liketrek.trek://navigate/<path>. A running app gets it as appUrlOpen;
an app started by the tap gets it as its launch URL, which nothing read, so
the path was lost and the app opened its start page (the packing widget
landed on the trip's plan instead of its lists). The launch link is now
followed once per launch.
This commit is contained in:
Maurice
2026-11-12 16:53:04 +01:00
parent 4983b3407c
commit 56b9bac031
2 changed files with 51 additions and 1 deletions
+26 -1
View File
@@ -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();
});
});
+25
View File
@@ -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/<path> 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<void> {
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) {