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 type EnvironmentId,
7 sessionGrantsScope,
8} from "@t3tools/contracts";
9import * as Option from "effect/Option";
10import { AsyncResult, Atom } from "effect/reactivity";
11
12import { connectionAtomRuntime } from "../connection/runtime";
13import { appAtomRegistry } from "../rpc/atomRegistry";
14
15export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime);
16
17const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());
18
19export function useEnvironmentScope(
20 environmentId: EnvironmentId | null,
21 scope: AuthEnvironmentScope,
22): boolean {
23 const result = useAtomValue(
24 environmentId === null
25 ? EMPTY_SESSION_STATE_ATOM
26 : environmentSession.sessionStateAtom(environmentId),
27 );
28 return sessionHasScope(result, scope);
29}
30
31function sessionHasScope(
32 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
33 scope: AuthEnvironmentScope,
34): boolean {
35 const session = Option.getOrNull(AsyncResult.value(result));
36 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
37}
38
39export function useEnvironmentsWithScope(
40 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
41 scope: AuthEnvironmentScope,
42): ReadonlySet<EnvironmentId> {
43 const permitted = Atom.make((get) => {
44 const ids = new Set<EnvironmentId>();
45 for (const { environmentId } of environments) {
46 const result = get(environmentSession.sessionStateAtom(environmentId));
47 if (sessionHasScope(result, scope)) {
48 ids.add(environmentId);
49 }
50 }
51 return ids;
52 });
53 return useAtomValue(permitted);
54}
55
56export function readEnvironmentScope(
57 environmentId: EnvironmentId,
58 scope: AuthEnvironmentScope,
59): boolean {
60 return sessionHasScope(
61 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
62 scope,
63 );
64}
65
66const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
67 Atom.withLabel("web-prepared-connection:empty"),
68);
69
70export function usePreparedConnection(environmentId: EnvironmentId | null) {
71 return useAtomValue(
72 environmentId === null
73 ? EMPTY_PREPARED_CONNECTION_ATOM
74 : environmentSession.preparedConnectionValueAtom(environmentId),
75 );
76}
77
78export function readPreparedConnection(environmentId: EnvironmentId) {
79 return Option.getOrNull(
80 appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
81 );
82}
83
84/**
85 * `data` stays populated across SWR revalidations; `isPending` is only
86 * meaningful before the first resolve.
87 */
88export function useEnvironmentSessionState(environmentId: EnvironmentId) {
89 const result = useAtomValue(environmentSession.sessionStateAtom(environmentId));
90 return {
91 data: Option.getOrNull(AsyncResult.value(result)),
92 hasError: result._tag === "Failure",
93 isPending: result.waiting,
94 };
95}