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/**
12 * From Home the selection pushes (never replaces) so Home stays beneath the
13 * thread — collapsing back to a compact width keeps a sane back stack.
14 */
15export function resolveThreadSelectionNavigationAction(input: {
16 readonly usesSplitView: boolean;
17 readonly pathname: string;
18 readonly workspaceRouteName?: string;
19}): AdaptiveNavigationAction {
20 if (!input.usesSplitView || input.pathname === "/") {
21 return "push";
22 }
23
24 return input.workspaceRouteName === "Thread" || isBaseThreadRoute(input.pathname)
25 ? "set-params"
26 : "replace";
27}
28
29export function resolveThreadSelectionOverlayState(input: {
30 readonly state: NavigationState | undefined;
31 readonly workspaceRouteKey: string | undefined;
32 readonly action: AdaptiveNavigationAction;
33 readonly params: ReactNavigation.RootParamList["Thread"];
34}) {
35 if (input.state === undefined) return null;
36 const workspaceIndex = input.state.routes.findIndex(
37 (route) => route.key === input.workspaceRouteKey,
38 );
39 if (workspaceIndex < 0 || workspaceIndex >= input.state.index) return null;
40
41 const workspaceRoute = input.state.routes[workspaceIndex];
42 const routes = input.state.routes.slice(0, workspaceIndex + (input.action === "push" ? 1 : 0));
43 return {
44 ...input.state,
45 index: routes.length,
46 routes: [
47 ...routes,
48 input.action === "set-params" && workspaceRoute?.name === "Thread"
49 ? { ...workspaceRoute, params: input.params }
50 : { name: "Thread", params: input.params },
51 ],
52 };
53}
54
55export function resolveFileSelectionNavigationAction(input: {
56 readonly hasPersistentFileInspector: boolean;
57}): AdaptiveNavigationAction {
58 return input.hasPersistentFileInspector ? "replace" : "push";
59}