Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

android-workspace-navigation.ts

83 lines3.5 KB

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