Skip to content

Commit 0ec0b01

Browse files
committed
Tell the iOS app how deep each tab is and when the page is about to change, for its swipe back
Every route message now carries depth, the number of entries in the current tab's stack (1 at its root; canGoBack is depth > 1), and scrollRestored: true, held until a list returned to has its scroll position back, so the app drops its snapshot with no jump. A new message, will-navigate { tab, direction: push | back | replace, depth }, comes before the page changes what is on screen, for a navigation inside a tab that the page started and that changes the path: a row or link, its own Back, a page replacing itself. Not for a tab switch, a tap on the current tab, a change of search alone, or Back the app asked for. On iOS, and only when a handler received it, the page then waits for openbot:navigate-ok, at most 120 ms; on Android and in a browser it never waits. Bridge version 1; nothing changes for a page with no app listening. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
1 parent 657e753 commit 0ec0b01

5 files changed

Lines changed: 397 additions & 29 deletions

File tree

‎app/src/lib/native-shell/bridge.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
* never the query or the fragment; the tab whose stack the page is in (`use-tab-navigation.ts`;
77
* by the address, `tabForPath`, where the stacks have not started); and whether Back has somewhere
88
* to go inside that tab. Native sends `openbot:back` for its Back when it does.
9+
* It also carries `depth`, how many locations the current tab's stack holds (1 at its root, so
10+
* `canGoBack` is `depth > 1`), and `scrollRestored: true`: a list returned to by Back has its
11+
* `route` sent once its scroll position is back.
12+
* - `{ type: "will-navigate", tab, direction, depth }` before the page changes what is on screen,
13+
* for a navigation inside a tab that the page started itself (a row, a link, its own Back) and
14+
* that changes the path. `direction` is "push", "back" or "replace"; `depth` is where it leads.
15+
* Not for a tab switch, a tap on the current tab, or Back native asked for (`openbot:back`). On
16+
* iOS the page then waits for `openbot:navigate-ok` on `window`, at most 120 ms, before it shows
17+
* the new page, so the app can take its snapshot for the swipe back.
918
* - `{ type: "signed-out" }` when the page reaches the signed-out state, where the shell shows its
1019
* own sign-in screen.
1120
*
@@ -27,6 +36,18 @@ export type ShellMessage =
2736
tab: NativeTab | null;
2837
signedIn: boolean;
2938
canGoBack: boolean;
39+
/** How many locations the current tab's stack holds: 1 at its root. */
40+
depth: number;
41+
/** Always true: a page whose list position is being put back is reported once it is. */
42+
scrollRestored: true;
43+
}
44+
| {
45+
/** Sent before the page changes what is on screen, for a navigation the page started. */
46+
type: "will-navigate";
47+
tab: NativeTab;
48+
direction: "push" | "back" | "replace";
49+
/** The depth the navigation leads to. */
50+
depth: number;
3051
}
3152
| { type: "signed-out" };
3253

@@ -39,6 +60,20 @@ type ShellWindow = {
3960
OpenBotShellAndroid?: { postMessage(message: string): void };
4061
};
4162

63+
/** Whether a shell is listening for messages: its iOS handler or its Android object is there. */
64+
export function shellListens(
65+
target: ShellWindow = window as ShellWindow,
66+
): boolean {
67+
try {
68+
return Boolean(
69+
target.webkit?.messageHandlers?.openbotShell ??
70+
target.OpenBotShellAndroid,
71+
);
72+
} catch {
73+
return false;
74+
}
75+
}
76+
4277
/** Sends `message` to the shell. Never throws: the shell not listening is no fault of the page's. */
4378
export function postToShell(
4479
message: ShellMessage,

‎app/src/lib/native-shell/route-reports.ts‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useRouterState } from "@tanstack/react-router";
22
import { useEffect } from "react";
33
import { postToShell, type ShellMessage, tabForPath } from "./bridge";
44
import { nativeShell } from "./shell";
5-
import { shellTabState } from "./use-tab-navigation";
5+
import { shellTabState, whenScrollRestored } from "./use-tab-navigation";
66

77
/**
88
* WHEN THE SHELL HEARS `ready`: once per page load, when the first page has drawn something a person
@@ -166,15 +166,22 @@ export function useNativeShellRouteReports(): void {
166166
useEffect(() => {
167167
if (!inShell || settled === null) return;
168168
const path = settled.slice(1);
169-
// The tab whose stack the page is in, and whether Back can go anywhere in it. The sign-in page
170-
// is in no tab: the shell shows its own sign-in over it.
171-
const tabs = path === "/sign" ? null : shellTabState();
172-
tellShell({
173-
type: "route",
174-
path,
175-
tab: tabs ? tabs.tab : tabForPath(path),
176-
signedIn: settled.startsWith("1"),
177-
canGoBack: tabs?.canGoBack ?? false,
169+
/*
170+
* The tab whose stack the page is in, whether Back can go anywhere in it and how deep it is. The
171+
* sign-in page is in no tab: the shell shows its own sign-in over it. A list returned to by Back
172+
* is reported once its scroll position is back, so the app drops its snapshot with no jump.
173+
*/
174+
return whenScrollRestored(() => {
175+
const tabs = path === "/sign" ? null : shellTabState();
176+
tellShell({
177+
type: "route",
178+
path,
179+
tab: tabs ? tabs.tab : tabForPath(path),
180+
signedIn: settled.startsWith("1"),
181+
canGoBack: tabs?.canGoBack ?? false,
182+
depth: tabs?.depth ?? 1,
183+
scrollRestored: true,
184+
});
178185
});
179186
}, [inShell, settled]);
180187
}

‎app/src/lib/native-shell/use-tab-navigation.ts‎

Lines changed: 117 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useRouter } from "@tanstack/react-router";
22
import { useEffect } from "react";
3-
import { type NativeTab, tabForPath } from "./bridge";
3+
import { type NativeTab, shellListens, tabForPath } from "./bridge";
4+
import { tellShell } from "./route-reports";
45
import { nativeShell } from "./shell";
56
import {
67
canGoBack,
@@ -44,19 +45,35 @@ export const BACK_REQUEST = "openbot:back";
4445

4546
/** The tabs' stacks for this page load, once the shell's navigation has started. */
4647
let stacks: TabStacks | null = null;
47-
/** Shows a step (set while the navigation is mounted). */
48-
let apply: ((next: TabStep) => void) | null = null;
48+
/** Shows a step, announced to the app as `direction` when given (set while mounted). */
49+
let apply: ((next: TabStep, direction?: Direction) => void) | null = null;
50+
51+
type Direction = "push" | "back" | "replace";
4952

5053
/** For tests: forget the stacks, as a new page load does. */
5154
export function forgetTabStacksForTests(): void {
5255
stacks = null;
5356
apply = null;
5457
scrolls.clear();
58+
restoring = null;
5559
}
5660

57-
/** The tab the page is in and whether Back can go anywhere, inside the shell; null outside it. */
58-
export function shellTabState(): { tab: NativeTab; canGoBack: boolean } | null {
59-
return stacks ? { tab: stacks.current, canGoBack: canGoBack(stacks) } : null;
61+
/**
62+
* The tab the page is in, whether Back can go anywhere, and how deep its stack is (1 at its root),
63+
* inside the shell; null outside it.
64+
*/
65+
export function shellTabState(): {
66+
tab: NativeTab;
67+
canGoBack: boolean;
68+
depth: number;
69+
} | null {
70+
return stacks
71+
? {
72+
tab: stacks.current,
73+
canGoBack: canGoBack(stacks),
74+
depth: stacks.stacks[stacks.current].length,
75+
}
76+
: null;
6077
}
6178

6279
/** The root of the tab the page is in, inside the shell: where a back control falls back to. */
@@ -70,7 +87,7 @@ export function currentTabRoot(): string | null {
7087
*/
7188
export function goBackInShell(): boolean {
7289
if (!stacks || !apply || !canGoBack(stacks)) return false;
73-
apply(goBack(stacks));
90+
apply(goBack(stacks), "back");
7491
return true;
7592
}
7693

@@ -101,22 +118,88 @@ function saveScroll(location: string) {
101118
if (list) scrolls.set(location, list.scrollTop);
102119
}
103120

121+
/**
122+
* A list being put back where it was, while it is: the `route` for its page waits for it
123+
* (`whenScrollRestored`), so the app drops its snapshot only once nothing will jump.
124+
*/
125+
let restoring: { done: Promise<void> } | null = null;
126+
104127
/** Puts `location`'s list back where it was, once it has drawn enough to scroll that far. */
105128
function restoreScroll(location: string) {
106129
const saved = scrolls.get(location);
107130
if (!saved) return;
108131
let frames = 0;
132+
let settle = () => {};
133+
const done = new Promise<void>((resolve) => {
134+
settle = resolve;
135+
});
136+
const current = { done };
137+
restoring = current;
138+
const finish = () => {
139+
if (restoring === current) restoring = null;
140+
settle();
141+
};
109142
const tick = () => {
110143
const list = document.querySelector(LIST);
111144
if (list) {
112145
list.scrollTop = saved;
113-
if (Math.abs(list.scrollTop - saved) < 2) return;
146+
if (Math.abs(list.scrollTop - saved) < 2) return finish();
114147
}
148+
// About a second at most: a list that can no longer scroll that far is shown where it can.
115149
if (++frames < 60) requestAnimationFrame(tick);
150+
else finish();
116151
};
117152
requestAnimationFrame(tick);
118153
}
119154

155+
/** Runs `then` once no list is being put back (at once, mostly). */
156+
export function whenScrollRestored(then: () => void): () => void {
157+
let live = true;
158+
const pending = restoring;
159+
if (!pending) then();
160+
else void pending.done.then(() => live && then());
161+
return () => {
162+
live = false;
163+
};
164+
}
165+
166+
/** The app's answer to `will-navigate`: it holds its snapshot, so the page may change. */
167+
export const NAVIGATE_OK = "openbot:navigate-ok";
168+
/** How long the page waits for that answer before it goes ahead anyway. */
169+
export const NAVIGATE_OK_MS = 120;
170+
171+
/**
172+
* Tells the app a navigation the page started is coming (`will-navigate`), then runs `go`: at once,
173+
* or on iOS, where the app takes a snapshot of the page for its swipe back, once the app answers
174+
* `openbot:navigate-ok` or after `NAVIGATE_OK_MS`, whichever is first. Never waits where nothing
175+
* received the message, nor on Android, which does not answer.
176+
*/
177+
function announce(direction: Direction, next: TabStacks, go: () => void): void {
178+
const message = {
179+
type: "will-navigate",
180+
tab: next.current,
181+
direction,
182+
depth: next.stacks[next.current].length,
183+
} as const;
184+
if (nativeShell()?.platform !== "ios" || !shellListens()) {
185+
tellShell(message);
186+
go();
187+
return;
188+
}
189+
// Listening before it is said, so an answer given at once is not missed.
190+
let gone = false;
191+
const once = () => {
192+
if (gone) return;
193+
gone = true;
194+
clearTimeout(timer);
195+
window.removeEventListener(NAVIGATE_OK, once);
196+
go();
197+
};
198+
const timer = setTimeout(once, NAVIGATE_OK_MS);
199+
window.addEventListener(NAVIGATE_OK, once);
200+
tellShell(message);
201+
}
202+
120203
function scrollListToTop() {
121204
document.querySelector(LIST)?.scrollTo({ top: 0, behavior: "smooth" });
122205
}
@@ -135,31 +218,47 @@ export function useTabNavigation(): void {
135218
const push = history.push;
136219
const replace = history.replace;
137220

221+
/** Whether going from `from` to `to` changes the page, as opposed to its search or fragment. */
222+
const changesPage = (from: string, to: string) =>
223+
pathOf(from) !== pathOf(to);
224+
138225
history.push = (path, state, options) => {
139-
saveScroll(here());
226+
const from = here();
227+
saveScroll(from);
140228
stacks = signedOut(path)
141229
? cleared()
142-
: pushLocation(stacks ?? stacksFor(here()), path).state;
143-
replace(path, state, options);
230+
: pushLocation(stacks ?? stacksFor(from), path).state;
231+
if (signedOut(path) || !changesPage(from, path))
232+
replace(path, state, options);
233+
else announce("push", stacks, () => replace(path, state, options));
144234
};
145235
history.replace = (path, state, options) => {
236+
const from = here();
146237
stacks = signedOut(path)
147238
? cleared()
148-
: replaceLocation(stacks ?? stacksFor(here()), path).state;
149-
replace(path, state, options);
239+
: replaceLocation(stacks ?? stacksFor(from), path).state;
240+
if (signedOut(path) || !changesPage(from, path))
241+
replace(path, state, options);
242+
else announce("replace", stacks, () => replace(path, state, options));
150243
};
151-
apply = (next) => {
244+
apply = (next, direction) => {
152245
const from = here();
153246
if (next.show !== from) saveScroll(from);
154247
stacks = next.state;
155-
if (next.show !== from) {
248+
if (next.scrollToTop) scrollListToTop();
249+
if (next.show === from) return;
250+
const go = () => {
156251
replace(next.show);
157252
restoreScroll(next.show);
158-
}
159-
if (next.scrollToTop) scrollListToTop();
253+
};
254+
if (direction && changesPage(from, next.show))
255+
announce(direction, next.state, go);
256+
else go();
160257
};
258+
// Back the app asked for: popped as the page's own Back is, but not announced, since the app
259+
// started it and already knows.
161260
const onBack = () => {
162-
goBackInShell();
261+
if (stacks && apply && canGoBack(stacks)) apply(goBack(stacks));
163262
};
164263
window.addEventListener(BACK_REQUEST, onBack);
165264
return () => {

0 commit comments

Comments
 (0)