Open History to see the last commit
1import type { EnvironmentId } from "@t3tools/contracts";
2import { msg } from "gt-react";
3
4import type {
5 SidebarProjectGroupMember,
6 SidebarProjectSnapshot,
7} from "../../sidebarProjectGrouping";
8import { m, type MessageResolver } from "../../i18n/messages";
9import type { EnvironmentPresentation } from "../../state/environments";
10
11export interface SettingsScopeSearch {
12 project?: string | undefined;
13 machine?: string | undefined;
14 checkout?: string | undefined;
15}
16
17type ScopeTargets = {
18 label: string;
19 members: readonly SidebarProjectGroupMember[];
20 environmentIds: readonly EnvironmentId[];
21};
22
23export 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". */
46export 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 // Keep invalid present keys as explicit undefined so they override raw parent search values
54 // without forging keys that retainSettingsScope would mistake for an explicit scope reset.
55 ...(project !== undefined || Object.hasOwn(raw, "project") ? { project } : {}),
56 ...(machine !== undefined || Object.hasOwn(raw, "machine") ? { machine } : {}),
57 ...(checkout !== undefined || Object.hasOwn(raw, "checkout") ? { checkout } : {}),
58 };
59}
60
61/** An unavailable selection never broadens a subsequent write. */
62export function resolveSettingsScope(
63 search: SettingsScopeSearch,
64 groups: readonly SidebarProjectSnapshot[],
65 environments: readonly Pick<EnvironmentPresentation, "environmentId" | "label">[],
66 resolve: MessageResolver = m,
67): ResolvedSettingsScope {
68 const unavailable = (
69 reason: Extract<ResolvedSettingsScope, { kind: "unavailable" }>["reason"],
70 message: string,
71 ): ResolvedSettingsScope => ({
72 kind: "unavailable",
73 reason,
74 label: resolve(msg("Unavailable selection")),
75 message,
76 members: [],
77 environmentIds: [],
78 });
79
80 if (search.checkout && !search.project) {
81 return unavailable(
82 "project-required",
83 resolve(msg("Select a project to choose one of its checkouts.")),
84 );
85 }
86
87 const environment = environments.find((candidate) => candidate.environmentId === search.machine);
88 if (search.machine && !environment) {
89 return unavailable(
90 "environment-missing",
91 resolve(msg("This environment is no longer available.")),
92 );
93 }
94
95 if (search.project) {
96 const group = groups.find((candidate) => candidate.projectKey === search.project);
97 if (!group)
98 return unavailable("project-missing", resolve(msg("This project is no longer available.")));
99 const members = group.memberProjects.filter(
100 (member) =>
101 (search.machine === undefined || member.environmentId === search.machine) &&
102 (search.checkout === undefined || member.physicalProjectKey === search.checkout),
103 );
104 if (members.length === 0) {
105 return unavailable(
106 "checkout-missing",
107 search.checkout
108 ? resolve(
109 msg("This checkout is no longer available in the selected project and environment."),
110 )
111 : resolve(msg("This project has no checkout on this environment.")),
112 );
113 }
114 if (search.checkout) {
115 const checkout = members[0]!;
116 const checkoutEnvironment = environments.find(
117 (candidate) => candidate.environmentId === checkout.environmentId,
118 );
119 if (!checkoutEnvironment) {
120 return unavailable(
121 "environment-missing",
122 resolve(msg("This checkout's environment is no longer available.")),
123 );
124 }
125 const sharesEnvironment = group.memberProjects.some(
126 (member) =>
127 member.environmentId === checkout.environmentId &&
128 member.physicalProjectKey !== checkout.physicalProjectKey,
129 );
130 return {
131 kind: "checkout",
132 group,
133 checkout,
134 environmentId: checkout.environmentId,
135 label: `${group.displayName} / ${checkoutEnvironment.label}${sharesEnvironment ? ` · ${checkout.workspaceRoot}` : ""}`,
136 members,
137 environmentIds: [checkout.environmentId],
138 };
139 }
140 return {
141 kind: "project",
142 group,
143 environmentId: environment?.environmentId ?? null,
144 label: `${group.displayName} / ${environment?.label ?? resolve(msg("All checkouts"))}`,
145 members,
146 environmentIds: [...new Set(members.map((member) => member.environmentId))],
147 };
148 }
149 if (environment) {
150 return {
151 kind: "environment",
152 environmentId: environment.environmentId,
153 label: environment.label,
154 members: [],
155 environmentIds: [environment.environmentId],
156 };
157 }
158 return {
159 kind: "all",
160 label: resolve(msg("All environments")),
161 members: [],
162 environmentIds: environments.map((candidate) => candidate.environmentId),
163 };
164}