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

session.ts

116 lines3.7 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 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​
23/** This client's own session on the environment, or null until the host has said. */
24export function useEnvironmentSessionId(environmentId: EnvironmentId | null): string | null {
25 return useAtomValue(
26 environmentId === null
27 ? EMPTY_SESSION_STATE_ATOM
28 : environmentSession.sessionStateAtom(environmentId),
29 (result) => AsyncResult.getOrElse(result, () => null)?.sessionId ?? null,
30 );
32​
33export function sessionHasScope(
34 result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
35 scope: AuthEnvironmentScope,
36): boolean {
37 const session = AsyncResult.getOrElse(result, () => null);
38 return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
40​
41function sessionStateAtomFor(environmentId: EnvironmentId | null) {
42 return environmentId === null
43 ? EMPTY_SESSION_STATE_ATOM
44 : environmentSession.sessionStateAtom(environmentId);
46​
47// Both hooks select a boolean: every revalidation emits a new result, and most consumers are
48// list rows.
49export function useEnvironmentScope(
50 environmentId: EnvironmentId | null,
51 scope: AuthEnvironmentScope,
52): boolean {
53 return useAtomValue(sessionStateAtomFor(environmentId), (result) =>
54 sessionHasScope(result, scope),
55 );
57​
58/** Unlike `!useEnvironmentScope`, stays false until the host has answered without the scope. */
59export function useEnvironmentScopeDenied(
60 environmentId: EnvironmentId | null,
61 scope: AuthEnvironmentScope,
62): boolean {
63 return useAtomValue(sessionStateAtomFor(environmentId), (result) =>
64 sessionDeniesScope(result, scope),
65 );
67​
68export function useEnvironmentsWithScope(
69 environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
70 scope: AuthEnvironmentScope,
71): ReadonlySet<EnvironmentId> {
72 const permittedEnvironments = useMemo(
73 () =>
74 Atom.make((get) => {
75 const permitted = new Set<EnvironmentId>();
76 for (const { environmentId } of environments) {
77 if (sessionHasScope(get(environmentSession.sessionStateAtom(environmentId)), scope)) {
78 permitted.add(environmentId);
79 }
80 }
81 return permitted;
82 }),
83 [environments, scope],
84 );
85 return useAtomValue(permittedEnvironments);
87​
88export function readEnvironmentScope(
89 environmentId: EnvironmentId,
90 scope: AuthEnvironmentScope,
91): boolean {
92 const result = appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId));
93 return sessionHasScope(result, scope);
95​
96export function readEnvironmentScopeDenied(
97 environmentId: EnvironmentId,
98 scope: AuthEnvironmentScope,
99): boolean {
100 return sessionDeniesScope(
101 appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
102 scope,
103 );
105​
106const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
107 Atom.withLabel("mobile-prepared-connection:empty"),
108);
109​
110export function usePreparedConnection(environmentId: EnvironmentId | null) {
111 return useAtomValue(
112 environmentId === null
113 ? EMPTY_PREPARED_CONNECTION_ATOM
114 : environmentSession.preparedConnectionValueAtom(environmentId),
115 );