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

android-workspace-navigation.ts

76 lines3.2 KB

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;
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 };
46​
47/**
48 * Resets the root stack to a workspace list or detail. Always reads and targets the root
49 * navigator: the sidebar and rail render in the root layout, where `useNavigation()` is the
50 * container, and a container dispatch runs on the deepest focused navigator. With Settings
51 * or New task open, that is their nested stack, which rejects the reset.
52 */
53export function resetAndroidWorkspace(
54 navigation: WorkspaceNavigation,
55 destination?: AndroidWorkspaceDestination,
56 reviewers?: ReactNavigation.RootParamList["PullRequestReviewers"],
57) {
58 let root = navigation;
59 for (let parent = root.getParent(); parent; parent = root.getParent()) root = parent;
60 const state = root.getRootState?.() ?? root.getState();
61 if (!state) return;
62 // Same shape as `CommonActions.reset`, which would pull React Native into node tests.
63 navigation.dispatch({
64 type: "RESET",
65 payload: androidWorkspaceNavigationState(state, destination, reviewers),
66 target: state.key,
67 });
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;