Skip to content
TypeScript

fix(mobile): preserve chat scroll position on tab returns (#27393)

Alex9fda652a1971
diff --git a/apps/mobile/src/features/home/HomeScreen.tsx b/apps/mobile/src/features/home/HomeScreen.tsx
index 243e693d1b8..5ac49af24a4 100644
--- a/apps/mobile/src/features/home/HomeScreen.tsx
+++ b/apps/mobile/src/features/home/HomeScreen.tsx
@@ -52,6 +52,7 @@ import { requestThreadMessageJump } from "../threads/threadMessageJump";
import { useThreadListSearchMatches } from "../threads/useThreadListSearchMatches";
import { useThreadListV2 } from "../threads/useThreadListV2";
import { useThreadListNow } from "../threads/use-thread-list-now";
+import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts";
import { usePendingThreadOrder } from "../../state/thread-order";
import { threadListEnvironmentsAtom } from "../../state/presentation";
@@ -317,14 +318,13 @@ export function HomeScreen(props: HomeScreenProps) {
const listRef = useRef<LegendListRef>(null);
const searchMode = workspace?.state.tabs.threads.searchMode;
const restoredOffset = workspace?.state.tabs.threads.scrollOffset;
- // 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
- useEffect(() => {
- if (searchMode !== "resting" || restoredOffset === undefined) return;
- listRef.current?.scrollToOffset({
+ useSearchScrollRestoration(searchMode, () => {
+ if (restoredOffset === undefined) return;
+ void listRef.current?.scrollToOffset({
offset: restoredOffset - (Platform.OS === "ios" ? insets.top : 0),
animated: false,
});
- }, [searchMode, insets.top, restoredOffset]);
+ });
const swipeRowActivation = createSwipeRowActivation();
const activateVisibleRows = (rows: ReadonlyArray<ThreadListV2ListItem>) => {
const state = listRef.current?.getState();
diff --git a/apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx b/apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx
index 02e8b516dec..5d55f86c4e8 100644
--- a/apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx
+++ b/apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx
@@ -6,7 +6,7 @@ import {
import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import { LegendList, type LegendListRef } from "@legendapp/list/react-native";
import { useNavigation } from "@react-navigation/native";
-import { use, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
+import { use, useMemo, useRef, useState, type ReactNode } from "react";
import { useAtomValue } from "@effect/atom-react";
import {
Keyboard,
@@ -49,6 +49,7 @@ import {
} from "./pullRequestList";
import { useDebouncedValue } from "../../state/queries";
import { useWorkspaceTabs } from "../workspace/WorkspaceTabsContext";
+import { useSearchScrollRestoration } from "../workspace/useSearchScrollRestoration";
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
function PullRequestScrollView(props: ScrollViewProps) {
@@ -118,21 +119,12 @@ export function PullRequestsScreen(
const listRef = useRef<LegendListRef>(null);
const tab = workspace.state.tabs.pullRequests;
const [initialScrollOffset] = useState(() => workspace.getScrollOffset("pullRequests"));
- // Cancelling search restores the offset recorded when search opened. The
- // toolbar that cancels has no handle on this list, so the list scrolls itself
- // once it sees the tab come back to resting.
- const previousSearchMode = useRef(tab.searchMode);
- // 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
- useEffect(() => {
- const restoringSearch =
- previousSearchMode.current !== "resting" && tab.searchMode === "resting";
- previousSearchMode.current = tab.searchMode;
- if (restoringSearch)
- void listRef.current?.scrollToOffset({
- offset: tab.scrollOffset - (Platform.OS === "ios" ? insets.top : 0),
- animated: false,
- });
- }, [tab.searchMode, tab.scrollOffset, insets.top]);
+ useSearchScrollRestoration(tab.searchMode, () => {
+ void listRef.current?.scrollToOffset({
+ offset: tab.scrollOffset - (Platform.OS === "ios" ? insets.top : 0),
+ animated: false,
+ });
+ });
const maintainVisiblePosition = {
data: tab.searchMode === "resting",
shouldRestorePosition: () => workspace.getScrollOffset("pullRequests") > 0,
diff --git a/apps/mobile/src/features/workspace/useSearchScrollRestoration.test.tsx b/apps/mobile/src/features/workspace/useSearchScrollRestoration.test.tsx
new file mode 100644
index 00000000000..c6ac3ace5a6
--- /dev/null
+++ b/apps/mobile/src/features/workspace/useSearchScrollRestoration.test.tsx
@@ -0,0 +1,97 @@
+import { Activity, act, useImperativeHandle, useRef, type Ref } from "react";
+import { createRoot } from "react-dom/client";
+import { expect, it, vi } from "vite-plus/test";
+
+import { useSearchScrollRestoration } from "./useSearchScrollRestoration";
+import type { WorkspaceSearchMode } from "./workspace-tabs-state";
+
+it("preserves the live scroll position across Activity reconnections and restores cancelled search", async () => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ using _globals = { [Symbol.dispose]: () => vi.unstubAllGlobals() };
+ let liveOffset = 0;
+ let connections = 0;
+ let disconnections = 0;
+ const scroll = vi.fn((offset: number) => {
+ liveOffset = offset;
+ });
+ type ListHandle = { scrollToOffset: (offset: number) => void };
+ function List({ ref }: { ref: Ref<ListHandle> }) {
+ useImperativeHandle(ref, () => {
+ connections += 1;
+ return { scrollToOffset: scroll };
+ });
+ return (
+ <div
+ ref={(node) => {
+ if (node === null) disconnections += 1;
+ }}
+ >
+ List
+ </div>
+ );
+ }
+ function Screen(props: {
+ searchMode: WorkspaceSearchMode | undefined;
+ savedOffset: number;
+ inset: number;
+ }) {
+ const list = useRef<ListHandle>(null);
+ useSearchScrollRestoration(props.searchMode, () => {
+ list.current?.scrollToOffset(props.savedOffset - props.inset);
+ });
+ return <List ref={list} />;
+ }
+ const root = createRoot(document.createElement("div"));
+ using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
+ const input: Parameters<typeof Screen>[0] = {
+ searchMode: "resting",
+ savedOffset: 0,
+ inset: 0,
+ };
+ const render = async (visible: boolean) => {
+ await act(async () =>
+ root.render(
+ <Activity mode={visible ? "visible" : "hidden"}>
+ <Screen {...input} />
+ </Activity>,
+ ),
+ );
+ };
+
+ await render(true);
+ expect(scroll).not.toHaveBeenCalled();
+ liveOffset = 640;
+ await render(false);
+ expect(disconnections).toBeGreaterThan(0);
+ const connectionsBeforeReturn = connections;
+ await render(true);
+ expect(connections).toBeGreaterThan(connectionsBeforeReturn);
+ expect(liveOffset).toBe(640);
+ expect(scroll).not.toHaveBeenCalled();
+
+ input.searchMode = "active";
+ input.savedOffset = liveOffset;
+ await render(true);
+ input.searchMode = "expanded";
+ await render(true);
+ liveOffset = 0;
+ input.searchMode = "resting";
+ input.inset = 24;
+ await render(true);
+ expect(liveOffset).toBe(616);
+ expect(scroll.mock.calls).toEqual([[616]]);
+
+ liveOffset = 1280;
+ input.inset = 48;
+ await render(true);
+ await render(false);
+ await render(true);
+ expect(liveOffset).toBe(1280);
+ expect(scroll.mock.calls).toEqual([[616]]);
+
+ input.searchMode = undefined;
+ await render(true);
+ input.searchMode = "resting";
+ await render(true);
+ expect(scroll.mock.calls).toEqual([[616]]);
+});
diff --git a/apps/mobile/src/features/workspace/useSearchScrollRestoration.ts b/apps/mobile/src/features/workspace/useSearchScrollRestoration.ts
new file mode 100644
index 00000000000..a51ee917180
--- /dev/null
+++ b/apps/mobile/src/features/workspace/useSearchScrollRestoration.ts
@@ -0,0 +1,19 @@
+import { useEffect, useRef } from "react";
+
+import type { WorkspaceSearchMode } from "./workspace-tabs-state";
+
+export function useSearchScrollRestoration(
+ searchMode: WorkspaceSearchMode | undefined,
+ restore: () => void,
+) {
+ const previousSearchMode = useRef(searchMode);
+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- restores the native list's saved search position
+ useEffect(() => {
+ const cancelledSearch =
+ previousSearchMode.current !== undefined &&
+ previousSearchMode.current !== "resting" &&
+ searchMode === "resting";
+ previousSearchMode.current = searchMode;
+ if (cancelledSearch) restore();
+ }, [searchMode, restore]);
+}
diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts
index e0e411272e8..b64dc1c4e2a 100644
--- a/apps/mobile/vite.config.ts
+++ b/apps/mobile/vite.config.ts
@@ -37,6 +37,7 @@ const reactTests = [
"src/features/terminal/useTerminalExitNavigation.test.tsx",
"src/features/threads/use-thread-list-now.test.tsx",
"src/features/home/home-list-options.test.tsx",
+ "src/features/workspace/useSearchScrollRestoration.test.tsx",
"src/features/connection/ConnectionsNewRouteScreen.test.tsx",
];