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
20/** Uses the selected environment's grant, including cached scopes during a refresh. */
21export function useEnvironmentScope(
22 environmentId: EnvironmentId | null,
23 scope: AuthEnvironmentScope,
24): boolean {
25 const result = useAtomValue(
26 environmentId === null
27 ? EMPTY_SESSION_STATE_ATOM
28 : environmentSession.sessionStateAtom(environmentId),
29 );
30 return sessionHasScope(result, scope);
31}
32
33function sessionHasScope(
34 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
35 scope: AuthEnvironmentScope,
36): boolean {
37 const session = Option.getOrNull(AsyncResult.value(result));
38 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
39}
40
41/** Subscribe to the grants of every selected environment. */
42export function useEnvironmentsWithScope(
43 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
44 scope: AuthEnvironmentScope,
45): ReadonlySet<EnvironmentId> {
46 const permitted = useMemo(
47 () =>
48 Atom.make((get) => {
49 const ids = new Set<EnvironmentId>();
50 for (const { environmentId } of environments) {
51 const result = get(environmentSession.sessionStateAtom(environmentId));
52 if (sessionHasScope(result, scope)) {
53 ids.add(environmentId);
54 }
55 }
56 return ids;
57 }),
58 [environments, scope],
59 );
60 return useAtomValue(permitted);
61}
62
63export function readEnvironmentScope(
64 environmentId: EnvironmentId,
65 scope: AuthEnvironmentScope,
66): boolean {
67 return sessionHasScope(
68 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
69 scope,
70 );
71}
72
73const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
74 Atom.withLabel("web-prepared-connection:empty"),
75);
76
77export function usePreparedConnection(environmentId: EnvironmentId | null) {
78 return useAtomValue(
79 environmentId === null
80 ? EMPTY_PREPARED_CONNECTION_ATOM
81 : environmentSession.preparedConnectionValueAtom(environmentId),
82 );
83}
84
85export function readPreparedConnection(environmentId: EnvironmentId) {
86 return Option.getOrNull(
87 appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
88 );
89}
90
91/**
92 * This client's authenticated session on one environment, as reported by that
93 * environment's `/api/auth/session` endpoint. `data` stays populated across
94 * SWR revalidations; `isPending` is only meaningful before the first resolve.
95 */
96export function useEnvironmentSessionState(environmentId: EnvironmentId) {
97 const result = useAtomValue(environmentSession.sessionStateAtom(environmentId));
98 return {
99 data: Option.getOrNull(AsyncResult.value(result)),
100 hasError: result._tag === "Failure",
101 isPending: result.waiting,
102 };
103}