Open History to see the last commit
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 type EnvironmentId,
10 sessionGrantsScope,
11} from "@t3tools/contracts";
12import * as Option from "effect/Option";
13import { AsyncResult, Atom } from "effect/reactivity";
14import { useMemo } from "react";
15
16import { connectionAtomRuntime } from "../connection/runtime";
17import { appAtomRegistry } from "./atom-registry";
18
19export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime);
20
21const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());
22
23export function sessionHasScope(
24 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
25 scope: AuthEnvironmentScope,
26): boolean {
27 const session = AsyncResult.getOrElse(result, () => null);
28 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
29}
30
31function sessionStateAtomFor(environmentId: EnvironmentId | null) {
32 return environmentId === null
33 ? EMPTY_SESSION_STATE_ATOM
34 : environmentSession.sessionStateAtom(environmentId);
35}
36
37// Both hooks select a boolean: every revalidation emits a new result, and most consumers are
38// list rows.
39export function useEnvironmentScope(
40 environmentId: EnvironmentId | null,
41 scope: AuthEnvironmentScope,
42): boolean {
43 return useAtomValue(sessionStateAtomFor(environmentId), (result) =>
44 sessionHasScope(result, scope),
45 );
46}
47
48/** Unlike `!useEnvironmentScope`, stays false until the host has answered without the scope. */
49export function useEnvironmentScopeDenied(
50 environmentId: EnvironmentId | null,
51 scope: AuthEnvironmentScope,
52): boolean {
53 return useAtomValue(sessionStateAtomFor(environmentId), (result) =>
54 sessionDeniesScope(result, scope),
55 );
56}
57
58export function useEnvironmentsWithScope(
59 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
60 scope: AuthEnvironmentScope,
61): ReadonlySet<EnvironmentId> {
62 const permittedEnvironments = useMemo(
63 () =>
64 Atom.make((get) => {
65 const permitted = new Set<EnvironmentId>();
66 for (const { environmentId } of environments) {
67 if (sessionHasScope(get(environmentSession.sessionStateAtom(environmentId)), scope)) {
68 permitted.add(environmentId);
69 }
70 }
71 return permitted;
72 }),
73 [environments, scope],
74 );
75 return useAtomValue(permittedEnvironments);
76}
77
78export function readEnvironmentScope(
79 environmentId: EnvironmentId,
80 scope: AuthEnvironmentScope,
81): boolean {
82 const result = appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId));
83 return sessionHasScope(result, scope);
84}
85
86const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
87 Atom.withLabel("mobile-prepared-connection:empty"),
88);
89
90export function usePreparedConnection(environmentId: EnvironmentId | null) {
91 return useAtomValue(
92 environmentId === null
93 ? EMPTY_PREPARED_CONNECTION_ATOM
94 : environmentSession.preparedConnectionValueAtom(environmentId),
95 );
96}