Open History to see the last commit
1import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
2import type { EnvironmentPresentation } from "../../state/environments";
3import type { ResolvedSettingsScope, SettingsScopeSearch } from "./settingsScope";
4
5type ScopeEnvironment = Pick<EnvironmentPresentation, "environmentId" | "label" | "displayUrl">;
6
7export function settingsScopeEnvironmentAddress(environment: ScopeEnvironment) {
8 if (!environment.displayUrl) return environment.environmentId;
9 try {
10 return new URL(environment.displayUrl).host || environment.displayUrl;
11 } catch {
12 return environment.displayUrl;
13 }
14}
15
16export function settingsScopeEnvironmentLabels(
17 environment: ScopeEnvironment,
18 environments?: readonly ScopeEnvironment[],
19) {
20 const duplicates = environments?.filter(
21 (other) =>
22 other.environmentId !== environment.environmentId && other.label === environment.label,
23 );
24 const address = settingsScopeEnvironmentAddress(environment);
25 const secondary = duplicates?.some((other) => settingsScopeEnvironmentAddress(other) === address)
26 ? environment.displayUrl || environment.environmentId
27 : address;
28 return {
29 primary: environment.label,
30 secondary: environments === undefined || duplicates?.length ? secondary : "",
31 };
32}
33
34export function settingsScopeEnvironmentLabel(
35 environment: ScopeEnvironment,
36 environments: readonly ScopeEnvironment[],
37) {
38 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
39 return secondary ? `${primary} · ${secondary}` : primary;
40}
41
42export function settingsScopeProjectMachines(
43 group: SidebarProjectSnapshot,
44 environments: readonly ScopeEnvironment[],
45 includeAddresses = false,
46) {
47 const machines = group.memberProjects.map((member) => {
48 const environment = environments.find((entry) => entry.environmentId === member.environmentId);
49 if (!environment) return member.environmentLabel ?? member.environmentId;
50 const { primary, secondary } = settingsScopeEnvironmentLabels(environment, environments);
51 const address =
52 secondary || (includeAddresses ? settingsScopeEnvironmentAddress(environment) : "");
53 return includeAddresses || address ? `${primary} · ${address}` : primary;
54 });
55 return Array.from(new Set(machines));
56}
57
58export const ALL_ENVIRONMENTS_VALUE = "all";
59export const ALL_PROJECTS_VALUE = "all";
60
61/**
62 * The environment axis: `all` or an environment id. A legacy checkout link
63 * without `machine` still names one environment, which the resolver supplies.
64 */
65export function environmentAxisValue(
66 search: SettingsScopeSearch,
67 resolvedEnvironmentId?: string | null,
68): string {
69 return search.machine ?? resolvedEnvironmentId ?? ALL_ENVIRONMENTS_VALUE;
70}
71
72/** The project axis: `all` or a project key. */
73export function projectAxisValue(search: SettingsScopeSearch): string {
74 return search.project ?? ALL_PROJECTS_VALUE;
75}
76
77/** Choosing an environment keeps the project; a pre-existing checkout narrowing is dropped. */
78export function selectEnvironmentAxis(
79 search: SettingsScopeSearch,
80 value: string,
81): SettingsScopeSearch {
82 const next: SettingsScopeSearch = {};
83 if (search.project) next.project = search.project;
84 if (value !== ALL_ENVIRONMENTS_VALUE) next.machine = value;
85 return next;
86}
87
88/** Choosing a project keeps the environment axis. */
89export function selectProjectAxis(search: SettingsScopeSearch, value: string): SettingsScopeSearch {
90 const next: SettingsScopeSearch = {};
91 if (value !== ALL_PROJECTS_VALUE) next.project = value;
92 if (search.machine) next.machine = search.machine;
93 return next;
94}
95
96/** Provider configuration always belongs to one environment, including project scopes. */
97export function selectSingleEnvironmentScope(
98 search: SettingsScopeSearch,
99 scope: ResolvedSettingsScope,
100 environments: readonly {
101 readonly environmentId: EnvironmentPresentation["environmentId"];
102 readonly connection: Pick<EnvironmentPresentation["connection"], "phase">;
103 }[],
104 primaryEnvironmentId: string | null,
105): SettingsScopeSearch {
106 if (search.machine || scope.kind === "unavailable") return search;
107 const selectedIds = new Set(scope.environmentIds);
108 const candidates = environments.filter((environment) =>
109 selectedIds.has(environment.environmentId),
110 );
111 const selected =
112 candidates.find((environment) => environment.environmentId === primaryEnvironmentId) ??
113 candidates.find((environment) => environment.connection.phase === "connected") ??
114 candidates[0];
115 return selected ? { ...search, machine: selected.environmentId } : search;
116}