Open History to see the last commit
1import type { NavigationState } from "@react-navigation/native";
2
3export type AdaptiveNavigationAction = "push" | "replace" | "set-params";
4
5const BASE_THREAD_ROUTE_PATTERN = /^\/threads\/[^/]+\/[^/]+\/?$/;
6
7export function isBaseThreadRoute(pathname: string): boolean {
8 return BASE_THREAD_ROUTE_PATTERN.test(pathname) || /^\/new\/draft\/?$/.test(pathname);
9}
10
11/** Push from Home so it stays beneath Thread when split view collapses. */
12export function resolveThreadSelectionNavigationAction(input: {
13 readonly usesSplitView: boolean;
14 readonly pathname: string;
15 readonly workspaceRouteName?: string;
16}): AdaptiveNavigationAction {
17 if (!input.usesSplitView || input.pathname === "/") {
18 return "push";
19 }
20
21 return input.workspaceRouteName === "Thread" || isBaseThreadRoute(input.pathname)
22 ? "set-params"
23 : "replace";
24}
25
26export function resolveThreadSelectionOverlayState(input: {
27 readonly state: NavigationState | undefined;
28 readonly workspaceRouteKey: string | undefined;
29 readonly action: AdaptiveNavigationAction;
30 readonly params: ReactNavigation.RootParamList["Thread"];
31}) {
32 if (input.state === undefined) return null;
33 const workspaceIndex = input.state.routes.findIndex(
34 (route) => route.key === input.workspaceRouteKey,
35 );
36 if (workspaceIndex < 0 || workspaceIndex >= input.state.index) return null;
37
38 const workspaceRoute = input.state.routes[workspaceIndex];
39 const routes = input.state.routes.slice(0, workspaceIndex + (input.action === "push" ? 1 : 0));
40 return {
41 ...input.state,
42 index: routes.length,
43 routes: [
44 ...routes,
45 input.action === "set-params" && workspaceRoute?.name === "Thread"
46 ? { ...workspaceRoute, params: input.params }
47 : { name: "Thread", params: input.params },
48 ],
49 };
50}
51
52export function resolveFileSelectionNavigationAction(input: {
53 readonly hasPersistentFileInspector: boolean;
54}): AdaptiveNavigationAction {
55 return input.hasPersistentFileInspector ? "replace" : "push";
56}