Files
t3code/apps/web/src/state/session.ts
T

104 lines
3.4 KiB
TypeScript

import { useAtomValue } from "@effect/atom-react";
import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session";
import {
type AuthEnvironmentScope,
type AuthSessionState,
type EnvironmentId,
sessionGrantsScope,
} from "@t3tools/contracts";
import { useMemo } from "react";
import * as Option from "effect/Option";
import { AsyncResult, Atom } from "effect/reactivity";
import { connectionAtomRuntime } from "../connection/runtime";
import { appAtomRegistry } from "../rpc/atomRegistry";
export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime);
const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());
/** Uses the selected environment's grant, including cached scopes during a refresh. */
export function useEnvironmentScope(
environmentId: EnvironmentId | null,
scope: AuthEnvironmentScope,
): boolean {
const result = useAtomValue(
environmentId === null
? EMPTY_SESSION_STATE_ATOM
: environmentSession.sessionStateAtom(environmentId),
);
return sessionHasScope(result, scope);
}
function sessionHasScope(
result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
scope: AuthEnvironmentScope,
): boolean {
const session = Option.getOrNull(AsyncResult.value(result));
return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
}
/** Subscribe to the grants of every selected environment. */
export function useEnvironmentsWithScope(
environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>,
scope: AuthEnvironmentScope,
): ReadonlySet<EnvironmentId> {
const permitted = useMemo(
() =>
Atom.make((get) => {
const ids = new Set<EnvironmentId>();
for (const { environmentId } of environments) {
const result = get(environmentSession.sessionStateAtom(environmentId));
if (sessionHasScope(result, scope)) {
ids.add(environmentId);
}
}
return ids;
}),
[environments, scope],
);
return useAtomValue(permitted);
}
export function readEnvironmentScope(
environmentId: EnvironmentId,
scope: AuthEnvironmentScope,
): boolean {
return sessionHasScope(
appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)),
scope,
);
}
const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe(
Atom.withLabel("web-prepared-connection:empty"),
);
export function usePreparedConnection(environmentId: EnvironmentId | null) {
return useAtomValue(
environmentId === null
? EMPTY_PREPARED_CONNECTION_ATOM
: environmentSession.preparedConnectionValueAtom(environmentId),
);
}
export function readPreparedConnection(environmentId: EnvironmentId) {
return Option.getOrNull(
appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)),
);
}
/**
* This client's authenticated session on one environment, as reported by that
* environment's `/api/auth/session` endpoint. `data` stays populated across
* SWR revalidations; `isPending` is only meaningful before the first resolve.
*/
export function useEnvironmentSessionState(environmentId: EnvironmentId) {
const result = useAtomValue(environmentSession.sessionStateAtom(environmentId));
return {
data: Option.getOrNull(AsyncResult.value(result)),
hasError: result._tag === "Failure",
isPending: result.waiting,
};
}