Open History to see the last commit
1import type { WorkspaceSelections } from "../../persistence/mobile-preferences";
2import {
3 SETTINGS_PAGE_TARGETS,
4 type SettingsPageTarget,
5} from "../settings/components/settings-page-targets";
6import { settingsNavigationState } from "../settings/settings-navigation";
7import type { WorkspaceTab } from "./workspace-tabs-state";
8
9type RouteLike = {
10 readonly name: string;
11 readonly key?: string;
12 readonly params?: object | undefined;
13};
14type ThreadSelection = NonNullable<WorkspaceSelections["threads"]>;
15type PullRequestSelection = NonNullable<WorkspaceSelections["pullRequests"]>;
16
17/** Opened when Settings has columns and none of its pages has been opened yet. */
18export const DEFAULT_SETTINGS_PAGE = "SettingsEnvironments" satisfies SettingsPageTarget;
19
20// Pages a Settings row opens, minus Project overview: it shows the project chosen in the
21// Settings scope, which resets whenever Settings closes.
22const REMEMBERED_SETTINGS_PAGES: ReadonlySet<string> = new Set(
23 SETTINGS_PAGE_TARGETS.filter((page) => page !== "SettingsProjectOverview"),
24);
25
26export function rememberedSettingsPage(selections: WorkspaceSelections): SettingsPageTarget {
27 return selections.settings && REMEMBERED_SETTINGS_PAGES.has(selections.settings)
28 ? (selections.settings as SettingsPageTarget)
29 : DEFAULT_SETTINGS_PAGE;
30}
31
32/** Whether a remembered chat or pull request can still be opened. */
33export interface WorkspaceSelectionCheck {
34 readonly thread: (selection: ThreadSelection) => boolean;
35 readonly pullRequest: (selection: PullRequestSelection) => boolean;
36}
37
38function stringParam(params: object | undefined, key: string) {
39 const value = (params as Record<string, unknown> | undefined)?.[key];
40 return typeof value === "string" ? value : undefined;
41}
42
43function pullRequestSelection(
44 params: object | undefined,
45): Omit<PullRequestSelection, "key"> | undefined {
46 const value = params as Record<string, unknown> | undefined;
47 if (
48 typeof value?.environmentId !== "string" ||
49 typeof value.projectId !== "string" ||
50 typeof value.repository !== "string" ||
51 typeof value.number !== "number"
52 ) {
53 return undefined;
54 }
55 return {
56 environmentId: value.environmentId,
57 projectId: value.projectId,
58 repository: value.repository,
59 number: value.number,
60 ...(typeof value.host === "string" ? { host: value.host } : {}),
61 };
62}
63
64function samePullRequest(
65 left: PullRequestSelection | undefined,
66 right: Omit<PullRequestSelection, "key">,
67) {
68 return (
69 left?.environmentId === right.environmentId &&
70 left.projectId === right.projectId &&
71 left.repository === right.repository &&
72 left.number === right.number &&
73 left.host === right.host
74 );
75}
76
77/**
78 * The page each tab shows in a root route chain: the newest chat, the newest pull request, and
79 * the Settings page directly above the Settings route. Returns `previous` when nothing changed.
80 */
81export function workspaceSelectionsIn(
82 routes: readonly RouteLike[],
83 previous: WorkspaceSelections,
84): WorkspaceSelections {
85 let next = previous;
86 const thread = routes.findLast((route) => route.name === "Thread");
87 const environmentId = stringParam(thread?.params, "environmentId");
88 const threadId = stringParam(thread?.params, "threadId");
89 if (
90 environmentId &&
91 threadId &&
92 (next.threads?.environmentId !== environmentId ||
93 next.threads.threadId !== threadId ||
94 next.threads.key !== thread?.key)
95 ) {
96 next = { ...next, threads: { environmentId, threadId, key: thread?.key } };
97 }
98 const pullRequestRoute = routes.findLast((route) => route.name === "PullRequest");
99 const pullRequest = pullRequestSelection(pullRequestRoute?.params);
100 if (
101 pullRequest &&
102 (!samePullRequest(next.pullRequests, pullRequest) ||
103 next.pullRequests?.key !== pullRequestRoute?.key)
104 ) {
105 next = { ...next, pullRequests: { ...pullRequest, key: pullRequestRoute?.key } };
106 }
107 const list = routes.findIndex((route) => route.name === "Settings");
108 const page = list < 0 ? undefined : routes[list + 1];
109 if (
110 page &&
111 REMEMBERED_SETTINGS_PAGES.has(page.name) &&
112 (page.name !== next.settings || page.key !== next.settingsKey)
113 ) {
114 next = { ...next, settings: page.name, settingsKey: page.key };
115 }
116 return next;
117}
118
119function identityOf({ key: _key, ...identity }: PullRequestSelection) {
120 return identity;
121}
122
123let routeKeys = 0;
124
125/** New routes get their key here, so the caller knows which route it is about to show. */
126function keyed<Route extends RouteLike>(route: Route): Route & { readonly key: string } {
127 if (route.key !== undefined) return route as Route & { readonly key: string };
128 routeKeys += 1;
129 return { ...route, key: `${route.name}-workspace-${routeKeys}` };
130}
131
132function detailRoute(
133 state: { readonly routes: readonly RouteLike[] },
134 tab: "threads" | "pullRequests",
135 selections: WorkspaceSelections,
136 check: WorkspaceSelectionCheck,
137): RouteLike | undefined {
138 if (tab === "threads") {
139 const thread = selections.threads;
140 if (!thread || !check.thread(thread)) return undefined;
141 return (
142 state.routes.find(
143 (route) =>
144 route.name === "Thread" &&
145 stringParam(route.params, "environmentId") === thread.environmentId &&
146 stringParam(route.params, "threadId") === thread.threadId,
147 ) ?? {
148 key: thread.key,
149 name: "Thread",
150 params: { environmentId: thread.environmentId, threadId: thread.threadId },
151 }
152 );
153 }
154 const pullRequest = selections.pullRequests;
155 if (!pullRequest || !check.pullRequest(pullRequest)) return undefined;
156 return (
157 state.routes.find(
158 (route) =>
159 route.name === "PullRequest" &&
160 samePullRequest(pullRequestSelection(route.params), pullRequest),
161 ) ?? { key: pullRequest.key, name: "PullRequest", params: identityOf(pullRequest) }
162 );
163}
164
165/**
166 * The root stack for opening a workspace tab. With columns the detail column must show
167 * something, so the tab reopens the page it last showed (Settings falls back to its default);
168 * without columns the tab opens on its list. Routes already on screen keep their keys, and
169 * `overlays` (open sheets) stay on top.
170 */
171export function workspaceTabNavigationState<
172 State extends { readonly routes: readonly RouteLike[] },
173>(
174 state: State,
175 tab: WorkspaceTab | "settings",
176 selections: WorkspaceSelections,
177 options: {
178 readonly columns: boolean;
179 readonly check: WorkspaceSelectionCheck;
180 readonly overlays?: readonly RouteLike[];
181 },
182) {
183 const overlays = options.overlays ?? [];
184 if (tab === "settings") {
185 const page = options.columns ? rememberedSettingsPage(selections) : undefined;
186 const settings = settingsNavigationState(
187 state,
188 page,
189 undefined,
190 page === selections.settings ? selections.settingsKey : undefined,
191 );
192 const routes = [...settings.routes.map(keyed), ...overlays];
193 return { ...settings, index: routes.length - 1, routes };
194 }
195 const home = state.routes.find((route) => route.name === "Home") ?? { name: "Home" };
196 const detail =
197 options.columns && tab !== "usage"
198 ? detailRoute(state, tab, selections, options.check)
199 : undefined;
200 const routes = [...[home, ...(detail ? [detail] : [])].map(keyed), ...overlays];
201 return { ...state, index: routes.length - 1, routes };
202}