Open History to see the last commit
1import type { CommonActions, NavigationState } from "@react-navigation/native";
2
3export type AndroidWorkspaceDestination =
4 | { readonly name: "Thread"; readonly params: ReactNavigation.RootParamList["Thread"] }
5 | { readonly name: "PullRequest"; readonly params: ReactNavigation.RootParamList["PullRequest"] };
6
7export type AndroidWorkspaceResetAction = ReturnType<typeof CommonActions.reset> & {
8 readonly target: string;
9};
10
11interface WorkspaceNavigation {
12 readonly dispatch: (action: AndroidWorkspaceResetAction) => void;
13 readonly getParent: () => WorkspaceNavigation | undefined;
14 readonly getState: () => NavigationState | undefined;
15 /** Only on the navigation container, whose `getState` stays empty until the first action. */
16 readonly getRootState?: () => NavigationState | undefined;
17}
18
19function androidWorkspaceBackReturnsToThread(state: NavigationState) {
20 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)
27 );
28}
29
30export function androidWorkspaceNavigationState(
31 state: NavigationState,
32 destination?: AndroidWorkspaceDestination,
33 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
34) {
35 const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
36 if (!destination) return { ...state, index: 0, routes: [home] };
37 const identity =
38 destination.name === "Thread"
39 ? ["environmentId", "threadId"]
40 : ["environmentId", "projectId", "repository", "number", "host"];
41 const params = destination.params as Readonly<Record<string, unknown>>;
42 const existing = state.routes.findLast((route) => {
43 if (route.name !== destination.name) return false;
44 const current = route.params as Readonly<Record<string, unknown>> | undefined;
45 return identity.every((key) => current?.[key] === params[key]);
46 });
47 return {
48 ...state,
49 index: reviewers ? 2 : 1,
50 routes: [
51 home,
52 { ...existing, ...destination },
53 ...(reviewers ? [{ name: "PullRequestReviewers", params: reviewers }] : []),
54 ],
55 };
56}
57
58/**
59 * Resets the root stack to a workspace list or detail. Always reads and targets the root
60 * navigator: the sidebar and rail render in the root layout, where `useNavigation()` is the
61 * container, and a container dispatch runs on the deepest focused navigator. With Settings
62 * or New task open, that is their nested stack, which rejects the reset.
63 */
64export function resetAndroidWorkspace(
65 navigation: WorkspaceNavigation,
66 destination?: AndroidWorkspaceDestination,
67 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
68) {
69 let root = navigation;
70 for (let parent = root.getParent(); parent; parent = root.getParent()) root = parent;
71 const state = root.getRootState?.() ?? root.getState();
72 if (!state) return;
73 // Same shape as `CommonActions.reset`, which would pull React Native into node tests.
74 navigation.dispatch({
75 type: "RESET",
76 payload: androidWorkspaceNavigationState(state, destination, reviewers),
77 target: state.key,
78 });
79}
80
81export function parentThreadIsBelow(
82 state: NavigationState,
83 parent: ReactNavigation.RootParamList["Thread"],
84) {
85 const previous = state.routes[state.index - 1];
86 const params = previous?.params as ReactNavigation.RootParamList["Thread"] | undefined;
87 return (
88 previous?.name === "Thread" &&
89 params?.environmentId === parent.environmentId &&
90 params.threadId === parent.threadId
91 );
92}
93
94export function returnToAndroidWorkspaceList(
95 navigation: WorkspaceNavigation & { readonly goBack: () => void },
96 selectTab: () => void,
97) {
98 const state = navigation.getState();
99 if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
100 selectTab();
101 resetAndroidWorkspace(navigation);
102}
103
104// In the split view these destinations swap the detail pane beside the sidebar instead of
105// stacking over Home, so the default slide-in would show a layer that is not there.
106export function androidWorkspaceDestinationAnimation(routeName: string, usesSplitView: boolean) {
107 return usesSplitView && (routeName === "Thread" || routeName === "PullRequest")
108 ? ("none" as const)
109 : undefined;
110}