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 { |
| 3 | createEnvironmentSessionAtoms, |
| 4 | sessionDeniesScope, |
| 5 | } from "@t3tools/client-runtime/state/session"; |
| 6 | import { |
| 7 | type AuthEnvironmentScope, |
| 8 | type AuthSessionState, |
| 9 | type EnvironmentId, |
| 10 | sessionGrantsScope, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 14 | import { useMemo } from "react"; |
| 15 | |
| 16 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 17 | import { appAtomRegistry } from "./atom-registry"; |
| 18 | |
| 19 | export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRuntime); |
| 20 | |
| 21 | const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>()); |
| 22 | |
| 23 | export 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 | |
| 31 | function 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. |
| 39 | export 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. */ |
| 49 | export function useEnvironmentScopeDenied( |
| 50 | environmentId: EnvironmentId | null, |
| 51 | scope: AuthEnvironmentScope, |
| 52 | ): boolean { |
| 53 | return useAtomValue(sessionStateAtomFor(environmentId), (result) => |
| 54 | sessionDeniesScope(result, scope), |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | export 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 | |
| 78 | export 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 | |
| 86 | const EMPTY_PREPARED_CONNECTION_ATOM = Atom.make(Option.none()).pipe( |
| 87 | Atom.withLabel("mobile-prepared-connection:empty"), |
| 88 | ); |
| 89 | |
| 90 | export function usePreparedConnection(environmentId: EnvironmentId | null) { |
| 91 | return useAtomValue( |
| 92 | environmentId === null |
| 93 | ? EMPTY_PREPARED_CONNECTION_ATOM |
| 94 | : environmentSession.preparedConnectionValueAtom(environmentId), |
| 95 | ); |
| 96 | } |