Open History to see the last commit
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 Option from "effect/Option";
9import * as Schema from "effect/Schema";
10import { AsyncResult, Atom } from "effect/reactivity";
11
12import { previewBridge } from "~/components/preview/previewBridge";
13
14const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000;
15const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000;
16
17export class PreviewWebviewBridgeUnavailableError extends Schema.TaggedError<PreviewWebviewBridgeUnavailableError>()(
18 "PreviewWebviewBridgeUnavailableError",
19 { environmentId: Schema.String },
20) {
21 override get message(): string {
22 return `Desktop preview configuration is unavailable for environment "${this.environmentId}".`;
23 }
24}
25
26export class PreviewWebviewConfigLoadError extends Schema.TaggedError<PreviewWebviewConfigLoadError>()(
27 "PreviewWebviewConfigLoadError",
28 {
29 environmentId: Schema.String,
30 cause: Schema.Defect(),
31 },
32) {
33 override get message(): string {
34 return `Failed to load desktop preview configuration for environment "${this.environmentId}".`;
35 }
36}
37
38export const PreviewWebviewConfigError = Schema.Union([
39 PreviewWebviewBridgeUnavailableError,
40 PreviewWebviewConfigLoadError,
41]);
42export type PreviewWebviewConfigError = typeof PreviewWebviewConfigError.Type;
43
44type PreviewConfigBridge = Pick<DesktopPreviewBridge, "getPreviewConfig">;
45
46const loadPreviewWebviewConfig = (
47 environmentId: EnvironmentId,
48 profileId?: string,
49 bridge: PreviewConfigBridge | null = previewBridge,
50): Effect.Effect<DesktopPreviewWebviewConfig, PreviewWebviewConfigError> => {
51 if (bridge === null) {
52 return Effect.fail(new PreviewWebviewBridgeUnavailableError({ environmentId }));
53 }
54
55 return Effect.tryPromise({
56 try: () => bridge.getPreviewConfig(environmentId, profileId),
57 catch: (cause) => new PreviewWebviewConfigLoadError({ environmentId, cause }),
58 });
59};
60
61// Atom.family needs a primitive key; keep profile last so delimiter-containing ids round-trip.
62const CONFIG_KEY_DELIMITER = "\u0000";
63
64const configKey = (environmentId: EnvironmentId, profileId: string | undefined): string =>
65 `${environmentId}${CONFIG_KEY_DELIMITER}${profileId ?? ""}`;
66
67const parseConfigKey = (key: string): { environmentId: EnvironmentId; profileId?: string } => {
68 const delimiter = key.indexOf(CONFIG_KEY_DELIMITER);
69 const environmentId = (delimiter === -1 ? key : key.slice(0, delimiter)) as EnvironmentId;
70 const profileId = delimiter === -1 ? "" : key.slice(delimiter + CONFIG_KEY_DELIMITER.length);
71 return {
72 environmentId,
73 ...(profileId === "" ? {} : { profileId }),
74 };
75};
76
77const previewWebviewConfigAtom = Atom.family((key: string) => {
78 const { environmentId, profileId } = parseConfigKey(key);
79 return Atom.make(loadPreviewWebviewConfig(environmentId, profileId)).pipe(
80 Atom.swr({
81 staleTime: PREVIEW_CONFIG_STALE_TIME_MS,
82 revalidateOnMount: true,
83 }),
84 Atom.setIdleTTL(PREVIEW_CONFIG_IDLE_TTL_MS),
85 Atom.withLabel(`preview:webview-config:${key}`),
86 );
87});
88
89export function usePreviewWebviewConfig(
90 environmentId: EnvironmentId,
91 profileId?: string,
92): DesktopPreviewWebviewConfig | null {
93 const result = useAtomValue(previewWebviewConfigAtom(configKey(environmentId, profileId)));
94 return Option.getOrNull(AsyncResult.value(result));
95}