61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { useAtomValue } from "@effect/atom-react";
|
|
import {
|
|
createManagedRelayQueryManager,
|
|
managedRelaySessionAtom,
|
|
readManagedRelaySnapshotState,
|
|
} from "@t3tools/client-runtime/relay";
|
|
import type { RelayClientEnvironmentRecord } from "@t3tools/contracts/relay";
|
|
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
|
import { useCallback, useEffect } from "react";
|
|
|
|
import { runtimeContextLayer } from "../../lib/runtime";
|
|
import { appAtomRegistry } from "../../state/atom-registry";
|
|
import { cloudDebugLog } from "./cloudDebugLog";
|
|
|
|
const managedRelayAtomRuntime = Atom.runtime(runtimeContextLayer);
|
|
|
|
export const managedRelayQueryManager = createManagedRelayQueryManager(managedRelayAtomRuntime, {
|
|
onQueryEvent: (event) =>
|
|
cloudDebugLog(`query:${event.operation}:${event.stage}:${event.phase}`, { ...event }),
|
|
});
|
|
|
|
const EMPTY_ENVIRONMENTS_ATOM = Atom.make(
|
|
AsyncResult.success<ReadonlyArray<RelayClientEnvironmentRecord>>([]),
|
|
).pipe(Atom.keepAlive, Atom.withLabel("managed-relay:mobile:environments: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 refreshManagedRelayEnvironments(): void {
|
|
const session = appAtomRegistry.get(managedRelaySessionAtom);
|
|
if (session) {
|
|
managedRelayQueryManager.refreshEnvironments(appAtomRegistry, session.accountId);
|
|
}
|
|
}
|