Skip to content

Commit bf08e91

Browse files
committed
fix(mobile): preserve folder picker scroll on root switches
1 parent 3c1a9a4 commit bf08e91

3 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎apps/mobile/src/features/files/ThreadFilesRouteScreen.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ export function ThreadFilesTreeScreen(props: ThreadFilesRouteScreenProps) {
460460
const navigation = useNavigation();
461461
const { fileInspector, layout, showAuxiliaryPane } = useAdaptiveWorkspaceLayout();
462462
const [searchQuery, setSearchQuery] = useState("");
463+
// The iOS picker remounts with the root-specific file tree.
464+
const folderPickerScrollOffsetRef = useRef(0);
465+
const [folderPickerInitialScrollOffset, setFolderPickerInitialScrollOffset] = useState(0);
463466
const { themeAppearance: highlightTheme } = useAppearancePreferences();
464467
const { cwd, environmentId, projectName, selectedThread, threadId, roots, rootId } =
465468
useThreadFilesWorkspace(props.route.params);
@@ -592,7 +595,14 @@ export function ThreadFilesTreeScreen(props: ThreadFilesRouteScreenProps) {
592595
<WorkspaceFolderPicker
593596
roots={roots}
594597
selectedId={rootId}
595-
onSelect={(rootId) => navigation.setParams({ rootId })}
598+
onSelect={(rootId) => {
599+
setFolderPickerInitialScrollOffset(folderPickerScrollOffsetRef.current);
600+
navigation.setParams({ rootId });
601+
}}
602+
initialScrollOffset={folderPickerInitialScrollOffset}
603+
onScrollOffsetChange={(offset) => {
604+
folderPickerScrollOffsetRef.current = offset;
605+
}}
596606
/>
597607
);
598608

‎apps/mobile/src/features/files/WorkspaceFolderPicker.tsx‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
import { Pressable, ScrollView } from "react-native";
1+
import { Platform, Pressable, ScrollView } from "react-native";
22
import type { WorkspaceFolder } from "@t3tools/contracts";
33
import { AppText as Text } from "../../components/AppText";
44

55
export function WorkspaceFolderPicker(props: {
66
readonly roots: ReadonlyArray<WorkspaceFolder>;
77
readonly selectedId: string | undefined;
88
readonly onSelect: (rootId: string) => void;
9+
readonly initialScrollOffset: number;
10+
readonly onScrollOffsetChange: (offset: number) => void;
911
}) {
1012
if (!props.roots.length) return null;
1113
return (
12-
<ScrollView horizontal showsHorizontalScrollIndicator={false} className="max-h-12 px-3">
14+
<ScrollView
15+
horizontal
16+
showsHorizontalScrollIndicator={false}
17+
className="max-h-12 px-3"
18+
contentOffset={Platform.OS === "ios" ? { x: props.initialScrollOffset, y: 0 } : undefined}
19+
onScroll={
20+
Platform.OS === "ios"
21+
? (event) => {
22+
props.onScrollOffsetChange(event.nativeEvent.contentOffset.x);
23+
}
24+
: undefined
25+
}
26+
scrollEventThrottle={Platform.OS === "ios" ? 16 : undefined}
27+
>
1328
{props.roots.map((root) => (
1429
<Pressable
1530
key={root.id}

‎apps/mobile/src/features/files/thread-file-navigator-pane.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { environmentSession } from "../../state/session";
33
import { workspaceFileRoots, type EnvironmentId } from "@t3tools/contracts";
44
import { SymbolView } from "../../components/AppSymbol";
55
import { MaterialScreenContent } from "../../components/MaterialScreenContent";
6-
import { useCallback, useMemo, useState, type ComponentProps } from "react";
6+
import { useCallback, useMemo, useRef, useState, type ComponentProps } from "react";
77
import { Platform, Pressable, View, type NativeSyntheticEvent } from "react-native";
88
import {
99
Screen,
@@ -48,6 +48,9 @@ export function ThreadFileNavigatorPane(props: {
4848
roots.find((entry) => entry.path === props.cwd);
4949
const cwd = root?.path ?? props.cwd;
5050
const [searchQuery, setSearchQuery] = useState("");
51+
// The iOS picker remounts with the root-specific file tree.
52+
const folderPickerScrollOffsetRef = useRef(0);
53+
const [folderPickerInitialScrollOffset, setFolderPickerInitialScrollOffset] = useState(0);
5154
const { toggleAuxiliaryPane } = useAdaptiveWorkspaceLayout();
5255
const { themeAppearance: highlightTheme } = useAppearancePreferences();
5356
const theme = useUniwindTheme();
@@ -98,7 +101,18 @@ export function ThreadFileNavigatorPane(props: {
98101
);
99102

100103
const folderPicker = (
101-
<WorkspaceFolderPicker roots={roots} selectedId={root?.id} onSelect={setSelectedRootId} />
104+
<WorkspaceFolderPicker
105+
roots={roots}
106+
selectedId={root?.id}
107+
onSelect={(rootId) => {
108+
setFolderPickerInitialScrollOffset(folderPickerScrollOffsetRef.current);
109+
setSelectedRootId(rootId);
110+
}}
111+
initialScrollOffset={folderPickerInitialScrollOffset}
112+
onScrollOffsetChange={(offset) => {
113+
folderPickerScrollOffsetRef.current = offset;
114+
}}
115+
/>
102116
);
103117

104118
const fileTree = (

0 commit comments

Comments
 (0)