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