Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session";
3import {
4 type AuthEnvironmentScope,
5 type AuthSessionState,
6 AuthRelayConfigureScope,
7 type EnvironmentId,
8 sessionGrantsScope,
9} from "@t3tools/contracts";
10import * as Option from "effect/Option";
11import { AsyncResult, Atom } from "effect/reactivity";
12
13import { connectionAtomRuntime } from "../connection/runtime";
14import { appAtomRegistry } from "../rpc/atomRegistry";
15import { environmentAtomList, environmentIdsKey } from "./environmentAtomList";
16
17export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime);
18export const environmentSessionStatesAtom = environmentAtomList(
19 "web-environment-session-states",
20 environmentSession.sessionStateAtom,
21);
22
23const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());
24
25export function useEnvironmentScope(
26 environmentId: EnvironmentId | null,
27 scope: AuthEnvironmentScope,
28): boolean {
29 // Select the grant: every revalidation emits a new result, and most consumers are list rows.
30 return useAtomValue(
31 environmentId === null
32 ? EMPTY_SESSION_STATE_ATOM
33 : environmentSession.sessionStateAtom(environmentId),
34 (result) => sessionHasScope(result, scope),
35 );
36}
37
38export function useEnvironmentCanConfigureRelay(environmentId: EnvironmentId | null): boolean {
39 return useAtomValue(
40 environmentId === null
41 ? EMPTY_SESSION_STATE_ATOM
42 : environmentSession.sessionStateAtom(environmentId),
43 (result) => sessionHasScope(result, AuthRelayConfigureScope),
44 );
45}
46
47export function readEnvironmentCanConfigureRelay(environmentId: EnvironmentId): boolean {
48 const result = appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId));
49 return sessionHasScope(result, AuthRelayConfigureScope);
50}
51
52export function sessionHasScope(
53 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
54 scope: AuthEnvironmentScope,
55): boolean {
56 const session = AsyncResult.getOrElse(result, () => null);
57 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
58}
59
60const environmentsWithScopeAtom = Atom.family((key: string) => {
61 const [scope, idsKey = ""] = key.split(/\n(.*)/s) as [AuthEnvironmentScope, string?];
62 const ids = idsKey === "" ? [] : (idsKey.split("\n") as EnvironmentId[]);
63 const statesAtom = environmentSessionStatesAtom(idsKey);
64 return Atom.make((get) => {
65 const states = get(statesAtom);
66 return new Set(ids.filter((_, index) => sessionHasScope(states[index]!, scope)));
67 }).pipe(Atom.withLabel(`web-environments-with-scope:${key}`));
68});
69
70export function useEnvironmentsWithScope(
71 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
72 scope: AuthEnvironmentScope,
73): ReadonlySet<EnvironmentId> {
74 return useAtomValue(
75 environmentsWithScopeAtom(
76 `${scope}\n${environmentIdsKey(environments.map(({ environmentId }) => environmentId))}`,
77 ),
78 );
79}
80
81export function readEnvironmentScope(
82 environmentId: EnvironmentId,
83 scope: AuthEnvironmentScope,
84): boolean {
85 return sessionHasScope(
86 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
87 scope,
88 );
89}
90
91const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
92 Atom.withLabel("web-prepared-connection:empty"),
93);
94
95export function usePreparedConnection(environmentId: EnvironmentId | null) {
96 return useAtomValue(
97 environmentId === null
98 ? EMPTY_PREPARED_CONNECTION_ATOM
99 : environmentSession.preparedConnectionValueAtom(environmentId),
100 );
101}
102
103/** Each environment's prepared connection, in the order of the ids. */
104export const preparedConnectionsAtom = environmentAtomList(
105 "web-prepared-connections",
106 environmentSession.preparedConnectionValueAtom,
107);
108
109export function readPreparedConnection(environmentId: EnvironmentId) {
110 return Option.getOrNull(
111 appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
112 );
113}
114
115/** data survives SWR revalidation; isPending applies only before first resolve. */
116export function useEnvironmentSessionState(environmentId: EnvironmentId | null) {
117 const result = useAtomValue(
118 environmentId === null
119 ? EMPTY_SESSION_STATE_ATOM
120 : environmentSession.sessionStateAtom(environmentId),
121 );
122 return {
123 data: AsyncResult.getOrElse(result, () => null),
124 hasError: result._tag === "Failure",
125 isPending: result.waiting,
126 };
127}