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

managedRelayState.ts

98 lines3.2 KB

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