Skip to content

Commit 9fda652

Browse files
authored
fix(mobile): preserve chat scroll position on tab returns (#27393)
1 parent bfd37b7 commit 9fda652

5 files changed

Lines changed: 130 additions & 21 deletions

File tree

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import { requestThreadMessageJump } from "../threads/threadMessageJump";
5252
import { useThreadListSearchMatches } from "../threads/useThreadListSearchMatches";
5353
import { useThreadListV2 } from "../threads/useThreadListV2";
5454
import { useThreadListNow } from "../threads/use-thread-list-now";
55+
import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
5556
import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts";
5657
import { usePendingThreadOrder } from "../../state/thread-order";
5758
import { threadListEnvironmentsAtom } from "../../state/presentation";
@@ -317,14 +318,13 @@ export function HomeScreen(props: HomeScreenProps) {
317318
const listRef = useRef<LegendListRef>(null);
318319
const searchMode = workspace?.state.tabs.threads.searchMode;
319320
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({
324324
offset: restoredOffset - (Platform.OS === "ios" ? insets.top : 0),
325325
animated: false,
326326
});
327-
}, [searchMode, insets.top, restoredOffset]);
327+
});
328328
const swipeRowActivation = createSwipeRowActivation();
329329
const activateVisibleRows = (rows: ReadonlyArray<ThreadListV2ListItem>) => {
330330
const state = listRef.current?.getState();

‎apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
77
import { LegendList, type LegendListRef } from "@legendapp/list/react-native";
88
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";
1010
import { useAtomValue } from "@effect/atom-react";
1111
import {
1212
Keyboard,
@@ -49,6 +49,7 @@ import {
4949
} from "./pullRequestList";
5050
import { useDebouncedValue } from "../../state/queries";
5151
import { useWorkspaceTabs } from "../workspace/WorkspaceTabsContext";
52+
import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
5253
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
5354

5455
function PullRequestScrollView(props: ScrollViewProps) {
@@ -118,21 +119,12 @@ export function PullRequestsScreen(
118119
const listRef = useRef<LegendListRef>(null);
119120
const tab = workspace.state.tabs.pullRequests;
120121
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+
});
136128
const maintainVisiblePosition = {
137129
data: tab.searchMode === "resting",
138130
shouldRestorePosition: () => workspace.getScrollOffset("pullRequests") > 0,
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const reactTests = [
3737
"src/features/terminal/useTerminalExitNavigation.test.tsx",
3838
"src/features/threads/use-thread-list-now.test.tsx",
3939
"src/features/home/home-list-options.test.tsx",
40+
"src/features/workspace/useSearchScrollRestoration.test.tsx",
4041
"src/features/connection/ConnectionsNewRouteScreen.test.tsx",
4142
];
4243

0 commit comments

Comments
 (0)