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 sessionGrantsScope,
7 type SessionGrantInput,
8} from "@t3tools/contracts";
9
10export interface ProviderEnvironmentOptionLike {
11 readonly environmentId: EnvironmentId;
12 readonly label: string;
13}
14
15export function isProviderSettingsEnvironmentAvailable(input: {
16 readonly connectionPhase: EnvironmentConnectionPhase;
17 readonly hasServerConfig: boolean;
18}): boolean {
19 return input.connectionPhase === "connected" && input.hasServerConfig;
20}
21
22export function buildProviderEnvironmentOptions<T extends ProviderEnvironmentOptionLike>(
23 environments: ReadonlyArray<T>,
24 primaryEnvironmentId: EnvironmentId | null,
25 environmentIds?: readonly EnvironmentId[],
26): ReadonlyArray<T> {
27 const allowed = environmentIds ? new Set(environmentIds) : null;
28 return environments
29 .filter((environment) => !allowed || allowed.has(environment.environmentId))
30 .toSorted((left, right) => {
31 const leftIsPrimary = left.environmentId === primaryEnvironmentId;
32 const rightIsPrimary = right.environmentId === primaryEnvironmentId;
33 if (leftIsPrimary !== rightIsPrimary) {
34 return leftIsPrimary ? -1 : 1;
35 }
36 return (
37 left.label.localeCompare(right.label) ||
38 String(left.environmentId).localeCompare(String(right.environmentId))
39 );
40 });
41}
42
43export function resolveSelectedProviderEnvironmentId(
44 environments: ReadonlyArray<ProviderEnvironmentOptionLike>,
45 selectedEnvironmentId: EnvironmentId | null,
46 primaryEnvironmentId: EnvironmentId | null,
47): EnvironmentId | null {
48 if (
49 selectedEnvironmentId !== null &&
50 environments.some((environment) => environment.environmentId === selectedEnvironmentId)
51 ) {
52 return selectedEnvironmentId;
53 }
54 if (
55 primaryEnvironmentId !== null &&
56 environments.some((environment) => environment.environmentId === primaryEnvironmentId)
57 ) {
58 return primaryEnvironmentId;
59 }
60 return environments[0]?.environmentId ?? null;
61}
62
63export type ProviderEnvironmentAccess =
64 | { readonly kind: "editable" }
65 /** `reason` distinguishes waiting on the device from waiting on permissions. */
66 | { readonly kind: "loading"; readonly reason: "config" | "permissions" }
67 | { readonly kind: "read-only" }
68 | { readonly kind: "unavailable" }
69 | { readonly kind: "error" };
70
71/**
72 * Whether the session may change provider configuration on an environment.
73 * `pending` means the answer is still unknown, which must not be presented as
74 * editable: rendering controls we already know might be rejected only turns a
75 * permission problem into a failed write.
76 */
77export type ProviderOperateAccess = "granted" | "denied" | "pending";
78
79/** Cached grants remain usable during revalidation; unknown or failed lookups grant nothing. */
80function resolveSessionOperateAccess(input: {
81 readonly session: SessionGrantInput | null;
82 readonly isPending: boolean;
83 readonly hasError: boolean;
84}): ProviderOperateAccess {
85 if (input.hasError) return "denied";
86 if (input.session === null) return input.isPending ? "pending" : "denied";
87 return sessionGrantsScope(input.session, AuthProvidersManageScope) ? "granted" : "denied";
88}
89
90export function resolvePrimaryOperateAccess(input: {
91 readonly isPrimary: boolean;
92 readonly hasDesktopBridge: boolean;
93 readonly session: SessionGrantInput | null;
94 readonly isPending: boolean;
95 readonly hasError: boolean;
96}): ProviderOperateAccess {
97 return resolveSessionOperateAccess(input);
98}
99
100export function resolveRemoteOperateAccess(input: {
101 readonly session: SessionGrantInput | null;
102 readonly isPending: boolean;
103 readonly hasError: boolean;
104}): ProviderOperateAccess {
105 return resolveSessionOperateAccess(input);
106}
107
108export function classifyProviderEnvironmentAccess(input: {
109 readonly connectionPhase: EnvironmentConnectionPhase;
110 readonly hasServerConfig: boolean;
111 readonly operateAccess: ProviderOperateAccess;
112}): ProviderEnvironmentAccess {
113 if (input.connectionPhase === "error") {
114 return { kind: "error" };
115 }
116 if (input.connectionPhase !== "connected") {
117 return { kind: "unavailable" };
118 }
119 if (!input.hasServerConfig) {
120 return { kind: "loading", reason: "config" };
121 }
122 if (input.operateAccess === "pending") {
123 return { kind: "loading", reason: "permissions" };
124 }
125 if (input.operateAccess === "denied") {
126 return { kind: "read-only" };
127 }
128 return { kind: "editable" };
129}