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

android-workspace-navigation.ts

76 lines2.8 KB

1import type { NavigationState } from "@react-navigation/native";
2import { resetRootStack, type RootStackNavigation } from "./root-stack-reset";
3​
4function androidWorkspaceBackReturnsToThread(state: NavigationState) {
5 const current = state.routes[state.index];
6 const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
7 return (
8 current?.name === "Thread" &&
9 state.routes[state.index - 1]?.name === "Thread" &&
10 params !== undefined &&
11 !parentThreadIsBelow(state, params)
12 );
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 selectTab: () => void,
63) {
64 const state = navigation.getState();
65 if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
66 selectTab();
67 resetAndroidWorkspace(navigation);
69​
70// In the split view these destinations swap the detail pane beside the sidebar instead of
71// stacking over Home, so the default slide-in would show a layer that is not there.
72export function androidWorkspaceDestinationAnimation(routeName: string, usesSplitView: boolean) {
73 return usesSplitView && (routeName === "Thread" || routeName === "PullRequest")
74 ? ("none" as const)
75 : undefined;