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 if (!parent?.environmentId || !parent.threadId) return false;
86 const previous = state.routes[state.index - 1];
87 const params = previous?.params as ReactNavigation.RootParamList["Thread"] | undefined;
88 return (
89 previous?.name === "Thread" &&
90 params?.environmentId === parent.environmentId &&
91 params?.threadId === parent.threadId
92 );
93}
94
95/**
96 * How a thread link opened from inside a thread reaches its target. `navigate` would reuse the
97 * current Thread route, so Back would skip the thread the link was in and land on the list.
98 */
99export function linkedThreadStep(
100 state: NavigationState,
101 target: ReactNavigation.RootParamList["Thread"],
102): "stay" | "pop" | "push" {
103 const params = state.routes[state.index]?.params as
104 | ReactNavigation.RootParamList["Thread"]
105 | undefined;
106 if (
107 state.routes[state.index]?.name === "Thread" &&
108 params?.environmentId === target?.environmentId &&
109 params?.threadId === target?.threadId
110 ) {
111 return "stay";
112 }
113 return parentThreadIsBelow(state, target) ? "pop" : "push";
114}
115
116export function returnToAndroidWorkspaceList(
117 navigation: WorkspaceNavigation & { readonly goBack: () => void },
118 selectTab: () => void,
119) {
120 const state = navigation.getState();
121 if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
122 selectTab();
123 resetAndroidWorkspace(navigation);
124}
125
126// In the split view these destinations swap the detail pane beside the sidebar instead of
127// stacking over Home, so the default slide-in would show a layer that is not there.
128export function androidWorkspaceDestinationAnimation(routeName: string, usesSplitView: boolean) {
129 return usesSplitView && (routeName === "Thread" || routeName === "PullRequest")
130 ? ("none" as const)
131 : undefined;
132}