The best way to control Claude Code, Codex and more from any computer or phone

main
ts 137 lines 5.11 KB
Raw
1 import type { NavigationState, PartialState } from "@react-navigation/native";
2
3 import {
4 dispatchRootStackReset,
5 resetRootStack,
6 rootStackState,
7 type RootStackNavigation,
8 } from "../../lib/root-stack-reset";
9
10 type RootParams = ReactNavigation.RootParamList;
11
12 /** Settings routes that open as modals over whatever is on screen, not as Settings pages. */
13 const SETTINGS_MODAL_ROUTES = new Set(["SettingsLegal", "SettingsAuth", "SettingsWaitlist"]);
14
15 /** Root routes that make up the Settings tab: its list and the pages it opens. */
16 export type SettingsRouteName = Exclude<
17 Extract<keyof RootParams, `Settings${string}`>,
18 "SettingsLegal" | "SettingsAuth" | "SettingsWaitlist"
19 >;
20
21 /** A page in the Settings tab's detail column. */
22 export type SettingsPageName = Exclude<SettingsRouteName, "Settings">;
23
24 export function isSettingsRoute(name: string): boolean {
25 return name.startsWith("Settings") && !SETTINGS_MODAL_ROUTES.has(name);
26 }
27
28 /**
29 * Settings is open while its `Settings` route is under the topmost workspace page and that
30 * page is a Settings page. A Settings page pushed without it, such as a usage account opened
31 * from the Usage tab, stays a detail of the current tab.
32 */
33 export function settingsOpenIn(workspaceRoutes: readonly { readonly name: string }[]): boolean {
34 return (
35 workspaceRoutes.some((route) => route.name === "Settings") &&
36 isSettingsRoute(workspaceRoutes.at(-1)?.name ?? "")
37 );
38 }
39
40 /**
41 * A `settings/...` link names only its page, so put the Settings tab's route under it. A bare
42 * `settings` link opens `page` beside the list when the window has columns.
43 */
44 export function restoreSettingsLinkState(
45 state: PartialState<NavigationState> | undefined,
46 page: SettingsPageName | undefined,
47 ) {
48 if (!state) return state;
49 const routes = [...state.routes];
50 const list = routes.findIndex((route) => route.name === "Settings");
51 const first = routes.findIndex((route) => isSettingsRoute(route.name));
52 if (first < 0) return state;
53 if (list < 0) routes.splice(first, 0, { name: "Settings" });
54 const top = routes.at(-1);
55 if (page && top?.name === "Settings") routes.push({ name: page });
56 return routes.length === state.routes.length
57 ? state
58 : { ...state, index: routes.length - 1, routes };
59 }
60
61 type SettingsPageArgs<Name extends SettingsPageName> = undefined extends RootParams[Name]
62 ? [params?: RootParams[Name]]
63 : [params: RootParams[Name]];
64
65 type RouteLike = { readonly name: string; readonly key?: string; readonly params?: object };
66
67 /**
68 * Settings pages sit on the root stack above Home, under a `Settings` route that keeps the
69 * Settings list as the workspace's primary content. Opening a page replaces whatever was above
70 * Home, so the tab always starts from its list and Back returns there. Routes already on screen
71 * keep their keys, so reopening the open page doesn't rebuild it.
72 */
73 export function settingsNavigationState<State extends { readonly routes: readonly RouteLike[] }>(
74 state: State,
75 page: SettingsPageName | undefined,
76 params?: object,
77 key?: string,
78 ) {
79 const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
80 const list = state.routes.find((route) => route.name === "Settings") ?? { name: "Settings" };
81 const current = page
82 ? state.routes.find(
83 (route) => route.name === page && JSON.stringify(route.params) === JSON.stringify(params),
84 )
85 : undefined;
86 const routes = [
87 home,
88 list,
89 ...(page ? [current ?? (key ? { key, name: page, params } : { name: page, params })] : []),
90 ];
91 return { ...state, index: routes.length - 1, routes };
92 }
93
94 /**
95 * A Settings link opened while the app runs resets the root stack the way the Settings tab
96 * does. React Navigation's default action pushes the linked route over whatever is open.
97 */
98 export function settingsLinkAction(
99 link: PartialState<NavigationState>,
100 current: NavigationState | undefined,
101 ) {
102 const top = link.routes.at(-1);
103 if (!current || !top || top.state || !isSettingsRoute(top.name)) return undefined;
104 const page = top.name === "Settings" ? undefined : (top.name as SettingsPageName);
105 return {
106 type: "RESET" as const,
107 payload: settingsNavigationState(current, page, top.params),
108 target: current.key,
109 };
110 }
111
112 /** Opens one Settings page in the Settings tab, from anywhere outside it. */
113 export function openSettings<Name extends SettingsPageName>(
114 navigation: RootStackNavigation,
115 page: Name,
116 ...[params]: SettingsPageArgs<Name>
117 ) {
118 resetRootStack(navigation, (state) => settingsNavigationState(state, page, params));
119 }
120
121 /**
122 * Closes a Settings page that finished its job. The first page beside the list is replaced with
123 * `fallback` when the window has columns, since going back would leave the detail column empty.
124 */
125 export function leaveSettingsPage(
126 navigation: RootStackNavigation & { readonly goBack: () => void },
127 columns: boolean,
128 fallback: SettingsPageName,
129 ) {
130 const state = rootStackState(navigation);
131 const below = state?.routes[state.index - 1];
132 if (state && columns && below?.name === "Settings") {
133 dispatchRootStackReset(navigation, state, settingsNavigationState(state, fallback));
134 } else {
135 navigation.goBack();
136 }
137 }