Open History to see the last commit
1import type { NavigationState, PartialState } from "@react-navigation/native";
2
3import {
4 dispatchRootStackReset,
5 resetRootStack,
6 rootStackState,
7 type RootStackNavigation,
8} from "../../lib/root-stack-reset";
9
10type RootParams = ReactNavigation.RootParamList;
11
12/** Settings routes that open as modals over whatever is on screen, not as Settings pages. */
13const 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. */
16export 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. */
22export type SettingsPageName = Exclude<SettingsRouteName, "Settings">;
23
24export 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 */
33export 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 */
44export 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
61type SettingsPageArgs<Name extends SettingsPageName> = undefined extends RootParams[Name]
62 ? [params?: RootParams[Name]]
63 : [params: RootParams[Name]];
64
65type 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 */
73export 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/** Opens one Settings page in the Settings tab, from anywhere outside it. */
95export function openSettings<Name extends SettingsPageName>(
96 navigation: RootStackNavigation,
97 page: Name,
98 ...[params]: SettingsPageArgs<Name>
99) {
100 resetRootStack(navigation, (state) => settingsNavigationState(state, page, params));
101}
102
103/**
104 * Closes a Settings page that finished its job. The first page beside the list is replaced with
105 * `fallback` when the window has columns, since going back would leave the detail column empty.
106 */
107export function leaveSettingsPage(
108 navigation: RootStackNavigation & { readonly goBack: () => void },
109 columns: boolean,
110 fallback: SettingsPageName,
111) {
112 const state = rootStackState(navigation);
113 const below = state?.routes[state.index - 1];
114 if (state && columns && below?.name === "Settings") {
115 dispatchRootStackReset(navigation, state, settingsNavigationState(state, fallback));
116 } else {
117 navigation.goBack();
118 }
119}