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

adaptive-navigation.ts

56 lines1.9 KB

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);
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}): AdaptiveNavigationAction {
19 if (!input.usesSplitView || input.pathname === "/") {
20 return "push";
21 }
22​
23 return isBaseThreadRoute(input.pathname) ? "set-params" : "replace";
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: { ...workspaceRoute.params, ...input.params } }
47 : { name: "Thread", params: input.params },
48 ],
49 };
51​
52export function resolveFileSelectionNavigationAction(input: {
53 readonly hasPersistentFileInspector: boolean;
54}): AdaptiveNavigationAction {
55 return input.hasPersistentFileInspector ? "replace" : "push";