Open History to see the last commit
1import type { NavigationState } from "@react-navigation/native";
2import { resetRootStack, type RootStackNavigation } from "./root-stack-reset";
3
4export type AndroidWorkspaceDestination =
5 | { readonly name: "Thread"; readonly params: ReactNavigation.RootParamList["Thread"] }
6 | { readonly name: "PullRequest"; readonly params: ReactNavigation.RootParamList["PullRequest"] };
7
8function androidWorkspaceBackReturnsToThread(state: NavigationState) {
9 const current = state.routes[state.index];
10 const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
11 return (
12 current?.name === "Thread" &&
13 state.routes[state.index - 1]?.name === "Thread" &&
14 params !== undefined &&
15 !parentThreadIsBelow(state, params)
16 );
17}
18
19export function androidWorkspaceNavigationState(
20 state: NavigationState,
21 destination?: AndroidWorkspaceDestination,
22 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
23) {
24 const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
25 if (!destination) return { ...state, index: 0, routes: [home] };
26 const identity =
27 destination.name === "Thread"
28 ? ["environmentId", "threadId"]
29 : ["environmentId", "projectId", "repository", "number", "host"];
30 const params = destination.params as Readonly<Record<string, unknown>>;
31 const existing = state.routes.findLast((route) => {
32 if (route.name !== destination.name) return false;
33 const current = route.params as Readonly<Record<string, unknown>> | undefined;
34 return identity.every((key) => current?.[key] === params[key]);
35 });
36 return {
37 ...state,
38 index: reviewers ? 2 : 1,
39 routes: [
40 home,
41 { ...existing, ...destination },
42 ...(reviewers ? [{ name: "PullRequestReviewers", params: reviewers }] : []),
43 ],
44 };
45}
46
47/** Resets the root stack to a workspace list or detail. */
48export function resetAndroidWorkspace(
49 navigation: RootStackNavigation,
50 destination?: AndroidWorkspaceDestination,
51 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
52) {
53 resetRootStack(navigation, (state) =>
54 androidWorkspaceNavigationState(state, destination, reviewers),
55 );
56}
57
58export function parentThreadIsBelow(
59 state: NavigationState,
60 parent: ReactNavigation.RootParamList["Thread"],
61) {
62 if (!parent?.environmentId || !parent.threadId) return false;
63 const previous = state.routes[state.index - 1];
64 const params = previous?.params as ReactNavigation.RootParamList["Thread"] | undefined;
65 return (
66 previous?.name === "Thread" &&
67 params?.environmentId === parent.environmentId &&
68 params?.threadId === parent.threadId
69 );
70}
71
72/**
73 * How a thread link opened from inside a thread reaches its target. `navigate` would reuse the
74 * current Thread route, so Back would skip the thread the link was in and land on the list.
75 */
76export function linkedThreadStep(
77 state: NavigationState,
78 target: ReactNavigation.RootParamList["Thread"],
79): "stay" | "pop" | "push" {
80 const params = state.routes[state.index]?.params as
81 | ReactNavigation.RootParamList["Thread"]
82 | undefined;
83 if (
84 state.routes[state.index]?.name === "Thread" &&
85 params?.environmentId === target?.environmentId &&
86 params?.threadId === target?.threadId
87 ) {
88 return "stay";
89 }
90 return parentThreadIsBelow(state, target) ? "pop" : "push";
91}
92
93export function returnToAndroidWorkspaceList(
94 navigation: RootStackNavigation & { readonly goBack: () => void },
95 selectTab: () => void,
96) {
97 const state = navigation.getState();
98 if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
99 selectTab();
100 resetAndroidWorkspace(navigation);
101}
102
103// In the split view these destinations swap the detail pane beside the sidebar instead of
104// stacking over Home, so the default slide-in would show a layer that is not there.
105export function androidWorkspaceDestinationAnimation(routeName: string, usesSplitView: boolean) {
106 return usesSplitView && (routeName === "Thread" || routeName === "PullRequest")
107 ? ("none" as const)
108 : undefined;
109}