Skip to content

Commit 2b2f7f7

Browse files
committed
fix(mobile): keep contextual Back alongside subagent parent Back
Main now pops Back to a subagent thread's parent and resets to the list otherwise. Back also returns to the chat or PR a detail was opened from, and a copy of the current detail below it still resets to the list. A contextual detail keeps the tab it was opened from.
1 parent 926808a commit 2b2f7f7

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎apps/mobile/src/features/workspace/useAndroidWorkspaceNavigation.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@ export function useWorkspaceBackNavigation(tab: WorkspaceTab) {
3232
}
3333

3434
export function useAndroidWorkspaceBack(tab: WorkspaceTab) {
35+
const navigation = useNavigation();
3536
const dispatch = useWorkspaceTabs()?.dispatch;
3637
const goBack = useWorkspaceBackNavigation(tab);
3738
useFocusEffect(
3839
useCallback(() => {
3940
if (Platform.OS !== "android") return;
40-
dispatch?.({ type: "select", tab });
41+
// A chat opened from a PR, or a PR from a chat, keeps the tab it was opened from.
42+
if ((navigation.getState()?.index ?? 0) <= 1) dispatch?.({ type: "select", tab });
4143
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
4244
if (Keyboard.isVisible()) Keyboard.dismiss();
4345
else goBack();
4446
return true;
4547
});
4648
return () => subscription.remove();
47-
}, [goBack, dispatch, tab]),
49+
}, [navigation, goBack, dispatch, tab]),
4850
);
4951
return goBack;
5052
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@ interface WorkspaceNavigation {
1616
readonly getRootState?: () => NavigationState | undefined;
1717
}
1818

19-
function androidWorkspaceBackReturnsToThread(state: NavigationState) {
19+
const detailIdentity = {
20+
Thread: ["environmentId", "threadId"],
21+
PullRequest: ["environmentId", "projectId", "repository", "number", "host"],
22+
} as const;
23+
24+
// Back returns to the detail a chat, PR, or subagent was opened from. A copy of the current
25+
// detail directly below it, which confirming a Git action from the inline inspector can leave,
26+
// is not a destination, so Back returns to the list instead.
27+
function androidWorkspaceBackReturnsToDetail(state: NavigationState) {
2028
const current = state.routes[state.index];
21-
const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
22-
return (
23-
current?.name === "Thread" &&
24-
state.routes[state.index - 1]?.name === "Thread" &&
25-
params !== undefined &&
26-
!parentThreadIsBelow(state, params)
29+
const previous = state.routes[state.index - 1];
30+
if (!current || (previous?.name !== "Thread" && previous?.name !== "PullRequest")) return false;
31+
if (current.name !== previous.name) return true;
32+
const currentParams = current.params as Readonly<Record<string, unknown>> | undefined;
33+
const previousParams = previous.params as Readonly<Record<string, unknown>> | undefined;
34+
return detailIdentity[previous.name].some(
35+
(key) => currentParams?.[key] !== previousParams?.[key],
2736
);
2837
}
2938

@@ -34,10 +43,7 @@ export function androidWorkspaceNavigationState(
3443
) {
3544
const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
3645
if (!destination) return { ...state, index: 0, routes: [home] };
37-
const identity =
38-
destination.name === "Thread"
39-
? ["environmentId", "threadId"]
40-
: ["environmentId", "projectId", "repository", "number", "host"];
46+
const identity = detailIdentity[destination.name];
4147
const params = destination.params as Readonly<Record<string, unknown>>;
4248
const existing = state.routes.findLast((route) => {
4349
if (route.name !== destination.name) return false;
@@ -102,7 +108,7 @@ export function returnToAndroidWorkspaceList(
102108
selectTab: () => void,
103109
) {
104110
const state = navigation.getState();
105-
if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
111+
if (state && androidWorkspaceBackReturnsToDetail(state)) return navigation.goBack();
106112
selectTab();
107113
resetAndroidWorkspace(navigation);
108114
}

0 commit comments

Comments
 (0)