Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import {
3 AuthOrchestrationOperateScope,
4 type AuthSessionState,
5 type EnvironmentId,
6} from "@t3tools/contracts";
7
8export interface ProviderEnvironmentOptionLike {
9 readonly environmentId: EnvironmentId;
10 readonly label: string;
11}
12
13export function isProviderSettingsEnvironmentAvailable(input: {
14 readonly connectionPhase: EnvironmentConnectionPhase;
15 readonly hasServerConfig: boolean;
16}): boolean {
17 return input.connectionPhase === "connected" && input.hasServerConfig;
18}
19
20export function buildProviderEnvironmentOptions<T extends ProviderEnvironmentOptionLike>(
21 environments: ReadonlyArray<T>,
22 primaryEnvironmentId: EnvironmentId | null,
23): ReadonlyArray<T> {
24 return environments.toSorted((left, right) => {
25 const leftIsPrimary = left.environmentId === primaryEnvironmentId;
26 const rightIsPrimary = right.environmentId === primaryEnvironmentId;
27 if (leftIsPrimary !== rightIsPrimary) {
28 return leftIsPrimary ? -1 : 1;
29 }
30 return (
31 left.label.localeCompare(right.label) ||
32 String(left.environmentId).localeCompare(String(right.environmentId))
33 );
34 });
35}
36
37export function resolveSelectedProviderEnvironmentId(
38 environments: ReadonlyArray<ProviderEnvironmentOptionLike>,
39 selectedEnvironmentId: EnvironmentId | null,
40 primaryEnvironmentId: EnvironmentId | null,
41): EnvironmentId | null {
42 if (
43 selectedEnvironmentId !== null &&
44 environments.some((environment) => environment.environmentId === selectedEnvironmentId)
45 ) {
46 return selectedEnvironmentId;
47 }
48 if (
49 primaryEnvironmentId !== null &&
50 environments.some((environment) => environment.environmentId === primaryEnvironmentId)
51 ) {
52 return primaryEnvironmentId;
53 }
54 return environments[0]?.environmentId ?? null;
55}
56
57export type ProviderEnvironmentAccess =
58 | { readonly kind: "editable" }
59 /** `reason` distinguishes waiting on the device from waiting on permissions. */
60 | { readonly kind: "loading"; readonly reason: "config" | "permissions" }
61 | { readonly kind: "read-only" }
62 | { readonly kind: "unavailable" }
63 | { readonly kind: "error" };
64
65/**
66 * Whether the session may change provider configuration on an environment.
67 * `pending` means the answer is still unknown, which must not be presented as
68 * editable: rendering controls we already know might be rejected only turns a
69 * permission problem into a failed write.
70 */
71export type ProviderOperateAccess = "granted" | "denied" | "pending";
72
73/**
74 * Resolve operate access from an environment's `/api/auth/session` answer.
75 *
76 * Cached session data wins over an in-flight revalidation. The session atoms
77 * are SWR-backed, so they report `isPending` on every background refresh;
78 * treating that as unknown would flip a working panel back to loading and
79 * discard in-progress edits.
80 *
81 * `missingScopesAccess` decides the case where the session resolved but did
82 * not report scopes: the primary serves the web app itself so its server
83 * always reports them (absence means denial), while a remote device may run an
84 * older server version that predates scope reporting, where denial would lock
85 * out a legitimate session. The environment RPC layer stays authoritative
86 * either way.
87 */
88function resolveSessionOperateAccess(input: {
89 readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
90 readonly isPending: boolean;
91 readonly hasError: boolean;
92 readonly missingScopesAccess: "granted" | "denied";
93}): ProviderOperateAccess {
94 if (input.session === null) {
95 if (input.isPending) {
96 return "pending";
97 }
98 // A failed session fetch is a transport problem, not a permission
99 // decision — locking the panel read-only would misreport it. Stay
100 // optimistic; the environment RPC layer still rejects unauthorized writes.
101 return input.hasError ? "granted" : "denied";
102 }
103 if (!input.session.authenticated) {
104 return "denied";
105 }
106 if (input.session.scopes === undefined) {
107 return input.missingScopesAccess;
108 }
109 return input.session.scopes.includes(AuthOrchestrationOperateScope) ? "granted" : "denied";
110}
111
112/** Operate access for the primary environment's own browser session. */
113export function resolvePrimaryOperateAccess(input: {
114 readonly isPrimary: boolean;
115 readonly hasDesktopBridge: boolean;
116 readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
117 readonly isPending: boolean;
118 readonly hasError: boolean;
119}): ProviderOperateAccess {
120 if (!input.isPrimary || input.hasDesktopBridge) {
121 return "granted";
122 }
123 return resolveSessionOperateAccess({
124 session: input.session,
125 isPending: input.isPending,
126 hasError: input.hasError,
127 missingScopesAccess: "denied",
128 });
129}
130
131/**
132 * Operate access for a non-primary environment, derived from the scopes its
133 * `/api/auth/session` endpoint reports for this client's credential.
134 */
135export function resolveRemoteOperateAccess(input: {
136 readonly session: Pick<AuthSessionState, "authenticated" | "scopes"> | null;
137 readonly isPending: boolean;
138 readonly hasError: boolean;
139}): ProviderOperateAccess {
140 return resolveSessionOperateAccess({
141 ...input,
142 missingScopesAccess: "granted",
143 });
144}
145
146export function classifyProviderEnvironmentAccess(input: {
147 readonly connectionPhase: EnvironmentConnectionPhase;
148 readonly hasServerConfig: boolean;
149 readonly operateAccess: ProviderOperateAccess;
150}): ProviderEnvironmentAccess {
151 if (input.connectionPhase === "error") {
152 return { kind: "error" };
153 }
154 if (input.connectionPhase !== "connected") {
155 return { kind: "unavailable" };
156 }
157 if (!input.hasServerConfig) {
158 return { kind: "loading", reason: "config" };
159 }
160 if (input.operateAccess === "pending") {
161 return { kind: "loading", reason: "permissions" };
162 }
163 if (input.operateAccess === "denied") {
164 return { kind: "read-only" };
165 }
166 return { kind: "editable" };
167}