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

settingsScopeAxis.ts

108 lines4.0 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 selectEnvironmentAxis(
73 search: SettingsScopeSearch,
74 value: string,
75): SettingsScopeSearch {
76 const next: SettingsScopeSearch = {};
77 if (search.project) next.project = search.project;
78 if (value !== ALL_ENVIRONMENTS_VALUE) next.machine = value;
79 return next;
81​
82export function selectProjectAxis(search: SettingsScopeSearch, value: string): SettingsScopeSearch {
83 const next: SettingsScopeSearch = {};
84 if (value !== ALL_PROJECTS_VALUE) next.project = value;
85 if (search.machine) next.machine = search.machine;
86 return next;
88​
89export function selectSingleEnvironmentScope(
90 search: SettingsScopeSearch,
91 scope: ResolvedSettingsScope,
92 environments: readonly {
93 readonly environmentId: EnvironmentPresentation["environmentId"];
94 readonly connection: Pick<EnvironmentPresentation["connection"], "phase">;
95 }[],
96 primaryEnvironmentId: string | null,
97): SettingsScopeSearch {
98 if (search.machine || scope.kind === "unavailable") return search;
99 const selectedIds = new Set(scope.environmentIds);
100 const candidates = environments.filter((environment) =>
101 selectedIds.has(environment.environmentId),
102 );
103 const selected =
104 candidates.find((environment) => environment.environmentId === primaryEnvironmentId) ??
105 candidates.find((environment) => environment.connection.phase === "connected") ??
106 candidates[0];
107 return selected ? { ...search, machine: selected.environmentId } : search;