Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import {
3 AuthProvidersManageScope,
4 type AuthSessionState,
5 type EnvironmentId,
6 type ProjectId,
7 type ProjectSettingsOverrides,
8 type ProviderInstanceId,
9 sessionGrantsScope,
10 type ServerSettings,
11 type SessionGrantInput,
12} from "@t3tools/contracts";
13
14export interface ProviderEnvironmentOptionLike {
15 readonly environmentId: EnvironmentId;
16 readonly label: string;
17}
18
19export function isProviderSettingsEnvironmentAvailable(input: {
20 readonly connectionPhase: EnvironmentConnectionPhase;
21 readonly hasServerConfig: boolean;
22}): boolean {
23 return input.connectionPhase === "connected" && input.hasServerConfig;
24}
25
26export function buildProviderEnvironmentOptions<T extends ProviderEnvironmentOptionLike>(
27 environments: ReadonlyArray<T>,
28 primaryEnvironmentId: EnvironmentId | null,
29 environmentIds?: readonly EnvironmentId[],
30): ReadonlyArray<T> {
31 const allowed = environmentIds ? new Set(environmentIds) : null;
32 return environments
33 .filter((environment) => !allowed || allowed.has(environment.environmentId))
34 .toSorted((left, right) => {
35 const leftIsPrimary = left.environmentId === primaryEnvironmentId;
36 const rightIsPrimary = right.environmentId === primaryEnvironmentId;
37 if (leftIsPrimary !== rightIsPrimary) {
38 return leftIsPrimary ? -1 : 1;
39 }
40 return (
41 left.label.localeCompare(right.label) ||
42 String(left.environmentId).localeCompare(String(right.environmentId))
43 );
44 });
45}
46
47export function resolveSelectedProviderEnvironmentId(
48 environments: ReadonlyArray<ProviderEnvironmentOptionLike>,
49 selectedEnvironmentId: EnvironmentId | null,
50 primaryEnvironmentId: EnvironmentId | null,
51): EnvironmentId | null {
52 if (
53 selectedEnvironmentId !== null &&
54 environments.some((environment) => environment.environmentId === selectedEnvironmentId)
55 ) {
56 return selectedEnvironmentId;
57 }
58 if (
59 primaryEnvironmentId !== null &&
60 environments.some((environment) => environment.environmentId === primaryEnvironmentId)
61 ) {
62 return primaryEnvironmentId;
63 }
64 return environments[0]?.environmentId ?? null;
65}
66
67export type ProviderEnvironmentAccess =
68 | { readonly kind: "editable" }
69 | { readonly kind: "loading"; readonly reason: "config" | "permissions" }
70 | { readonly kind: "read-only" }
71 | { readonly kind: "unavailable" }
72 | { readonly kind: "error" };
73
74export type ProviderOperateAccess = "granted" | "denied" | "pending";
75
76function resolveSessionOperateAccess(input: {
77 readonly session: SessionGrantInput | null;
78 readonly isPending: boolean;
79 readonly hasError: boolean;
80}): ProviderOperateAccess {
81 if (input.hasError) return "denied";
82 if (input.session === null) return input.isPending ? "pending" : "denied";
83 return sessionGrantsScope(input.session, AuthProvidersManageScope) ? "granted" : "denied";
84}
85
86export function resolvePrimaryOperateAccess(input: {
87 readonly isPrimary: boolean;
88 readonly hasDesktopBridge: boolean;
89 readonly session: SessionGrantInput | null;
90 readonly isPending: boolean;
91 readonly hasError: boolean;
92}): ProviderOperateAccess {
93 return resolveSessionOperateAccess(input);
94}
95
96export function resolveRemoteOperateAccess(input: {
97 readonly session: SessionGrantInput | null;
98 readonly isPending: boolean;
99 readonly hasError: boolean;
100}): ProviderOperateAccess {
101 return resolveSessionOperateAccess(input);
102}
103
104/**
105 * `providerInstances` is always sent explicitly, even as `{}`, because the
106 * server keeps a stored `providerInstances` map when an incoming entry omits
107 * the key (so older clients that don't know the field cannot wipe it by editing
108 * another setting).
109 */
110
111export function buildProviderInstanceOverridePatch(
112 settings: Pick<ServerSettings, "projectSettingsOverrides">,
113 projectIds: readonly ProjectId[],
114 instanceId: ProviderInstanceId,
115 value: boolean | undefined,
116): Record<ProjectId, ProjectSettingsOverrides | null> {
117 const patch: Record<ProjectId, ProjectSettingsOverrides | null> = {};
118 for (const projectId of projectIds) {
119 const current = settings.projectSettingsOverrides[projectId];
120 const instances = { ...current?.providerInstances };
121 const instance = { ...instances[instanceId] };
122 if (value === undefined) delete instance.enabled;
123 else instance.enabled = value;
124 if (Object.keys(instance).length > 0) instances[instanceId] = instance;
125 else delete instances[instanceId];
126
127 const { providerInstances: _omit, ...rest } = current ?? {};
128 const nextEntry: ProjectSettingsOverrides = { ...rest, providerInstances: instances };
129 patch[projectId] =
130 Object.keys(rest).length === 0 && Object.keys(instances).length === 0 ? null : nextEntry;
131 }
132 return patch;
133}
134
135export function classifyProviderEnvironmentAccess(input: {
136 readonly connectionPhase: EnvironmentConnectionPhase;
137 readonly hasServerConfig: boolean;
138 readonly operateAccess: ProviderOperateAccess;
139}): ProviderEnvironmentAccess {
140 if (input.connectionPhase === "error") {
141 return { kind: "error" };
142 }
143 if (input.connectionPhase !== "connected") {
144 return { kind: "unavailable" };
145 }
146 if (!input.hasServerConfig) {
147 return { kind: "loading", reason: "config" };
148 }
149 if (input.operateAccess === "pending") {
150 return { kind: "loading", reason: "permissions" };
151 }
152 if (input.operateAccess === "denied") {
153 return { kind: "read-only" };
154 }
155 return { kind: "editable" };
156}