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

main
ts 117 lines 4.95 KB
Raw
1 import type {
2 NavigationAction,
3 ParamListBase,
4 PartialState,
5 NavigationState,
6 Router,
7 StackNavigationState,
8 } from "@react-navigation/native";
9 import { workspaceRoutesOf } from "../workspace-overlay-routes";
10
11 type RootParams = ReactNavigation.RootParamList;
12
13 /** A page the workspace's detail column shows on its own: what a list row selects. */
14 export type WorkspaceDetail =
15 | { readonly name: "Thread"; readonly params: RootParams["Thread"] }
16 | { readonly name: "PullRequest"; readonly params: RootParams["PullRequest"] }
17 | { readonly name: "Issue"; readonly params: RootParams["Issue"] };
18
19 /** Android opens the reviewers sheet over the pull request in the same step. */
20 export type WorkspaceDetailSheet = {
21 readonly name: "PullRequestReviewers";
22 readonly params: RootParams["PullRequestReviewers"];
23 };
24
25 const SELECT_WORKSPACE_DETAIL = "SELECT_WORKSPACE_DETAIL";
26
27 export interface SelectWorkspaceDetailAction {
28 readonly type: typeof SELECT_WORKSPACE_DETAIL;
29 readonly payload: { readonly detail: WorkspaceDetail; readonly sheet?: WorkspaceDetailSheet };
30 }
31
32 /**
33 * Shows `detail` as the workspace's only page, the way selecting a list row does. Drilling in
34 * from a page pushes instead.
35 */
36 export function selectWorkspaceDetail(
37 detail: WorkspaceDetail,
38 sheet?: WorkspaceDetailSheet,
39 ): SelectWorkspaceDetailAction {
40 return { type: SELECT_WORKSPACE_DETAIL, payload: sheet ? { detail, sheet } : { detail } };
41 }
42
43 const IDENTITY = {
44 Thread: ["environmentId", "threadId"],
45 PullRequest: ["environmentId", "projectId", "repository", "number", "host"],
46 Issue: ["environmentId", "projectId", "tracker", "host", "scope", "issueKey"],
47 } as const;
48
49 type RouteLike = { readonly name: string; readonly key?: string; readonly params?: object };
50
51 function showsDetail(route: RouteLike, detail: WorkspaceDetail) {
52 if (route.name !== detail.name) return false;
53 const current = route.params as Readonly<Record<string, unknown>> | undefined;
54 const target = detail.params as Readonly<Record<string, unknown>> | undefined;
55 return IDENTITY[detail.name].every((key) => current?.[key] === target?.[key]);
56 }
57
58 // UIKit pops a card by native Back before the router hears of it, so a row tapped meanwhile
59 // must not reuse the card on its way out.
60 let nativelyPoppingRoute: string | null = null;
61
62 export function setNativelyPoppingWorkspaceRoute(key: string | null) {
63 nativelyPoppingRoute = key;
64 }
65
66 /**
67 * The root stack after selecting `detail`: Home, then the detail alone. The route already showing
68 * it, or else the detail column's current page of the same kind, keeps its key, so the column
69 * updates in place instead of pushing.
70 */
71 function workspaceDetailState<
72 State extends { readonly index: number; readonly routes: readonly RouteLike[] },
73 >(state: State, detail: WorkspaceDetail, sheet?: WorkspaceDetailSheet, popping?: string | null) {
74 const { workspace } = workspaceRoutesOf(state.routes.slice(0, state.index + 1));
75 const leaving = workspace.findIndex((route) => route.key !== undefined && route.key === popping);
76 const live = leaving < 0 ? workspace : workspace.slice(0, leaving);
77 const home = live.find((route) => route.name === "Home") ?? { name: "Home" };
78 const page = live.find((route) => route.name !== "Home" && route.name !== "Settings");
79 const reused =
80 live.findLast((route) => showsDetail(route, detail)) ??
81 (page?.name === detail.name ? page : undefined);
82 const routes = [
83 home,
84 reused ? { ...reused, params: detail.params } : { name: detail.name, params: detail.params },
85 ...(sheet ? [sheet] : []),
86 ];
87 return { ...state, index: routes.length - 1, routes };
88 }
89
90 /** Root stack router overrides that handle `selectWorkspaceDetail`. */
91 export function workspaceDetailRouter<Action extends NavigationAction>(
92 original: Router<StackNavigationState<ParamListBase>, Action>,
93 ): Partial<Router<StackNavigationState<ParamListBase>, Action>> {
94 return {
95 getStateForAction(state, action, options) {
96 if (action.type !== SELECT_WORKSPACE_DETAIL) {
97 return original.getStateForAction(state, action, options);
98 }
99 const { detail, sheet } = (action as unknown as SelectWorkspaceDetailAction).payload;
100 const next = workspaceDetailState(state, detail, sheet, nativelyPoppingRoute);
101 const reset = { type: "RESET", payload: { ...next, stale: false } };
102 return original.getStateForAction(state, reset as unknown as Action, options);
103 },
104 };
105 }
106
107 /** A link to a single thread, pull request or issue selects it, like its list row would. */
108 export function workspaceDetailLinkAction(
109 state: PartialState<NavigationState> | NavigationState,
110 ): SelectWorkspaceDetailAction | undefined {
111 const pages = state.routes.filter((route) => route.name !== "Home");
112 const [page] = pages;
113 if (pages.length !== 1 || !page || page.state) return undefined;
114 if (page.name !== "Thread" && page.name !== "PullRequest" && page.name !== "Issue")
115 return undefined;
116 return selectWorkspaceDetail({ name: page.name, params: page.params } as WorkspaceDetail);
117 }