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 | AuthRelayReadScope, |
| 4 | type EnvironmentCloudLinkStateResult, |
| 5 | type EnvironmentId, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 8 | import * as Cause from "effect/Cause"; |
| 9 | import { useGT, useMessages } from "gt-react"; |
| 10 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 11 | |
| 12 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 13 | import { usePrimaryEnvironmentId } from "../state/environments"; |
| 14 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 15 | import { readEnvironmentScope, useEnvironmentScope } from "../state/session"; |
| 16 | import { readCloudLinkState, type CloudLinkTarget } from "./linkEnvironment"; |
| 17 | import { hasCloudPublicConfig } from "./publicConfig"; |
| 18 | |
| 19 | const cloudLinkStateAtom = Atom.family((environmentId: EnvironmentId) => |
| 20 | connectionAtomRuntime |
| 21 | .atom(readCloudLinkState({ target: { environmentId } })) |
| 22 | .pipe( |
| 23 | Atom.swr({ staleTime: 5_000, revalidateOnMount: true }), |
| 24 | Atom.setIdleTTL(5 * 60_000), |
| 25 | Atom.withLabel(`cloud-link:${environmentId}`), |
| 26 | ), |
| 27 | ); |
| 28 | |
| 29 | const EMPTY_CLOUD_LINK_STATE_ATOM = Atom.make( |
| 30 | AsyncResult.success<EnvironmentCloudLinkStateResult | null>(null), |
| 31 | ).pipe(Atom.keepAlive, Atom.withLabel("cloud-link:null")); |
| 32 | |
| 33 | function refreshCloudLinkState(target: CloudLinkTarget | null): void { |
| 34 | if (target && readEnvironmentScope(target.environmentId, AuthRelayReadScope)) { |
| 35 | appAtomRegistry.refresh(cloudLinkStateAtom(target.environmentId)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export function readCachedCloudLinkState(target: CloudLinkTarget) { |
| 40 | if (!readEnvironmentScope(target.environmentId, AuthRelayReadScope)) return null; |
| 41 | const result = appAtomRegistry.get(cloudLinkStateAtom(target.environmentId)); |
| 42 | return result._tag === "Success" ? result.value : null; |
| 43 | } |
| 44 | |
| 45 | export function useCloudLinkState(environmentId: EnvironmentId | null) { |
| 46 | const gt = useGT(); |
| 47 | const resolve = useMessages(); |
| 48 | const canReadRelay = useEnvironmentScope(environmentId, AuthRelayReadScope); |
| 49 | const target: CloudLinkTarget | null = environmentId === null ? null : { environmentId }; |
| 50 | const atom = |
| 51 | environmentId !== null && hasCloudPublicConfig() && canReadRelay |
| 52 | ? cloudLinkStateAtom(environmentId) |
| 53 | : EMPTY_CLOUD_LINK_STATE_ATOM; |
| 54 | const result = useAtomValue(atom); |
| 55 | const refresh = () => { |
| 56 | refreshCloudLinkState(target); |
| 57 | }; |
| 58 | let error: string | null = null; |
| 59 | if (result._tag === "Failure") { |
| 60 | const cause = Cause.squash(result.cause); |
| 61 | error = |
| 62 | (cause instanceof Error ? thrownErrorMessage(cause, resolve) : null) ?? |
| 63 | gt("Could not read T3 Connect link state."); |
| 64 | } |
| 65 | |
| 66 | return { |
| 67 | data: result._tag === "Failure" ? null : AsyncResult.getOrElse(result, () => null), |
| 68 | error, |
| 69 | isPending: result.waiting, |
| 70 | refresh, |
| 71 | target, |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | export function usePrimaryCloudLinkState() { |
| 76 | return useCloudLinkState(usePrimaryEnvironmentId()); |
| 77 | } |