Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

settingsScopeAxis.ts

119 lines4.4 KB

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 }
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 };
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;
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));
57​
58export const ALL_ENVIRONMENTS_VALUE = "all";
59export const ALL_PROJECTS_VALUE = "all";
60​
61export function environmentAxisValue(
62 search: SettingsScopeSearch,
63 resolvedEnvironmentId?: string | null,
64): string {
65 return search.machine ?? resolvedEnvironmentId ?? ALL_ENVIRONMENTS_VALUE;
67​
68export function projectAxisValue(search: SettingsScopeSearch): string {
69 return search.project ?? ALL_PROJECTS_VALUE;
71​
72export function projectScopeMenuGroups(
73 groups: readonly SidebarProjectSnapshot[],
74 search: Pick<SettingsScopeSearch, "machine">,
75) {
76 const { machine } = search;
77 if (machine === undefined) return groups;
78 return groups.filter((group) =>
79 group.memberProjects.some((member) => member.environmentId === machine),
80 );
82​
83export function selectEnvironmentAxis(
84 search: SettingsScopeSearch,
85 value: string,
86): SettingsScopeSearch {
87 const next: SettingsScopeSearch = {};
88 if (search.project) next.project = search.project;
89 if (value !== ALL_ENVIRONMENTS_VALUE) next.machine = value;
90 return next;
92​
93export function selectProjectAxis(search: SettingsScopeSearch, value: string): SettingsScopeSearch {
94 const next: SettingsScopeSearch = {};
95 if (value !== ALL_PROJECTS_VALUE) next.project = value;
96 if (search.machine) next.machine = search.machine;
97 return next;
99​
100export function selectSingleEnvironmentScope(
101 search: SettingsScopeSearch,
102 scope: ResolvedSettingsScope,
103 environments: readonly {
104 readonly environmentId: EnvironmentPresentation["environmentId"];
105 readonly connection: Pick<EnvironmentPresentation["connection"], "phase">;
106 }[],
107 primaryEnvironmentId: string | null,
108): SettingsScopeSearch {
109 if (search.machine || scope.kind === "unavailable") return search;
110 const selectedIds = new Set(scope.environmentIds);
111 const candidates = environments.filter((environment) =>
112 selectedIds.has(environment.environmentId),
113 );
114 const selected =
115 candidates.find((environment) => environment.environmentId === primaryEnvironmentId) ??
116 candidates.find((environment) => environment.connection.phase === "connected") ??
117 candidates[0];
118 return selected ? { ...search, machine: selected.environmentId } : search;