Open History to see the last commit
1import type { NavigationState } from "@react-navigation/native";
2import { isSettingsRoute } from "../features/settings/settings-navigation";
3import { resetRootStack, type RootStackNavigation } from "./root-stack-reset";
4
5function androidWorkspaceBackReturnsToThread(state: NavigationState) {
6 const current = state.routes[state.index];
7 const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
8 return (
9 current?.name === "Thread" &&
10 state.routes[state.index - 1]?.name === "Thread" &&
11 params !== undefined &&
12 !parentThreadIsBelow(state, params)
13 );
14}
15
16/** The root stack with only the workspace list. */
17export function androidWorkspaceNavigationState(state: NavigationState) {
18 const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
19 return { ...state, index: 0, routes: [home] };
20}
21
22export function resetAndroidWorkspace(navigation: RootStackNavigation) {
23 resetRootStack(navigation, androidWorkspaceNavigationState);
24}
25
26export function parentThreadIsBelow(
27 state: NavigationState,
28 parent: ReactNavigation.RootParamList["Thread"],
29) {
30 if (!parent?.environmentId || !parent.threadId) return false;
31 const previous = state.routes[state.index - 1];
32 const params = previous?.params as ReactNavigation.RootParamList["Thread"] | undefined;
33 return (
34 previous?.name === "Thread" &&
35 params?.environmentId === parent.environmentId &&
36 params?.threadId === parent.threadId
37 );
38}
39
40/**
41 * How a thread link opened from inside a thread reaches its target. `navigate` would reuse the
42 * current Thread route, so Back would skip the thread the link was in and land on the list.
43 */
44export function linkedThreadStep(
45 state: NavigationState,
46 target: ReactNavigation.RootParamList["Thread"],
47): "stay" | "pop" | "push" {
48 const params = state.routes[state.index]?.params as
49 | ReactNavigation.RootParamList["Thread"]
50 | undefined;
51 if (
52 state.routes[state.index]?.name === "Thread" &&
53 params?.environmentId === target?.environmentId &&
54 params?.threadId === target?.threadId
55 ) {
56 return "stay";
57 }
58 return parentThreadIsBelow(state, target) ? "pop" : "push";
59}
60
61export function returnToAndroidWorkspaceList(
62 navigation: RootStackNavigation & { readonly goBack: () => void },
63 selectTab: () => void,
64) {
65 const state = navigation.getState();
66 if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
67 selectTab();
68 resetAndroidWorkspace(navigation);
69}
70
71// Split view: Thread, PullRequest, and a Settings page picked from the list swap the detail pane
72// rather than stack, so they don't animate. Pages opened from a Settings page crossfade inside the
73// pane's card; the default push edge-extends the incoming page, bleeding a stripe beside the list.
74export function androidWorkspaceDestinationAnimation(
75 route: { readonly key: string; readonly name: string },
76 routes: readonly { readonly key: string; readonly name: string }[],
77 usesSplitView: boolean,
78) {
79 if (!usesSplitView) return undefined;
80 if (route.name === "Thread" || route.name === "PullRequest") return "none" as const;
81 if (route.name === "Settings" || !isSettingsRoute(route.name)) return undefined;
82 const below = routes[routes.findIndex((candidate) => candidate.key === route.key) - 1];
83 return below?.name === "Settings" ? ("none" as const) : ("fade" as const);
84}