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

session.ts

163 lines5.5 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 createEnvironmentSessionAtoms,
4 sessionDeniesScope,
5} from "@t3tools/client-runtime/state/session";
6import {
7 type AuthEnvironmentScope,
8 type AuthSessionState,
9 AuthRelayConfigureScope,
10 type EnvironmentId,
11 sessionGrantsScope,
12} from "@t3tools/contracts";
13import * as Option from "effect/Option";
14import { AsyncResult, Atom } from "effect/reactivity";
15​
16import { connectionAtomRuntime } from "../connection/runtime";
17import { appAtomRegistry } from "../rpc/atomRegistry";
18import { environmentAtomList, environmentIdsKey } from "./environmentAtomList";
19​
20export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime);
21export const environmentSessionStatesAtom = environmentAtomList(
22 "web-environment-session-states",
23 environmentSession.sessionStateAtom,
24);
25​
26const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());
27​
28export function useEnvironmentScope(
29 environmentId: EnvironmentId | null,
30 scope: AuthEnvironmentScope,
31): boolean {
32 // Select the grant: every revalidation emits a new result, and most consumers are list rows.
33 return useAtomValue(
34 environmentId === null
35 ? EMPTY_SESSION_STATE_ATOM
36 : environmentSession.sessionStateAtom(environmentId),
37 (result) => sessionHasScope(result, scope),
38 );
40​
41export function useEnvironmentCanConfigureRelay(environmentId: EnvironmentId | null): boolean {
42 return useAtomValue(
43 environmentId === null
44 ? EMPTY_SESSION_STATE_ATOM
45 : environmentSession.sessionStateAtom(environmentId),
46 (result) => sessionHasScope(result, AuthRelayConfigureScope),
47 );
49​
50export function readEnvironmentCanConfigureRelay(environmentId: EnvironmentId): boolean {
51 const result = appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId));
52 return sessionHasScope(result, AuthRelayConfigureScope);
54​
55/** This client's own session on the environment, or null until the host has said. */
56export function useEnvironmentSessionId(environmentId: EnvironmentId | null): string | null {
57 return useAtomValue(
58 environmentId === null
59 ? EMPTY_SESSION_STATE_ATOM
60 : environmentSession.sessionStateAtom(environmentId),
61 (result) => AsyncResult.getOrElse(result, () => null)?.sessionId ?? null,
62 );
64​
65export function sessionHasScope(
66 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
67 scope: AuthEnvironmentScope,
68): boolean {
69 const session = AsyncResult.getOrElse(result, () => null);
70 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
72​
73const environmentsWithScopeAtom = Atom.family((key: string) => {
74 const [scope, idsKey = ""] = key.split(/\n(.*)/s) as [AuthEnvironmentScope, string?];
75 const ids = idsKey === "" ? [] : (idsKey.split("\n") as EnvironmentId[]);
76 const statesAtom = environmentSessionStatesAtom(idsKey);
77 return Atom.make((get) => {
78 const states = get(statesAtom);
79 return new Set(ids.filter((_, index) => sessionHasScope(states[index]!, scope)));
80 }).pipe(Atom.withLabel(`web-environments-with-scope:${key}`));
81});
82​
83export function useEnvironmentsWithScope(
84 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
85 scope: AuthEnvironmentScope,
86): ReadonlySet<EnvironmentId> {
87 return useAtomValue(
88 environmentsWithScopeAtom(
89 `${scope}\n${environmentIdsKey(environments.map(({ environmentId }) => environmentId))}`,
90 ),
91 );
93​
94export function readEnvironmentScope(
95 environmentId: EnvironmentId,
96 scope: AuthEnvironmentScope,
97): boolean {
98 return sessionHasScope(
99 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
100 scope,
101 );
103​
104/** Unlike `!useEnvironmentScope`, stays false until the host has answered without the scope. */
105export function useEnvironmentScopeDenied(
106 environmentId: EnvironmentId | null,
107 scope: AuthEnvironmentScope,
108): boolean {
109 return useAtomValue(
110 environmentId === null
111 ? EMPTY_SESSION_STATE_ATOM
112 : environmentSession.sessionStateAtom(environmentId),
113 (result) => sessionDeniesScope(result, scope),
114 );
116​
117export function readEnvironmentScopeDenied(
118 environmentId: EnvironmentId,
119 scope: AuthEnvironmentScope,
120): boolean {
121 return sessionDeniesScope(
122 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
123 scope,
124 );
126​
127const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
128 Atom.withLabel("web-prepared-connection:empty"),
129);
130​
131export function usePreparedConnection(environmentId: EnvironmentId | null) {
132 return useAtomValue(
133 environmentId === null
134 ? EMPTY_PREPARED_CONNECTION_ATOM
135 : environmentSession.preparedConnectionValueAtom(environmentId),
136 );
138​
139/** Each environment's prepared connection, in the order of the ids. */
140export const preparedConnectionsAtom = environmentAtomList(
141 "web-prepared-connections",
142 environmentSession.preparedConnectionValueAtom,
143);
144​
145export function readPreparedConnection(environmentId: EnvironmentId) {
146 return Option.getOrNull(
147 appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
148 );
150​
151/** data survives SWR revalidation; isPending applies only before first resolve. */
152export function useEnvironmentSessionState(environmentId: EnvironmentId | null) {
153 const result = useAtomValue(
154 environmentId === null
155 ? EMPTY_SESSION_STATE_ATOM
156 : environmentSession.sessionStateAtom(environmentId),
157 );
158 return {
159 data: AsyncResult.getOrElse(result, () => null),
160 hasError: result._tag === "Failure",
161 isPending: result.waiting,
162 };