| ... | @@ -1,8 +1,12 @@ | ... | @@ -1,8 +1,12 @@ |
| 1 | import { useAtomValue } from "@effect/atom-react"; | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import type { DesktopPreviewWebviewConfig, EnvironmentId } from "@t3tools/contracts"; | 2 | import type { |
| 3 | import * as Data from "effect/Data"; | 3 | DesktopPreviewBridge, |
| | 4 | DesktopPreviewWebviewConfig, |
| | 5 | EnvironmentId, |
| | 6 | } from "@t3tools/contracts"; |
| 4 | import * as Effect from "effect/Effect"; | 7 | import * as Effect from "effect/Effect"; |
| 5 | import * as Option from "effect/Option"; | 8 | import * as Option from "effect/Option"; |
| | 9 | import * as Schema from "effect/Schema"; |
| 6 | import { AsyncResult, Atom } from "effect/unstable/reactivity"; | 10 | import { AsyncResult, Atom } from "effect/unstable/reactivity"; |
| 7 | | 11 | |
| 8 | import { previewBridge } from "~/components/preview/previewBridge"; | 12 | import { previewBridge } from "~/components/preview/previewBridge"; |
| ... | @@ -10,27 +14,51 @@ import { previewBridge } from "~/components/preview/previewBridge"; | ... | @@ -10,27 +14,51 @@ import { previewBridge } from "~/components/preview/previewBridge"; |
| 10 | const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000; | 14 | const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000; |
| 11 | const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000; | 15 | const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000; |
| 12 | | 16 | |
| 13 | class PreviewWebviewConfigError extends Data.TaggedError("PreviewWebviewConfigError")<{ | 17 | export class PreviewWebviewBridgeUnavailableError extends Schema.TaggedErrorClass<PreviewWebviewBridgeUnavailableError>()( |
| 14 | readonly message: string; | 18 | "PreviewWebviewBridgeUnavailableError", |
| 15 | readonly cause?: unknown; | 19 | { environmentId: Schema.String }, |
| 16 | }> {} | 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 | const previewWebviewConfigAtom = Atom.family((environmentId: EnvironmentId) => | 60 | const previewWebviewConfigAtom = Atom.family((environmentId: EnvironmentId) => |
| 19 | Atom.make( | 61 | Atom.make(loadPreviewWebviewConfig(environmentId)).pipe( |
| 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( | | |
| 34 | Atom.swr({ | 62 | Atom.swr({ |
| 35 | staleTime: PREVIEW_CONFIG_STALE_TIME_MS, | 63 | staleTime: PREVIEW_CONFIG_STALE_TIME_MS, |
| 36 | revalidateOnMount: true, | 64 | revalidateOnMount: true, |