| ... | ... | @@ -1,8 +1,12 @@ |
| 1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | | import type { DesktopPreviewWebviewConfig, EnvironmentId } from "@t3tools/contracts"; |
| 3 | | import * as Data from "effect/Data"; |
| 2 | import type { |
| 3 | DesktopPreviewBridge, |
| 4 | DesktopPreviewWebviewConfig, |
| 5 | EnvironmentId, |
| 6 | } from "@t3tools/contracts"; |
| 4 | 7 | import * as Effect from "effect/Effect"; |
| 5 | 8 | import * as Option from "effect/Option"; |
| 9 | import * as Schema from "effect/Schema"; |
| 6 | 10 | import { AsyncResult, Atom } from "effect/unstable/reactivity"; |
| 7 | 11 | |
| 8 | 12 | import { previewBridge } from "~/components/preview/previewBridge"; |
| ... | ... | @@ -10,27 +14,51 @@ import { previewBridge } from "~/components/preview/previewBridge"; |
| 10 | 14 | const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000; |
| 11 | 15 | const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000; |
| 12 | 16 | |
| 13 | | class PreviewWebviewConfigError extends Data.TaggedError("PreviewWebviewConfigError")<{ |
| 14 | | readonly message: string; |
| 15 | | readonly cause?: unknown; |
| 16 | | }> {} |
| 17 | export class PreviewWebviewBridgeUnavailableError extends Schema.TaggedErrorClass<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 | |
| 26 | export class PreviewWebviewConfigLoadError extends Schema.TaggedErrorClass<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 | |
| 38 | export const PreviewWebviewConfigError = Schema.Union([ |
| 39 | PreviewWebviewBridgeUnavailableError, |
| 40 | PreviewWebviewConfigLoadError, |
| 41 | ]); |
| 42 | export type PreviewWebviewConfigError = typeof PreviewWebviewConfigError.Type; |
| 43 | |
| 44 | type PreviewConfigBridge = Pick<DesktopPreviewBridge, "getPreviewConfig">; |
| 45 | |
| 46 | export const loadPreviewWebviewConfig = ( |
| 47 | environmentId: EnvironmentId, |
| 48 | bridge: PreviewConfigBridge | null = previewBridge, |
| 49 | ): Effect.Effect<DesktopPreviewWebviewConfig, PreviewWebviewConfigError> => { |
| 50 | if (bridge === null) { |
| 51 | return Effect.fail(new PreviewWebviewBridgeUnavailableError({ environmentId })); |
| 52 | } |
| 53 | |
| 54 | return Effect.tryPromise({ |
| 55 | try: () => bridge.getPreviewConfig(environmentId), |
| 56 | catch: (cause) => new PreviewWebviewConfigLoadError({ environmentId, cause }), |
| 57 | }); |
| 58 | }; |
| 17 | 59 | |
| 18 | 60 | const previewWebviewConfigAtom = Atom.family((environmentId: EnvironmentId) => |
| 19 | | Atom.make( |
| 20 | | Effect.tryPromise({ |
| 21 | | try: () => { |
| 22 | | if (!previewBridge) { |
| 23 | | throw new Error("Desktop preview bridge is unavailable."); |
| 24 | | } |
| 25 | | return previewBridge.getPreviewConfig(environmentId); |
| 26 | | }, |
| 27 | | catch: (cause) => |
| 28 | | new PreviewWebviewConfigError({ |
| 29 | | message: "Could not load desktop preview configuration.", |
| 30 | | cause, |
| 31 | | }), |
| 32 | | }), |
| 33 | | ).pipe( |
| 61 | Atom.make(loadPreviewWebviewConfig(environmentId)).pipe( |
| 34 | 62 | Atom.swr({ |
| 35 | 63 | staleTime: PREVIEW_CONFIG_STALE_TIME_MS, |
| 36 | 64 | revalidateOnMount: true, |