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