Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import {
3 AuthRelayReadScope,
4 EnvironmentId,
5 type EnvironmentCloudLinkStateResult,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import { AsyncResult, Atom } from "effect/reactivity";
12import { HttpClient } from "effect/http";
13
14import { usePrimaryEnvironment } from "../state/environments";
15import { runtime } from "../lib/runtime";
16import { appAtomRegistry } from "../rpc/atomRegistry";
17import { readEnvironmentScope, useEnvironmentScope } from "../state/session";
18import { readPrimaryCloudLinkState, type CloudLinkTarget } from "./linkEnvironment";
19import { hasCloudPublicConfig } from "./publicConfig";
20
21const primaryCloudLinkAtomRuntime = Atom.runtime(
22 Layer.effect(
23 HttpClient.HttpClient,
24 runtime.contextEffect.pipe(
25 Effect.map((context) => Context.get(context, HttpClient.HttpClient)),
26 ),
27 ),
28);
29
30const primaryCloudLinkStateAtom = Atom.family((key: string) => {
31 const target = JSON.parse(key) as CloudLinkTarget;
32 return primaryCloudLinkAtomRuntime
33 .atom(readPrimaryCloudLinkState({ target }))
34 .pipe(
35 Atom.swr({ staleTime: 5_000, revalidateOnMount: true }),
36 Atom.setIdleTTL(5 * 60_000),
37 Atom.withLabel(`primary-cloud-link:${target.environmentId}`),
38 );
39});
40
41const EMPTY_PRIMARY_CLOUD_LINK_STATE_ATOM = Atom.make(
42 AsyncResult.success<EnvironmentCloudLinkStateResult | null>(null),
43).pipe(Atom.keepAlive, Atom.withLabel("primary-cloud-link:null"));
44
45function targetKey(target: CloudLinkTarget): string {
46 return JSON.stringify(target);
47}
48
49function refreshPrimaryCloudLinkState(target: CloudLinkTarget | null): void {
50 if (
51 target &&
52 readEnvironmentScope(EnvironmentId.make(target.environmentId), AuthRelayReadScope)
53 ) {
54 appAtomRegistry.refresh(primaryCloudLinkStateAtom(targetKey(target)));
55 }
56}
57
58export function readCachedPrimaryCloudLinkState(target: CloudLinkTarget) {
59 if (!readEnvironmentScope(EnvironmentId.make(target.environmentId), AuthRelayReadScope))
60 return null;
61 const result = appAtomRegistry.get(primaryCloudLinkStateAtom(targetKey(target)));
62 return result._tag === "Success" ? result.value : null;
63}
64
65export function usePrimaryCloudLinkState() {
66 const primary = usePrimaryEnvironment();
67 const canReadRelay = useEnvironmentScope(primary?.environmentId ?? null, AuthRelayReadScope);
68 const target =
69 primary?.entry.target._tag === "PrimaryConnectionTarget"
70 ? {
71 environmentId: primary.environmentId,
72 label: primary.label,
73 httpBaseUrl: primary.entry.target.httpBaseUrl,
74 wsBaseUrl: primary.entry.target.wsBaseUrl,
75 }
76 : null;
77 const atom =
78 target && hasCloudPublicConfig() && canReadRelay
79 ? primaryCloudLinkStateAtom(targetKey(target))
80 : EMPTY_PRIMARY_CLOUD_LINK_STATE_ATOM;
81 const result = useAtomValue(atom);
82 const refresh = () => {
83 refreshPrimaryCloudLinkState(target);
84 };
85 let error: string | null = null;
86 if (result._tag === "Failure") {
87 const cause = Cause.squash(result.cause);
88 error = cause instanceof Error ? cause.message : "Could not read T3 Connect link state.";
89 }
90
91 return {
92 data: result._tag === "Failure" ? null : AsyncResult.getOrElse(result, () => null),
93 error,
94 isPending: result.waiting,
95 refresh,
96 target,
97 };
98}