Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

managedRelayState.ts

100 lines3.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import {
3 createManagedRelayQueryManager,
4 managedRelaySessionAtom,
5 readManagedRelaySnapshotState,
6} from "@t3tools/client-runtime/relay";
7import type {
8 RelayClientEnvironmentRecord,
9 RelayEnvironmentStatusResponse,
10} from "@t3tools/contracts/relay";
11import { AsyncResult, Atom } from "effect/unstable/reactivity";
12import { useCallback, useEffect } from "react";
13​
14import { runtimeContextLayer } from "../../lib/runtime";
15import { appAtomRegistry } from "../../state/atom-registry";
16import { cloudDebugLog } from "./cloudDebugLog";
17​
18const managedRelayAtomRuntime = Atom.runtime(runtimeContextLayer);
19​
20export const managedRelayQueryManager = createManagedRelayQueryManager(managedRelayAtomRuntime, {
21 onQueryEvent: (event) =>
22 cloudDebugLog(`query:${event.operation}:${event.stage}:${event.phase}`, { ...event }),
23});
24​
25const EMPTY_ENVIRONMENTS_ATOM = Atom.make(
26 AsyncResult.success<ReadonlyArray<RelayClientEnvironmentRecord>>([]),
27).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:mobile:environments:null"));
28​
29const EMPTY_ENVIRONMENT_STATUS_ATOM = Atom.make(
30 AsyncResult.initial<RelayEnvironmentStatusResponse, never>(false),
31).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:mobile:environment-status:null"));
32​
33export function useManagedRelayEnvironments() {
34 const session = useAtomValue(managedRelaySessionAtom);
35 const accountId = session?.accountId ?? null;
36 const atom = accountId
37 ? managedRelayQueryManager.environmentsAtom(accountId)
38 : EMPTY_ENVIRONMENTS_ATOM;
39 const result = useAtomValue(atom);
40 const snapshot = readManagedRelaySnapshotState(result);
41 useEffect(() => {
42 if (snapshot.error) {
43 console.error("[t3-cloud] Relay environment listing failed", {
44 message: snapshot.error,
45 traceId: snapshot.errorTraceId,
46 });
47 }
48 }, [snapshot.error, snapshot.errorTraceId]);
49 const refresh = useCallback(() => {
50 if (accountId) {
51 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);
52 }
53 }, [accountId]);
54​
55 return {
56 ...snapshot,
57 accountId,
58 refresh,
59 };
61​
62export function useManagedRelayEnvironmentStatus(environment: RelayClientEnvironmentRecord) {
63 const session = useAtomValue(managedRelaySessionAtom);
64 const accountId = session?.accountId ?? null;
65 const atom = accountId
66 ? managedRelayQueryManager.environmentStatusAtom({ accountId, environment })
67 : EMPTY_ENVIRONMENT_STATUS_ATOM;
68 const result = useAtomValue(atom);
69 const snapshot = readManagedRelaySnapshotState(result);
70 useEffect(() => {
71 if (snapshot.error) {
72 console.error("[t3-cloud] Relay environment status failed", {
73 environmentId: environment.environmentId,
74 message: snapshot.error,
75 traceId: snapshot.errorTraceId,
76 });
77 }
78 }, [environment.environmentId, snapshot.error, snapshot.errorTraceId]);
79 const refresh = useCallback(() => {
80 if (accountId) {
81 managedRelayQueryManager.refreshEnvironmentStatus(appAtomRegistry, {
82 accountId,
83 environment,
84 });
85 }
86 }, [accountId, environment]);
87​
88 return {
89 ...snapshot,
90 accountId,
91 refresh,
92 };
94​
95export function refreshManagedRelayEnvironments(): void {
96 const session = appAtomRegistry.get(managedRelaySessionAtom);
97 if (session) {
98 managedRelayQueryManager.refreshEnvironments(appAtomRegistry, session.accountId);
99 }