The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): preserve chat scroll position on tab returns (#27393)
Alex committed
Oct 31, 2026 at 19:18 UTC
9fda652a19717fd492086191192a9a9551d29f04
5 files changed
+130
-21
apps/mobile/src/features/home/HomeScreen.tsx
+5
-5
@@ -52,6 +52,7 @@ import { requestThreadMessageJump } from "../threads/threadMessageJump";
52
import { useThreadListSearchMatches } from "../threads/useThreadListSearchMatches";
53
import { useThreadListV2 } from "../threads/useThreadListV2";
54
import { useThreadListNow } from "../threads/use-thread-list-now";
55
+import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
56
import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts";
57
import { usePendingThreadOrder } from "../../state/thread-order";
58
import { threadListEnvironmentsAtom } from "../../state/presentation";
@@ -317,14 +318,13 @@ export function HomeScreen(props: HomeScreenProps) {
318
const listRef = useRef<LegendListRef>(null);
319
const searchMode = workspace?.state.tabs.threads.searchMode;
320
const restoredOffset = workspace?.state.tabs.threads.scrollOffset;
320
- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- restores the thread list's saved scroll offset through the list ref when search goes back to resting
321
- useEffect(() => {
322
- if (searchMode !== "resting" || restoredOffset === undefined) return;
323
- listRef.current?.scrollToOffset({
321
+ useSearchScrollRestoration(searchMode, () => {
322
+ if (restoredOffset === undefined) return;
323
+ void listRef.current?.scrollToOffset({
324
offset: restoredOffset - (Platform.OS === "ios" ? insets.top : 0),
325
animated: false,
326
});
327
- }, [searchMode, insets.top, restoredOffset]);
327
+ });
328
const swipeRowActivation = createSwipeRowActivation();
329
const activateVisibleRows = (rows: ReadonlyArray<ThreadListV2ListItem>) => {
330
const state = listRef.current?.getState();
apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx
+8
-16
@@ -6,7 +6,7 @@ import {
6
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
7
import { LegendList, type LegendListRef } from "@legendapp/list/react-native";
8
import { useNavigation } from "@react-navigation/native";
9
-import { use, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
9
+import { use, useMemo, useRef, useState, type ReactNode } from "react";
10
import { useAtomValue } from "@effect/atom-react";
11
import {
12
Keyboard,
@@ -49,6 +49,7 @@ import {
49
} from "./pullRequestList";
50
import { useDebouncedValue } from "../../state/queries";
51
import { useWorkspaceTabs } from "../workspace/WorkspaceTabsContext";
52
+import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
53
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
54
55
function PullRequestScrollView(props: ScrollViewProps) {
@@ -118,21 +119,12 @@ export function PullRequestsScreen(
119
const listRef = useRef<LegendListRef>(null);
120
const tab = workspace.state.tabs.pullRequests;
121
const [initialScrollOffset] = useState(() => workspace.getScrollOffset("pullRequests"));
121
- // Cancelling search restores the offset recorded when search opened. The
122
- // toolbar that cancels has no handle on this list, so the list scrolls itself
123
- // once it sees the tab come back to resting.
124
- const previousSearchMode = useRef(tab.searchMode);
125
- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- scrolls the list to the offset saved when search opened once the tab returns to resting
126
- useEffect(() => {
127
- const restoringSearch =
128
- previousSearchMode.current !== "resting" && tab.searchMode === "resting";
129
- previousSearchMode.current = tab.searchMode;
130
- if (restoringSearch)
131
- void listRef.current?.scrollToOffset({
132
- offset: tab.scrollOffset - (Platform.OS === "ios" ? insets.top : 0),
133
- animated: false,
134
- });
135
- }, [tab.searchMode, tab.scrollOffset, insets.top]);
122
+ useSearchScrollRestoration(tab.searchMode, () => {
123
+ void listRef.current?.scrollToOffset({
124
+ offset: tab.scrollOffset - (Platform.OS === "ios" ? insets.top : 0),
125
+ animated: false,
126
+ });
127
+ });
128
const maintainVisiblePosition = {
129
data: tab.searchMode === "resting",
130
shouldRestorePosition: () => workspace.getScrollOffset("pullRequests") > 0,
apps/mobile/src/features/workspace/useSearchScrollRestoration.test.tsx
new
+97
@@ -0,0 +1,97 @@
1
+import { Activity, act, useImperativeHandle, useRef, type Ref } from "react";
2
+import { createRoot } from "react-dom/client";
3
+import { expect, it, vi } from "vite-plus/test";
4
+
5
+import { useSearchScrollRestoration } from "./useSearchScrollRestoration";
6
+import type { WorkspaceSearchMode } from "./workspace-tabs-state";
7
+
8
+it("preserves the live scroll position across Activity reconnections and restores cancelled search", async () => {
9
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
10
+ using _globals = { [Symbol.dispose]: () => vi.unstubAllGlobals() };
11
+ let liveOffset = 0;
12
+ let connections = 0;
13
+ let disconnections = 0;
14
+ const scroll = vi.fn((offset: number) => {
15
+ liveOffset = offset;
16
+ });
17
+ type ListHandle = { scrollToOffset: (offset: number) => void };
18
+ function List({ ref }: { ref: Ref<ListHandle> }) {
19
+ useImperativeHandle(ref, () => {
20
+ connections += 1;
21
+ return { scrollToOffset: scroll };
22
+ });
23
+ return (
24
+ <div
25
+ ref={(node) => {
26
+ if (node === null) disconnections += 1;
27
+ }}
28
+ >
29
+ List
30
+ </div>
31
+ );
32
+ }
33
+ function Screen(props: {
34
+ searchMode: WorkspaceSearchMode | undefined;
35
+ savedOffset: number;
36
+ inset: number;
37
+ }) {
38
+ const list = useRef<ListHandle>(null);
39
+ useSearchScrollRestoration(props.searchMode, () => {
40
+ list.current?.scrollToOffset(props.savedOffset - props.inset);
41
+ });
42
+ return <List ref={list} />;
43
+ }
44
+ const root = createRoot(document.createElement("div"));
45
+ using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
46
+ const input: Parameters<typeof Screen>[0] = {
47
+ searchMode: "resting",
48
+ savedOffset: 0,
49
+ inset: 0,
50
+ };
51
+ const render = async (visible: boolean) => {
52
+ await act(async () =>
53
+ root.render(
54
+ <Activity mode={visible ? "visible" : "hidden"}>
55
+ <Screen {...input} />
56
+ </Activity>,
57
+ ),
58
+ );
59
+ };
60
+
61
+ await render(true);
62
+ expect(scroll).not.toHaveBeenCalled();
63
+ liveOffset = 640;
64
+ await render(false);
65
+ expect(disconnections).toBeGreaterThan(0);
66
+ const connectionsBeforeReturn = connections;
67
+ await render(true);
68
+ expect(connections).toBeGreaterThan(connectionsBeforeReturn);
69
+ expect(liveOffset).toBe(640);
70
+ expect(scroll).not.toHaveBeenCalled();
71
+
72
+ input.searchMode = "active";
73
+ input.savedOffset = liveOffset;
74
+ await render(true);
75
+ input.searchMode = "expanded";
76
+ await render(true);
77
+ liveOffset = 0;
78
+ input.searchMode = "resting";
79
+ input.inset = 24;
80
+ await render(true);
81
+ expect(liveOffset).toBe(616);
82
+ expect(scroll.mock.calls).toEqual([[616]]);
83
+
84
+ liveOffset = 1280;
85
+ input.inset = 48;
86
+ await render(true);
87
+ await render(false);
88
+ await render(true);
89
+ expect(liveOffset).toBe(1280);
90
+ expect(scroll.mock.calls).toEqual([[616]]);
91
+
92
+ input.searchMode = undefined;
93
+ await render(true);
94
+ input.searchMode = "resting";
95
+ await render(true);
96
+ expect(scroll.mock.calls).toEqual([[616]]);
97
+});
apps/mobile/src/features/workspace/useSearchScrollRestoration.ts
new
+19
@@ -0,0 +1,19 @@
1
+import { useEffect, useRef } from "react";
2
+
3
+import type { WorkspaceSearchMode } from "./workspace-tabs-state";
4
+
5
+export function useSearchScrollRestoration(
6
+ searchMode: WorkspaceSearchMode | undefined,
7
+ restore: () => void,
8
+) {
9
+ const previousSearchMode = useRef(searchMode);
10
+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- restores the native list's saved search position
11
+ useEffect(() => {
12
+ const cancelledSearch =
13
+ previousSearchMode.current !== undefined &&
14
+ previousSearchMode.current !== "resting" &&
15
+ searchMode === "resting";
16
+ previousSearchMode.current = searchMode;
17
+ if (cancelledSearch) restore();
18
+ }, [searchMode, restore]);
19
+}
apps/mobile/vite.config.ts
+1
@@ -37,6 +37,7 @@ const reactTests = [
37
"src/features/terminal/useTerminalExitNavigation.test.tsx",
38
"src/features/threads/use-thread-list-now.test.tsx",
39
"src/features/home/home-list-options.test.tsx",
40
+ "src/features/workspace/useSearchScrollRestoration.test.tsx",
41
"src/features/connection/ConnectionsNewRouteScreen.test.tsx",
42
];
43