@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 69 lines 2.28 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import {
3 createManagedRelayQueryManager,
4 deregisterManagedRelayEnvironment,
5 managedRelaySessionAtom,
6 readManagedRelaySnapshotState,
7 } from "@t3tools/client-runtime/relay";
8 import {
9 createAtomCommandScheduler,
10 createRuntimeCommand,
11 } from "@t3tools/client-runtime/state/runtime";
12 import type { EnvironmentId } from "@t3tools/contracts";
13 import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";
14 import { AsyncResult, Atom } from "effect/reactivity";
15
16 import * as Runtime from "../../lib/runtime";
17 import { appAtomRegistry } from "../../state/atom-registry";
18
19 const managedRelayAtomRuntime = Atom.runtime(Runtime.layer);
20
21 const managedRelayQueryManager = createManagedRelayQueryManager(managedRelayAtomRuntime);
22
23 const managedRelayMutationScheduler = createAtomCommandScheduler();
24
25 export const deregisterManagedRelayEnvironmentCommand = createRuntimeCommand(
26 managedRelayAtomRuntime,
27 {
28 label: "mobile:managed-relay:deregister-environment",
29 scheduler: managedRelayMutationScheduler,
30 concurrency: {
31 mode: "serial",
32 key: (input: { readonly accountId: string; readonly environmentId: EnvironmentId }) =>
33 input.accountId,
34 },
35 execute: (input, registry) => deregisterManagedRelayEnvironment(registry, input),
36 },
37 );
38
39 const EMPTY_ENVIRONMENTS_ATOM = Atom.make(
40 AsyncResult.success<ReadonlyArray<RelayClientEnvironmentRecord>>([]),
41 ).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:mobile:environments:null"));
42
43 export function useManagedRelayEnvironments() {
44 const session = useAtomValue(managedRelaySessionAtom);
45 const accountId = session?.accountId ?? null;
46 const atom = accountId
47 ? managedRelayQueryManager.environmentsAtom(accountId)
48 : EMPTY_ENVIRONMENTS_ATOM;
49 const result = useAtomValue(atom);
50 const snapshot = readManagedRelaySnapshotState(result);
51 const refresh = () => {
52 if (accountId) {
53 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);
54 }
55 };
56
57 return {
58 ...snapshot,
59 accountId,
60 refresh,
61 };
62 }
63
64 export function refreshManagedRelayEnvironments(): void {
65 const session = appAtomRegistry.get(managedRelaySessionAtom);
66 if (session) {
67 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, session.accountId);
68 }
69 }