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

previewWebviewConfigState.ts

103 lines3.6 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 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 }
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 }
37​
38export const PreviewWebviewConfigError = Schema.Union([
39 PreviewWebviewBridgeUnavailableError,
40 PreviewWebviewConfigLoadError,
41]);
42export type PreviewWebviewConfigError = typeof PreviewWebviewConfigError.Type;
43​
44type PreviewConfigBridge = Pick<DesktopPreviewBridge, "getPreviewConfig">;
45​
46export const 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/**
62 * `Atom.family` keys on its argument, so the environment and profile are
63 * folded into one string: passing an object would allocate a fresh entry on
64 * every render.
65 *
66 * The profile is the tail rather than a second field, so an id containing the
67 * delimiter round-trips whole instead of being truncated into a different
68 * profile's key.
69 */
70const CONFIG_KEY_DELIMITER = "\u0000";
71​
72const configKey = (environmentId: EnvironmentId, profileId: string | undefined): string =>
73 `${environmentId}${CONFIG_KEY_DELIMITER}${profileId ?? ""}`;
74​
75const parseConfigKey = (key: string): { environmentId: EnvironmentId; profileId?: string } => {
76 const delimiter = key.indexOf(CONFIG_KEY_DELIMITER);
77 const environmentId = (delimiter === -1 ? key : key.slice(0, delimiter)) as EnvironmentId;
78 const profileId = delimiter === -1 ? "" : key.slice(delimiter + CONFIG_KEY_DELIMITER.length);
79 return {
80 environmentId,
81 ...(profileId === "" ? {} : { profileId }),
82 };
83};
84​
85const previewWebviewConfigAtom = Atom.family((key: string) => {
86 const { environmentId, profileId } = parseConfigKey(key);
87 return Atom.make(loadPreviewWebviewConfig(environmentId, profileId)).pipe(
88 Atom.swr({
89 staleTime: PREVIEW_CONFIG_STALE_TIME_MS,
90 revalidateOnMount: true,
91 }),
92 Atom.setIdleTTL(PREVIEW_CONFIG_IDLE_TTL_MS),
93 Atom.withLabel(`preview:webview-config:${key}`),
94 );
95});
96​
97export function usePreviewWebviewConfig(
98 environmentId: EnvironmentId,
99 profileId?: string,
100): DesktopPreviewWebviewConfig | null {
101 const result = useAtomValue(previewWebviewConfigAtom(configKey(environmentId, profileId)));
102 return Option.getOrNull(AsyncResult.value(result));