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/** Send providerInstances even when empty; omission preserves the stored map for older clients. */
105
106export function buildProviderInstanceOverridePatch(
107 settings: Pick<ServerSettings, "projectSettingsOverrides">,
108 projectIds: readonly ProjectId[],
109 instanceId: ProviderInstanceId,
110 value: boolean | undefined,
111): Record<ProjectId, ProjectSettingsOverrides | null> {
112 const patch: Record<ProjectId, ProjectSettingsOverrides | null> = {};
113 for (const projectId of projectIds) {
114 const current = settings.projectSettingsOverrides[projectId];
115 const instances = { ...current?.providerInstances };
116 const instance = { ...instances[instanceId] };
117 if (value === undefined) delete instance.enabled;
118 else instance.enabled = value;
119 if (Object.keys(instance).length > 0) instances[instanceId] = instance;
120 else delete instances[instanceId];
121
122 const { providerInstances: _omit, ...rest } = current ?? {};
123 const nextEntry: ProjectSettingsOverrides = { ...rest, providerInstances: instances };
124 patch[projectId] =
125 Object.keys(rest).length === 0 && Object.keys(instances).length === 0 ? null : nextEntry;
126 }
127 return patch;
128}
129
130export function classifyProviderEnvironmentAccess(input: {
131 readonly connectionPhase: EnvironmentConnectionPhase;
132 readonly hasServerConfig: boolean;
133 readonly operateAccess: ProviderOperateAccess;
134}): ProviderEnvironmentAccess {
135 if (input.connectionPhase === "error") {
136 return { kind: "error" };
137 }
138 if (input.connectionPhase !== "connected") {
139 return { kind: "unavailable" };
140 }
141 if (!input.hasServerConfig) {
142 return { kind: "loading", reason: "config" };
143 }
144 if (input.operateAccess === "pending") {
145 return { kind: "loading", reason: "permissions" };
146 }
147 if (input.operateAccess === "denied") {
148 return { kind: "read-only" };
149 }
150 return { kind: "editable" };
151}