Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderSettingsPanel.logic.ts

171 lines6.0 KB

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