102 lines
3.2 KiB
TypeScript
102 lines
3.2 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import {
|
|
createManagedRelayQueryManager,
|
|
ManagedRelay,
|
|
managedRelaySessionAtom,
|
|
readManagedRelaySnapshotState,
|
|
} from "@t3tools/client-runtime/relay";
|
|
import type {
|
|
RelayClientDeviceRecord,
|
|
RelayClientEnvironmentRecord,
|
|
} from "@t3tools/contracts/relay";
|
|
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 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);
|
|
}
|
|
}
|