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

main
ts 162 lines 5.26 KB
Raw
1 import type { EnvironmentId } from "@t3tools/contracts";
2 import { msg } from "gt-react";
3
4 import type {
5 SidebarProjectGroupMember,
6 SidebarProjectSnapshot,
7 } from "../../sidebarProjectGrouping";
8 import { m, type MessageResolver } from "../../i18n/messages";
9 import type { EnvironmentPresentation } from "../../state/environments";
10
11 export interface SettingsScopeSearch {
12 project?: string | undefined;
13 machine?: string | undefined;
14 checkout?: string | undefined;
15 }
16
17 type ScopeTargets = {
18 label: string;
19 members: readonly SidebarProjectGroupMember[];
20 environmentIds: readonly EnvironmentId[];
21 };
22
23 export type ResolvedSettingsScope = ScopeTargets &
24 (
25 | { kind: "all" }
26 | { kind: "environment"; environmentId: EnvironmentId }
27 | {
28 kind: "project";
29 group: SidebarProjectSnapshot;
30 environmentId: EnvironmentId | null;
31 }
32 | {
33 kind: "checkout";
34 group: SidebarProjectSnapshot;
35 checkout: SidebarProjectGroupMember;
36 environmentId: EnvironmentId;
37 }
38 | {
39 kind: "unavailable";
40 reason: "project-required" | "project-missing" | "environment-missing" | "checkout-missing";
41 message: string;
42 }
43 );
44
45 /** Stale IDs remain visible to the resolver so a removed target reads as unavailable, not as "all". */
46 export function validateSettingsScopeSearch(raw: Record<string, unknown>): SettingsScopeSearch {
47 const stringValue = (value: unknown) =>
48 typeof value === "string" && value.trim().length > 0 ? value : undefined;
49 const project = stringValue(raw.project);
50 const machine = stringValue(raw.machine);
51 const checkout = stringValue(raw.checkout);
52 return {
53 ...(project === undefined ? {} : { project }),
54 ...(machine === undefined ? {} : { machine }),
55 ...(checkout === undefined ? {} : { checkout }),
56 };
57 }
58
59 /** An unavailable selection never broadens a subsequent write. */
60 export function resolveSettingsScope(
61 search: SettingsScopeSearch,
62 groups: readonly SidebarProjectSnapshot[],
63 environments: readonly Pick<EnvironmentPresentation, "environmentId" | "label">[],
64 resolve: MessageResolver = m,
65 ): ResolvedSettingsScope {
66 const unavailable = (
67 reason: Extract<ResolvedSettingsScope, { kind: "unavailable" }>["reason"],
68 message: string,
69 ): ResolvedSettingsScope => ({
70 kind: "unavailable",
71 reason,
72 label: resolve(msg("Unavailable selection")),
73 message,
74 members: [],
75 environmentIds: [],
76 });
77
78 if (search.checkout && !search.project) {
79 return unavailable(
80 "project-required",
81 resolve(msg("Select a project to choose one of its checkouts.")),
82 );
83 }
84
85 const environment = environments.find((candidate) => candidate.environmentId === search.machine);
86 if (search.machine && !environment) {
87 return unavailable(
88 "environment-missing",
89 resolve(msg("This environment is no longer available.")),
90 );
91 }
92
93 if (search.project) {
94 const group = groups.find((candidate) => candidate.projectKey === search.project);
95 if (!group)
96 return unavailable("project-missing", resolve(msg("This project is no longer available.")));
97 const members = group.memberProjects.filter(
98 (member) =>
99 (search.machine === undefined || member.environmentId === search.machine) &&
100 (search.checkout === undefined || member.physicalProjectKey === search.checkout),
101 );
102 if (members.length === 0) {
103 return unavailable(
104 "checkout-missing",
105 search.checkout
106 ? resolve(
107 msg("This checkout is no longer available in the selected project and environment."),
108 )
109 : resolve(msg("This project has no checkout on this environment.")),
110 );
111 }
112 if (search.checkout) {
113 const checkout = members[0]!;
114 const checkoutEnvironment = environments.find(
115 (candidate) => candidate.environmentId === checkout.environmentId,
116 );
117 if (!checkoutEnvironment) {
118 return unavailable(
119 "environment-missing",
120 resolve(msg("This checkout's environment is no longer available.")),
121 );
122 }
123 const sharesEnvironment = group.memberProjects.some(
124 (member) =>
125 member.environmentId === checkout.environmentId &&
126 member.physicalProjectKey !== checkout.physicalProjectKey,
127 );
128 return {
129 kind: "checkout",
130 group,
131 checkout,
132 environmentId: checkout.environmentId,
133 label: `${group.displayName} / ${checkoutEnvironment.label}${sharesEnvironment ? ` · ${checkout.workspaceRoot}` : ""}`,
134 members,
135 environmentIds: [checkout.environmentId],
136 };
137 }
138 return {
139 kind: "project",
140 group,
141 environmentId: environment?.environmentId ?? null,
142 label: `${group.displayName} / ${environment?.label ?? resolve(msg("All checkouts"))}`,
143 members,
144 environmentIds: [...new Set(members.map((member) => member.environmentId))],
145 };
146 }
147 if (environment) {
148 return {
149 kind: "environment",
150 environmentId: environment.environmentId,
151 label: environment.label,
152 members: [],
153 environmentIds: [environment.environmentId],
154 };
155 }
156 return {
157 kind: "all",
158 label: resolve(msg("All environments")),
159 members: [],
160 environmentIds: environments.map((candidate) => candidate.environmentId),
161 };
162 }