Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 createManagedRelayQueryManager,
4 ManagedRelay,
5 managedRelaySessionAtom,
6 readManagedRelaySnapshotState,
7} from "@t3tools/client-runtime/relay";
8import type {
9 RelayClientDeviceRecord,
10 RelayClientEnvironmentRecord,
11} from "@t3tools/contracts/relay";
12import * as Context from "effect/Context";
13import * as Effect from "effect/Effect";
14import * as Layer from "effect/Layer";
15import { AsyncResult, Atom } from "effect/unstable/reactivity";
16import { useCallback, useEffect } from "react";
17
18import { runtime } from "../lib/runtime";
19import { appAtomRegistry } from "../rpc/atomRegistry";
20
21const managedRelayAtomRuntime = Atom.runtime(
22 Layer.effect(
23 ManagedRelay.ManagedRelayClient,
24 runtime.contextEffect.pipe(
25 Effect.map((context) => Context.get(context, ManagedRelay.ManagedRelayClient)),
26 ),
27 ),
28);
29
30export const managedRelayQueryManager = createManagedRelayQueryManager(managedRelayAtomRuntime);
31
32const EMPTY_ENVIRONMENTS_ATOM = Atom.make(
33 AsyncResult.success<ReadonlyArray<RelayClientEnvironmentRecord>>([]),
34).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:web:environments:null"));
35
36const EMPTY_DEVICES_ATOM = Atom.make(
37 AsyncResult.success<ReadonlyArray<RelayClientDeviceRecord>>([]),
38).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:web:devices:null"));
39
40export function useManagedRelayEnvironments() {
41 const session = useAtomValue(managedRelaySessionAtom);
42 const accountId = session?.accountId ?? null;
43 const atom = accountId
44 ? managedRelayQueryManager.environmentsAtom(accountId)
45 : EMPTY_ENVIRONMENTS_ATOM;
46 const result = useAtomValue(atom);
47 const snapshot = readManagedRelaySnapshotState(result);
48 useEffect(() => {
49 if (snapshot.error) {
50 console.error("[t3-cloud] Relay environment listing failed", {
51 message: snapshot.error,
52 traceId: snapshot.errorTraceId,
53 });
54 }
55 }, [snapshot.error, snapshot.errorTraceId]);
56 const refresh = useCallback(() => {
57 if (accountId) {
58 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);
59 }
60 }, [accountId]);
61
62 return {
63 ...snapshot,
64 accountId,
65 refresh,
66 };
67}
68
69export function useManagedRelayDevices() {
70 const session = useAtomValue(managedRelaySessionAtom);
71 const accountId = session?.accountId ?? null;
72 const atom = accountId ? managedRelayQueryManager.devicesAtom(accountId) : EMPTY_DEVICES_ATOM;
73 const result = useAtomValue(atom);
74 const snapshot = readManagedRelaySnapshotState(result);
75 useEffect(() => {
76 if (snapshot.error) {
77 console.error("[t3-cloud] Relay device listing failed", {
78 message: snapshot.error,
79 traceId: snapshot.errorTraceId,
80 });
81 }
82 }, [snapshot.error, snapshot.errorTraceId]);
83 const refresh = useCallback(() => {
84 if (accountId) {
85 managedRelayQueryManager.refreshDevices(appAtomRegistry, accountId);
86 }
87 }, [accountId]);
88
89 return {
90 ...snapshot,
91 accountId,
92 refresh,
93 };
94}
95
96export function refreshManagedRelayEnvironments(): void {
97 const session = appAtomRegistry.get(managedRelaySessionAtom);
98 if (session) {
99 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, session.accountId);
100 }
101}