Skip to content

Commit 16f73fc

Browse files
bitmvkclaude
andcommitted
fix(mobile): Back from a PR opened in a chat returns to the chat on Android
Android opened PR, chat and stacked-PR links by replacing the workspace stack, and PR/chat pages switched the list tab when focused, so Back landed on the PR list. Links now push like on iOS, Back follows history (skipping duplicate thread copies), and the tab only changes on explicit tab or list selection. Fixes #24753 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent dac8b11 commit 16f73fc

10 files changed

Lines changed: 44 additions & 79 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ function usePullRequestToolbarHeight(bottomInset: number) {
337337
}
338338

339339
export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestParams>) {
340-
const returnToList = useAndroidWorkspaceBack("pullRequests");
340+
const returnToList = useAndroidWorkspaceBack();
341341
const { layout } = useAdaptiveWorkspaceLayout();
342342
const sidebarHeaderItem = useWorkspaceSidebarHeaderItem();
343343
const navigation = useNavigation();

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { StackActions, useNavigation } from "@react-navigation/native";
22
import { useState } from "react";
3-
import { Platform, View } from "react-native";
4-
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
3+
import { View } from "react-native";
54
import { AppText as Text } from "../../components/ui/AppText";
65
import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls";
76
import { pullRequestStatus } from "./pullRequestStatus";
@@ -14,7 +13,6 @@ export function PullRequestStackCard(props: {
1413
readonly state: PullRequestStackState;
1514
}) {
1615
const navigation = useNavigation();
17-
const selectDetail = useSelectWorkspaceDetail();
1816
const [expanded, setExpanded] = useState(false);
1917
const stack = props.state.stack;
2018
if (!stack) {
@@ -67,9 +65,7 @@ export function PullRequestStackCard(props: {
6765
? undefined
6866
: () => {
6967
const params = { ...props.params, number: layer.number };
70-
if (Platform.OS === "android")
71-
selectDetail({ name: "PullRequest", params });
72-
else navigation.dispatch(StackActions.push("PullRequest", params));
68+
navigation.dispatch(StackActions.push("PullRequest", params));
7369
}
7470
}
7571
last={index === stack.layers.length - 1}

‎apps/mobile/src/features/pull-requests/usePullRequestThreads.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { formatComposerContextReference } from "@t3tools/shared/composerContextR
1919
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2020
import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests";
2121
import { useRef, useState } from "react";
22-
import { Platform } from "react-native";
23-
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
2422
import { pullRequestComposerContext } from "../../lib/composerContext";
2523
import { scopedThreadKey } from "../../lib/scopedEntities";
2624
import { uuidv4 } from "../../lib/uuid";
@@ -76,16 +74,13 @@ export function usePullRequestThreads(
7674
const canAsk = !connected || canOperate;
7775
const canCheckout = useEnvironmentScope(target.environmentId, AuthSourceControlWriteScope);
7876
const navigation = useNavigation();
79-
const selectDetail = useSelectWorkspaceDetail();
8077
const pending = useRef(false);
8178
const previousPrompts = useRef(new Map<string, string>());
8279
const openThread = (thread: LinkedThread) => {
8380
openThreadRoute({ environmentId: params.environmentId, threadId: thread.id });
8481
};
85-
// Android shows one page at a time; iOS opens the thread over the pull request, so Back returns.
8682
const openThreadRoute = (destination: ReactNavigation.RootParamList["Thread"]) => {
87-
if (Platform.OS === "android") selectDetail({ name: "Thread", params: destination });
88-
else navigation.dispatch(StackActions.push("Thread", destination));
83+
navigation.dispatch(StackActions.push("Thread", destination));
8984
};
9085
const [busy, setBusy] = useState(false);
9186
const ask = async (thread?: LinkedThread, task: PullRequestAgentTask = "ask") => {

‎apps/mobile/src/features/threads/NewTaskDraftScreen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export function NewTaskDraftScreen(props: {
157157
const flow = useNewTaskFlow();
158158
const navigation = useNavigation();
159159
const isFocused = useIsFocused();
160-
const goBack = useWorkspaceBackNavigation("threads");
160+
const goBack = useWorkspaceBackNavigation();
161161
const currentRoute = useRoute();
162162
const flowKey = newTaskFlowKey(currentRoute);
163163
const { getShare, isLoading: isIncomingShareInboxLoading } = useIncomingShare();

‎apps/mobile/src/features/threads/ThreadHeader.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function ThreadHeader(
1919
readonly onOpenFilesInspector: () => void;
2020
},
2121
) {
22-
const goBack = useWorkspaceBackNavigation("threads");
22+
const goBack = useWorkspaceBackNavigation();
2323
const { layout, panes, toggleAuxiliaryPane, composerKeyboardActive } =
2424
useAdaptiveWorkspaceLayout();
2525
const { onOpenTerminal, onMergeBack } = props.gitControls;

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ function firstRouteParam(value: string | string[] | undefined): string | null {
126126
}
127127

128128
function ThreadRouteFallbackLayout(props: { readonly children: ReactNode }) {
129-
const goBack = useWorkspaceBackNavigation("threads");
129+
const goBack = useWorkspaceBackNavigation();
130130
const { layout } = useAdaptiveWorkspaceLayout();
131131
const { themeVariables } = useAppearancePreferences();
132132
if (Platform.OS !== "android")
@@ -239,7 +239,7 @@ export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
239239
}
240240

241241
function LiveThreadRouteController(props: ThreadRouteScreenProps) {
242-
useAndroidWorkspaceBack("threads");
242+
useAndroidWorkspaceBack();
243243
const connectionsReady = useConnectionsReady();
244244
const { connectionState } = useRemoteConnectionStatus();
245245
const { selectedThread } = useThreadSelection();
Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,29 @@
11
import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native";
22
import { useCallback } from "react";
33
import { BackHandler, Keyboard, Platform } from "react-native";
4-
import {
5-
resetAndroidWorkspace,
6-
returnToAndroidWorkspaceList,
7-
} from "../../lib/android-workspace-navigation";
8-
import { useWorkspaceTabs } from "./WorkspaceTabsContext";
9-
import type { WorkspaceTab } from "./workspace-tabs-state";
4+
import { returnToAndroidWorkspaceList } from "../../lib/android-workspace-navigation";
105

11-
function useWorkspaceListNavigation(tab: WorkspaceTab) {
6+
export function useWorkspaceBackNavigation() {
127
const navigation = useNavigation();
13-
const dispatch = useWorkspaceTabs()?.dispatch;
148
return () => {
15-
if (Platform.OS === "android") {
16-
dispatch?.({ type: "select", tab });
17-
resetAndroidWorkspace(navigation);
18-
} else if (navigation.canGoBack()) navigation.goBack();
9+
if (Platform.OS === "android") returnToAndroidWorkspaceList(navigation);
10+
else if (navigation.canGoBack()) navigation.goBack();
1911
else navigation.dispatch(StackActions.replace("Home"));
2012
};
2113
}
2214

23-
export function useWorkspaceBackNavigation(tab: WorkspaceTab) {
24-
const navigation = useNavigation();
25-
const dispatch = useWorkspaceTabs()?.dispatch;
26-
const returnToList = useWorkspaceListNavigation(tab);
27-
return () => {
28-
if (Platform.OS === "android") {
29-
returnToAndroidWorkspaceList(navigation, () => dispatch?.({ type: "select", tab }));
30-
} else returnToList();
31-
};
32-
}
33-
34-
export function useAndroidWorkspaceBack(tab: WorkspaceTab) {
35-
const dispatch = useWorkspaceTabs()?.dispatch;
36-
const goBack = useWorkspaceBackNavigation(tab);
15+
export function useAndroidWorkspaceBack() {
16+
const goBack = useWorkspaceBackNavigation();
3717
useFocusEffect(
3818
useCallback(() => {
3919
if (Platform.OS !== "android") return;
40-
dispatch?.({ type: "select", tab });
4120
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
4221
if (Keyboard.isVisible()) Keyboard.dismiss();
4322
else goBack();
4423
return true;
4524
});
4625
return () => subscription.remove();
47-
}, [goBack, dispatch, tab]),
26+
}, [goBack]),
4827
);
4928
return goBack;
5029
}

‎apps/mobile/src/lib/android-workspace-navigation.test.ts‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,12 @@ function names(state: ReturnType<typeof router.getInitialState>) {
8585
}
8686

8787
describe("Android workspace navigation", () => {
88-
it("returns to a distinct previous thread, otherwise to the workspace list", () => {
88+
it("returns to the previous page, otherwise to the workspace list", () => {
8989
const parent = withThread();
9090
let state = apply(
9191
parent,
9292
StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
9393
);
94-
let listSelected = false;
9594
const navigation = {
9695
dispatch: (action: RootStackResetAction) => {
9796
state = apply(state, action);
@@ -102,39 +101,41 @@ describe("Android workspace navigation", () => {
102101
state = apply(state, StackActions.pop());
103102
},
104103
};
105-
const back = () =>
106-
returnToAndroidWorkspaceList(navigation, () => {
107-
listSelected = true;
108-
});
104+
const back = () => returnToAndroidWorkspaceList(navigation);
109105

110106
back();
111107
expect(state.routes).toEqual(parent.routes);
112108
expect(state.index).toBe(parent.index);
113-
expect(listSelected).toBe(false);
114109

115110
back();
116111
expect(state.routes).toEqual([parent.routes[0]]);
117112
expect(state.index).toBe(0);
118-
expect(listSelected).toBe(true);
119113

120114
state = apply(
121115
parent,
122116
StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
123117
);
124-
listSelected = false;
125118
back();
126119
expect(state.routes).toEqual(parent.routes);
127120
expect(state.index).toBe(parent.index);
128-
expect(listSelected).toBe(false);
121+
122+
const pullRequestOverThread = legacyHistory();
123+
state = pullRequestOverThread;
124+
back();
125+
expect(state.routes).toEqual(pullRequestOverThread.routes.slice(0, 2));
129126

130127
// Confirming a Git action from the inline inspector can duplicate the current thread.
131128
state = apply(parent, StackActions.push("Thread", thread.params));
132129
back();
133-
expect({ routes: names(state), index: state.index, listSelected }).toEqual({
134-
routes: ["Home"],
135-
index: 0,
136-
listSelected: true,
137-
});
130+
expect({ routes: names(state), index: state.index }).toEqual({ routes: ["Home"], index: 0 });
131+
132+
const child = { ...thread.params, threadId: "child-a" };
133+
state = apply(
134+
apply(parent, StackActions.push("Thread", child)),
135+
StackActions.push("Thread", child),
136+
);
137+
back();
138+
expect(state.routes).toEqual(parent.routes);
138139
});
139140

140141
it("opens a linked thread above the current one so Back returns to it", () => {

‎apps/mobile/src/lib/android-workspace-navigation.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import type { NavigationState } from "@react-navigation/native";
22
import { resetRootStack, type RootStackNavigation } from "./root-stack-reset";
33

4-
function androidWorkspaceBackReturnsToThread(state: NavigationState) {
5-
const current = state.routes[state.index];
6-
const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
7-
return (
8-
current?.name === "Thread" &&
9-
state.routes[state.index - 1]?.name === "Thread" &&
10-
params !== undefined &&
11-
!parentThreadIsBelow(state, params)
12-
);
4+
// Back follows history; Git confirmations can stack copies of the current thread, so skip those too.
5+
function androidWorkspaceBackTarget(state: NavigationState) {
6+
let index = state.index;
7+
const params = state.routes[index]?.params as ReactNavigation.RootParamList["Thread"] | undefined;
8+
if (state.routes[index]?.name === "Thread" && params) {
9+
while (parentThreadIsBelow({ ...state, index }, params)) index -= 1;
10+
}
11+
return index - 1;
1312
}
1413

1514
/** The root stack with only the workspace list. */
@@ -59,12 +58,12 @@ export function linkedThreadStep(
5958

6059
export function returnToAndroidWorkspaceList(
6160
navigation: RootStackNavigation & { readonly goBack: () => void },
62-
selectTab: () => void,
6361
) {
6462
const state = navigation.getState();
65-
if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
66-
selectTab();
67-
resetAndroidWorkspace(navigation);
63+
const index = state ? androidWorkspaceBackTarget(state) : -1;
64+
if (!state || index < 0) return resetAndroidWorkspace(navigation);
65+
if (index === state.index - 1) return navigation.goBack();
66+
resetRootStack(navigation, () => ({ ...state, index, routes: state.routes.slice(0, index + 1) }));
6867
}
6968

7069
// In the split view these destinations swap the detail pane beside the sidebar instead of

‎apps/mobile/src/native/useOpenPullRequest.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { useNavigation, useRoute, StackActions } from "@react-navigation/native";
22
import { nativePullRequestDestination } from "../lib/pullRequestNavigation";
3-
import { Platform } from "react-native";
4-
import { useSelectWorkspaceDetail } from "../features/workspace/useSelectWorkspaceDetail";
53
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
64
import { useProjects, useThreadShell } from "../state/entities";
75
import { tryOpenExternalUrl } from "../lib/openExternalUrl";
@@ -13,7 +11,6 @@ type PullRequestContext = { readonly environmentId: string; readonly projectId:
1311
// Avoid useRoute so layouts without route context can open any thread's pull requests.
1412
export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) {
1513
const navigation = useNavigation();
16-
const selectDetail = useSelectWorkspaceDetail();
1714
const projects = useProjects();
1815
const replace = options?.replace;
1916
return async (url: string, context: PullRequestContext | null) => {
@@ -23,9 +20,7 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean
2320
serverConfigs.has(environmentId),
2421
);
2522
if (!params) return tryOpenExternalUrl(url, "pull-request");
26-
// Android shows one page at a time; iOS opens it over the current page, so Back returns.
27-
if (Platform.OS === "android") selectDetail({ name: "PullRequest", params });
28-
else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
23+
if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
2924
else navigation.dispatch(StackActions.push("PullRequest", params));
3025
return true;
3126
};

0 commit comments

Comments
 (0)