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

previewWebviewConfigState.ts

107 lines3.8 KB

1import { useAtomValue } from "@effect/atom-react";
2import type {
3 DesktopPreviewBridge,
4 DesktopPreviewWebviewConfig,
5 EnvironmentId,
6} from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Schema from "effect/Schema";
9import { AsyncResult, Atom } from "effect/reactivity";
10​
11import { previewBridge } from "~/components/preview/previewBridge";
12import { environmentCatalog } from "~/connection/catalog";
13​
14import { tunnelsEnvironmentLoopback } from "./browserTargetResolver";
15​
16const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000;
17const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000;
18​
19export class PreviewWebviewBridgeUnavailableError extends Schema.TaggedError<PreviewWebviewBridgeUnavailableError>()(
20 "PreviewWebviewBridgeUnavailableError",
21 { environmentId: Schema.String },
22) {
23 override get message(): string {
24 return `Desktop preview configuration is unavailable for environment "${this.environmentId}".`;
25 }
27​
28export class PreviewWebviewConfigLoadError extends Schema.TaggedError<PreviewWebviewConfigLoadError>()(
29 "PreviewWebviewConfigLoadError",
30 {
31 environmentId: Schema.String,
32 cause: Schema.Defect(),
33 },
34) {
35 override get message(): string {
36 return `Failed to load desktop preview configuration for environment "${this.environmentId}".`;
37 }
39​
40export const PreviewWebviewConfigError = Schema.Union([
41 PreviewWebviewBridgeUnavailableError,
42 PreviewWebviewConfigLoadError,
43]);
44export type PreviewWebviewConfigError = typeof PreviewWebviewConfigError.Type;
45​
46type PreviewConfigBridge = Pick<DesktopPreviewBridge, "getPreviewConfig">;
47​
48const loadPreviewWebviewConfig = (
49 environmentId: EnvironmentId,
50 profileId: string | undefined,
51 tunnelLoopback: boolean,
52 bridge: PreviewConfigBridge | null = previewBridge,
53): Effect.Effect<DesktopPreviewWebviewConfig, PreviewWebviewConfigError> => {
54 if (bridge === null) {
55 return Effect.fail(new PreviewWebviewBridgeUnavailableError({ environmentId }));
56 }
57​
58 return Effect.tryPromise({
59 try: () => bridge.getPreviewConfig(environmentId, profileId, tunnelLoopback),
60 catch: (cause) => new PreviewWebviewConfigLoadError({ environmentId, cause }),
61 });
62};
63​
64// Atom.family needs a primitive key; keep profile last so delimiter-containing ids round-trip.
65const CONFIG_KEY_DELIMITER = "\u0000";
66​
67const configKey = (environmentId: EnvironmentId, profileId: string | undefined): string =>
68 `${environmentId}${CONFIG_KEY_DELIMITER}${profileId ?? ""}`;
69​
70const parseConfigKey = (key: string): { environmentId: EnvironmentId; profileId?: string } => {
71 const delimiter = key.indexOf(CONFIG_KEY_DELIMITER);
72 const environmentId = (delimiter === -1 ? key : key.slice(0, delimiter)) as EnvironmentId;
73 const profileId = delimiter === -1 ? "" : key.slice(delimiter + CONFIG_KEY_DELIMITER.length);
74 return {
75 environmentId,
76 ...(profileId === "" ? {} : { profileId }),
77 };
78};
79​
80const previewWebviewConfigAtom = Atom.family((key: string) => {
81 const { environmentId, profileId } = parseConfigKey(key);
82 // The catalog knows the target before the environment connects, and a change re-routes the partition.
83 return Atom.make((get) =>
84 loadPreviewWebviewConfig(
85 environmentId,
86 profileId,
87 tunnelsEnvironmentLoopback(
88 get(environmentCatalog.catalogValueAtom).entries.get(environmentId)?.target,
89 ),
90 ),
91 ).pipe(
92 Atom.swr({
93 staleTime: PREVIEW_CONFIG_STALE_TIME_MS,
94 revalidateOnMount: true,
95 }),
96 Atom.setIdleTTL(PREVIEW_CONFIG_IDLE_TTL_MS),
97 Atom.withLabel(`preview:webview-config:${key}`),
98 );
99});
100​
101export function usePreviewWebviewConfig(
102 environmentId: EnvironmentId,
103 profileId?: string,
104): DesktopPreviewWebviewConfig | null {
105 const result = useAtomValue(previewWebviewConfigAtom(configKey(environmentId, profileId)));
106 return AsyncResult.getOrElse(result, () => null);