Skip to content

Commit a9622dd

Browse files
committed
Merge the phone app's web branch (mobile-shell-polish at 5960aef) into the Android shell branch
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012xYtaUjbkB1hwLtK4TEzhq
2 parents 2531d66 + 5960aef commit a9622dd

3 files changed

Lines changed: 139 additions & 8 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,16 @@ function saveScroll(location: string) {
122122
* A list being put back where it was, while it is: the `route` for its page waits for it
123123
* (`whenScrollRestored`), so the app drops its snapshot only once nothing will jump.
124124
*/
125-
let restoring: { done: Promise<void> } | null = null;
125+
let restoring: { done: Promise<void>; finish: () => void } | null = null;
126+
127+
/**
128+
* Ends a list's restore that is still under way: the page is moving on (a tab tap, another page),
129+
* so whatever waits for it (the route of the page being left) is released, not held behind a list
130+
* that is no longer on screen.
131+
*/
132+
function stopRestoring() {
133+
restoring?.finish();
134+
}
126135

127136
/** Puts `location`'s list back where it was, once it has drawn enough to scroll that far. */
128137
function restoreScroll(location: string) {
@@ -133,13 +142,14 @@ function restoreScroll(location: string) {
133142
const done = new Promise<void>((resolve) => {
134143
settle = resolve;
135144
});
136-
const current = { done };
137-
restoring = current;
138145
const finish = () => {
139146
if (restoring === current) restoring = null;
140147
settle();
141148
};
149+
const current = { done, finish };
150+
restoring = current;
142151
const tick = () => {
152+
if (restoring !== current) return;
143153
const list = document.querySelector(LIST);
144154
if (list) {
145155
list.scrollTop = saved;
@@ -223,6 +233,7 @@ export function useTabNavigation(): void {
223233
pathOf(from) !== pathOf(to);
224234

225235
history.push = (path, state, options) => {
236+
stopRestoring();
226237
const from = here();
227238
saveScroll(from);
228239
stacks = signedOut(path)
@@ -233,6 +244,7 @@ export function useTabNavigation(): void {
233244
else announce("push", stacks, () => replace(path, state, options));
234245
};
235246
history.replace = (path, state, options) => {
247+
stopRestoring();
236248
const from = here();
237249
stacks = signedOut(path)
238250
? cleared()
@@ -242,6 +254,7 @@ export function useTabNavigation(): void {
242254
else announce("replace", stacks, () => replace(path, state, options));
243255
};
244256
apply = (next, direction) => {
257+
if (next.show !== here()) stopRestoring();
245258
const from = here();
246259
if (next.show !== from) saveScroll(from);
247260
stacks = next.state;

‎app/tests/native-shell-app.test.tsx‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,104 @@ describe("the app's swipe back", () => {
828828
});
829829
});
830830

831+
describe("a Back while a list's scroll is being put back", () => {
832+
/** Frames the page asks for, run only when the test says: the scroll's restore is held meanwhile. */
833+
let frames: FrameRequestCallback[] = [];
834+
let realFrame: typeof window.requestAnimationFrame;
835+
beforeEach(() => {
836+
frames = [];
837+
realFrame = window.requestAnimationFrame;
838+
window.requestAnimationFrame = ((callback: FrameRequestCallback) => {
839+
frames.push(callback);
840+
return frames.length;
841+
}) as typeof window.requestAnimationFrame;
842+
});
843+
afterEach(() => {
844+
window.requestAnimationFrame = realFrame;
845+
});
846+
const runFrames = () =>
847+
act(() => {
848+
const now = frames;
849+
frames = [];
850+
for (const frame of now) frame(performance.now());
851+
});
852+
const routesOf = (heard: unknown[]) =>
853+
heard
854+
.filter((m) => (m as { type: string }).type === "route")
855+
.map((m) => (m as { path: string; depth: number }).path);
856+
const back = () =>
857+
act(() => {
858+
window.dispatchEvent(new CustomEvent("openbot:back"));
859+
});
860+
861+
/** The Chats list scrolled down, then a conversation opened from it, then Back: the hold. */
862+
async function holding() {
863+
inShell(true);
864+
const heard = listen();
865+
serve(
866+
Array.from({ length: 30 }, (_, index) =>
867+
channel(`c-${index}`, `Conversation ${index}`),
868+
),
869+
);
870+
const { view, router } = renderApp("/conversations");
871+
await view.findByText("Conversation 29");
872+
const list = view.container.querySelector(
873+
'[data-slot="section-panel"] [data-slot="sidebar-content"]',
874+
) as HTMLElement;
875+
list.scrollTop = 400;
876+
await act(() =>
877+
router.navigate({
878+
to: "/channel/$channelId",
879+
params: { channelId: "c-3" },
880+
}),
881+
);
882+
await view.findByText("A conversation");
883+
await back();
884+
await view.findByText("Conversation 29");
885+
await settleReactWork();
886+
return { heard, view, router };
887+
}
888+
889+
test("a second Back at the root does nothing, and the held route goes out once", async () => {
890+
const { heard, router } = await holding();
891+
// Held: the list has not been put back yet, so the root's route has not been said.
892+
expect(routesOf(heard).at(-1)).toBe("/channel/c-3");
893+
// Android's Back still read canGoBack true and asks again.
894+
await back();
895+
await settleReactWork();
896+
expect(router.state.location.pathname).toBe("/conversations");
897+
await runFrames();
898+
await waitFor(() =>
899+
expect(routesOf(heard)).toEqual([
900+
"/conversations",
901+
"/channel/c-3",
902+
"/conversations",
903+
]),
904+
);
905+
await runFrames();
906+
await settleReactWork();
907+
expect(
908+
routesOf(heard).filter((path) => path === "/conversations"),
909+
).toHaveLength(2);
910+
});
911+
912+
test("a tab tap during the hold is not lost: the tapped tab is shown and reported", async () => {
913+
const { heard, view, router } = await holding();
914+
act(() => {
915+
window.dispatchEvent(
916+
new CustomEvent("openbot:open", {
917+
detail: { path: "/bots" },
918+
cancelable: true,
919+
}),
920+
);
921+
});
922+
await view.findByText("The roster");
923+
await runFrames();
924+
await waitFor(() => expect(routesOf(heard).at(-1)).toBe("/bots"));
925+
expect(router.state.location.pathname).toBe("/bots");
926+
});
927+
});
928+
831929
describe("the Bots tab in the shell", () => {
832930
test("is the sidebar's Bots panel, with New Bot and the account in its title's row", async () => {
833931
inShell(true);

‎app/workbench/mock-server.ts‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
* # phone app builds that don't send it yet:
2222
* MOCK_ENTER_NO_HEADER_CHECK=1 bun app/workbench/mock-server.ts
2323
*
24+
* # Another person in another organization (Sam Rivera in Acme when unset), say for a second mock
25+
* # standing in for a self-hosted OpenBot:
26+
* MOCK_PERSON_NAME="Robin Park" MOCK_ORG_NAME="Globex" SERVER_PORT=3002 bun app/workbench/mock-server.ts
27+
*
2428
* It listens on 127.0.0.1:${SERVER_PORT:-3001}. Besides the API it answers:
2529
*
2630
* GET /__mock/session {"signedIn": boolean}: whether a request with no cookie is
@@ -45,7 +49,8 @@
4549
* Ends the mock's session: its cookie stops counting until the next /enter.
4650
*
4751
* Signed in, the API answers as the workbench's app-home scene does (workbench/session.ts,
48-
* api/extraHandlers.ts and the spec's generated answers), with a fixed person (Sam Rivera, in Acme),
52+
* api/extraHandlers.ts and the spec's generated answers), with a person (Sam Rivera, in Acme, unless
53+
* MOCK_PERSON_NAME and MOCK_ORG_NAME say otherwise),
4954
* three Bots (General Assistant, Research Bot, Writing Bot) and fifteen conversations, the first three
5055
* Competitor pricing notes (pinned and unread), Quarterly forecast and Trip to Lisbon, then one a Bot
5156
* is working in (Board update for December), another unread (Launch announcement) and more, eight of
@@ -68,9 +73,24 @@ import { DEFAULT_SESSION, sessionApi } from "./session";
6873

6974
const PORT = Number(process.env.SERVER_PORT ?? 3001);
7075
const ORIGIN = `http://127.0.0.1:${PORT}`;
71-
const ORGANIZATION = DEFAULT_SESSION.signedIn
72-
? DEFAULT_SESSION.organization.id
73-
: "default";
76+
/**
77+
* The signed-in person and their organization: Sam Rivera in Acme, or the names in MOCK_PERSON_NAME
78+
* and MOCK_ORG_NAME, so a second mock beside the first (a self-hosted OpenBot in a sign-in test) can
79+
* show which server a screen is on. The app draws the initials from the name.
80+
*/
81+
const SESSION: typeof DEFAULT_SESSION = DEFAULT_SESSION.signedIn
82+
? {
83+
...DEFAULT_SESSION,
84+
name: process.env.MOCK_PERSON_NAME?.trim() || DEFAULT_SESSION.name,
85+
organization: {
86+
...DEFAULT_SESSION.organization,
87+
name:
88+
process.env.MOCK_ORG_NAME?.trim() ||
89+
DEFAULT_SESSION.organization.name,
90+
},
91+
}
92+
: DEFAULT_SESSION;
93+
const ORGANIZATION = SESSION.signedIn ? SESSION.organization.id : "default";
7494
const API = `*/api/v2/organizations/:organizationId`;
7595
/** The cookie that makes a request signed in, whatever the session's default. */
7696
export const MOCK_SESSION_COOKIE = "better-auth.session_token";
@@ -758,7 +778,7 @@ async function start() {
758778
now: () => performance.now(),
759779
});
760780
// The signed-in session's answers. A signed-out request is refused before it gets here.
761-
for (const [route, answer] of Object.entries(sessionApi(DEFAULT_SESSION))) {
781+
for (const [route, answer] of Object.entries(sessionApi(SESSION))) {
762782
if (answer) api.setAnswer(route as ApiRoute, answer);
763783
}
764784
const answer = unused.fetch;

0 commit comments

Comments
 (0)