forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagedRelayState.ts
More file actions
123 lines (111 loc) · 3.89 KB
/
Copy pathmanagedRelayState.ts
File metadata and controls
123 lines (111 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useAtomValue } from "@effect/atom-react";
import {
createManagedRelayQueryManager,
deregisterManagedRelayEnvironment,
ManagedRelay,
managedRelaySessionAtom,
readManagedRelaySnapshotState,
} from "@t3tools/client-runtime/relay";
import {
createAtomCommandScheduler,
createRuntimeCommand,
} from "@t3tools/client-runtime/state/runtime";
import type {
RelayClientDeviceRecord,
RelayClientEnvironmentRecord,
} from "@t3tools/contracts/relay";
import type { EnvironmentId } from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { AsyncResult, Atom } from "effect/unstable/reactivity";
import { useCallback, useEffect } from "react";
import { runtime } from "../lib/runtime";
import { appAtomRegistry } from "../rpc/atomRegistry";
const managedRelayAtomRuntime = Atom.runtime(
Layer.effect(
ManagedRelay.ManagedRelayClient,
runtime.contextEffect.pipe(
Effect.map((context) => Context.get(context, ManagedRelay.ManagedRelayClient)),
),
),
);
export const managedRelayQueryManager = createManagedRelayQueryManager(managedRelayAtomRuntime);
const managedRelayMutationScheduler = createAtomCommandScheduler();
export const deregisterManagedRelayEnvironmentCommand = createRuntimeCommand(
managedRelayAtomRuntime,
{
label: "web:managed-relay:deregister-environment",
scheduler: managedRelayMutationScheduler,
concurrency: {
mode: "serial",
key: (input: { readonly accountId: string; readonly environmentId: EnvironmentId }) =>
input.accountId,
},
execute: (input, registry) => deregisterManagedRelayEnvironment(registry, input),
},
);
const EMPTY_ENVIRONMENTS_ATOM = Atom.make(
AsyncResult.success<ReadonlyArray<RelayClientEnvironmentRecord>>([]),
).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:web:environments:null"));
const EMPTY_DEVICES_ATOM = Atom.make(
AsyncResult.success<ReadonlyArray<RelayClientDeviceRecord>>([]),
).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:web:devices:null"));
export function useManagedRelayEnvironments() {
const session = useAtomValue(managedRelaySessionAtom);
const accountId = session?.accountId ?? null;
const atom = accountId
? managedRelayQueryManager.environmentsAtom(accountId)
: EMPTY_ENVIRONMENTS_ATOM;
const result = useAtomValue(atom);
const snapshot = readManagedRelaySnapshotState(result);
useEffect(() => {
if (snapshot.error) {
console.error("[t3-cloud] Relay environment listing failed", {
message: snapshot.error,
traceId: snapshot.errorTraceId,
});
}
}, [snapshot.error, snapshot.errorTraceId]);
const refresh = useCallback(() => {
if (accountId) {
managedRelayQueryManager.refreshEnvironments(appAtomRegistry, accountId);
}
}, [accountId]);
return {
...snapshot,
accountId,
refresh,
};
}
export function useManagedRelayDevices() {
const session = useAtomValue(managedRelaySessionAtom);
const accountId = session?.accountId ?? null;
const atom = accountId ? managedRelayQueryManager.devicesAtom(accountId) : EMPTY_DEVICES_ATOM;
const result = useAtomValue(atom);
const snapshot = readManagedRelaySnapshotState(result);
useEffect(() => {
if (snapshot.error) {
console.error("[t3-cloud] Relay device listing failed", {
message: snapshot.error,
traceId: snapshot.errorTraceId,
});
}
}, [snapshot.error, snapshot.errorTraceId]);
const refresh = useCallback(() => {
if (accountId) {
managedRelayQueryManager.refreshDevices(appAtomRegistry, accountId);
}
}, [accountId]);
return {
...snapshot,
accountId,
refresh,
};
}
export function refreshManagedRelayEnvironments(): void {
const session = appAtomRegistry.get(managedRelaySessionAtom);
if (session) {
managedRelayQueryManager.refreshEnvironments(appAtomRegistry, session.accountId);
}
}