The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session"; |
| 3 | import { |
| 4 | type AuthEnvironmentScope, |
| 5 | type AuthSessionState, |
| 6 | AuthRelayConfigureScope, |
| 7 | type EnvironmentId, |
| 8 | sessionGrantsScope, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import * as Option from "effect/Option"; |
| 11 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 12 | |
| 13 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 14 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 15 | import { environmentAtomList, environmentIdsKey } from "./environmentAtomList"; |
| 16 | |
| 17 | export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime); |
| 18 | export const environmentSessionStatesAtom = environmentAtomList( |
| 19 | "web-environment-session-states", |
| 20 | environmentSession.sessionStateAtom, |
| 21 | ); |
| 22 | |
| 23 | const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>()); |
| 24 | |
| 25 | export function useEnvironmentScope( |
| 26 | environmentId: EnvironmentId | null, |
| 27 | scope: AuthEnvironmentScope, |
| 28 | ): boolean { |
| 29 | // Select the grant: every revalidation emits a new result, and most consumers are list rows. |
| 30 | return useAtomValue( |
| 31 | environmentId === null |
| 32 | ? EMPTY_SESSION_STATE_ATOM |
| 33 | : environmentSession.sessionStateAtom(environmentId), |
| 34 | (result) => sessionHasScope(result, scope), |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | export function useEnvironmentCanConfigureRelay(environmentId: EnvironmentId | null): boolean { |
| 39 | return useAtomValue( |
| 40 | environmentId === null |
| 41 | ? EMPTY_SESSION_STATE_ATOM |
| 42 | : environmentSession.sessionStateAtom(environmentId), |
| 43 | (result) => sessionHasScope(result, AuthRelayConfigureScope), |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | export function readEnvironmentCanConfigureRelay(environmentId: EnvironmentId): boolean { |
| 48 | const result = appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)); |
| 49 | return sessionHasScope(result, AuthRelayConfigureScope); |
| 50 | } |
| 51 | |
| 52 | export function sessionHasScope( |
| 53 | result: AsyncResult.AsyncResult<AuthSessionState, unknown>, |
| 54 | scope: AuthEnvironmentScope, |
| 55 | ): boolean { |
| 56 | const session = AsyncResult.getOrElse(result, () => null); |
| 57 | return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope); |
| 58 | } |
| 59 | |
| 60 | const environmentsWithScopeAtom = Atom.family((key: string) => { |
| 61 | const [scope, idsKey = ""] = key.split(/\n(.*)/s) as [AuthEnvironmentScope, string?]; |
| 62 | const ids = idsKey === "" ? [] : (idsKey.split("\n") as EnvironmentId[]); |
| 63 | const statesAtom = environmentSessionStatesAtom(idsKey); |
| 64 | return Atom.make((get) => { |
| 65 | const states = get(statesAtom); |
| 66 | return new Set(ids.filter((_, index) => sessionHasScope(states[index]!, scope))); |
| 67 | }).pipe(Atom.withLabel(`web-environments-with-scope:${key}`)); |
| 68 | }); |
| 69 | |
| 70 | export function useEnvironmentsWithScope( |
| 71 | environments: ReadonlyArray<{ readonly environmentId: EnvironmentId }>, |
| 72 | scope: AuthEnvironmentScope, |
| 73 | ): ReadonlySet<EnvironmentId> { |
| 74 | return useAtomValue( |
| 75 | environmentsWithScopeAtom( |
| 76 | `${scope}\n${environmentIdsKey(environments.map(({ environmentId }) => environmentId))}`, |
| 77 | ), |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | export function readEnvironmentScope( |
| 82 | environmentId: EnvironmentId, |
| 83 | scope: AuthEnvironmentScope, |
| 84 | ): boolean { |
| 85 | return sessionHasScope( |
| 86 | appAtomRegistry.get(environmentSession.sessionStateAtom(environmentId)), |
| 87 | scope, |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe( |
| 92 | Atom.withLabel("web-prepared-connection:empty"), |
| 93 | ); |
| 94 | |
| 95 | export function usePreparedConnection(environmentId: EnvironmentId | null) { |
| 96 | return useAtomValue( |
| 97 | environmentId === null |
| 98 | ? EMPTY_PREPARED_CONNECTION_ATOM |
| 99 | : environmentSession.preparedConnectionValueAtom(environmentId), |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** Each environment's prepared connection, in the order of the ids. */ |
| 104 | export const preparedConnectionsAtom = environmentAtomList( |
| 105 | "web-prepared-connections", |
| 106 | environmentSession.preparedConnectionValueAtom, |
| 107 | ); |
| 108 | |
| 109 | export function readPreparedConnection(environmentId: EnvironmentId) { |
| 110 | return Option.getOrNull( |
| 111 | appAtomRegistry.get(environmentSession.preparedConnectionValueAtom(environmentId)), |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** data survives SWR revalidation; isPending applies only before first resolve. */ |
| 116 | export function useEnvironmentSessionState(environmentId: EnvironmentId | null) { |
| 117 | const result = useAtomValue( |
| 118 | environmentId === null |
| 119 | ? EMPTY_SESSION_STATE_ATOM |
| 120 | : environmentSession.sessionStateAtom(environmentId), |
| 121 | ); |
| 122 | return { |
| 123 | data: AsyncResult.getOrElse(result, () => null), |
| 124 | hasError: result._tag === "Failure", |
| 125 | isPending: result.waiting, |
| 126 | }; |
| 127 | } |