The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import type { |
| 3 | DesktopPreviewBridge, |
| 4 | DesktopPreviewWebviewConfig, |
| 5 | EnvironmentId, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 10 | |
| 11 | import { previewBridge } from "~/components/preview/previewBridge"; |
| 12 | import { environmentCatalog } from "~/connection/catalog"; |
| 13 | |
| 14 | import { tunnelsEnvironmentLoopback } from "./browserTargetResolver"; |
| 15 | |
| 16 | const PREVIEW_CONFIG_STALE_TIME_MS = 5 * 60_000; |
| 17 | const PREVIEW_CONFIG_IDLE_TTL_MS = 10 * 60_000; |
| 18 | |
| 19 | export 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 | } |
| 26 | } |
| 27 | |
| 28 | export 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 | } |
| 38 | } |
| 39 | |
| 40 | export const PreviewWebviewConfigError = Schema.Union([ |
| 41 | PreviewWebviewBridgeUnavailableError, |
| 42 | PreviewWebviewConfigLoadError, |
| 43 | ]); |
| 44 | export type PreviewWebviewConfigError = typeof PreviewWebviewConfigError.Type; |
| 45 | |
| 46 | type PreviewConfigBridge = Pick<DesktopPreviewBridge, "getPreviewConfig">; |
| 47 | |
| 48 | const 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. |
| 65 | const CONFIG_KEY_DELIMITER = "\u0000"; |
| 66 | |
| 67 | const configKey = (environmentId: EnvironmentId, profileId: string | undefined): string => |
| 68 | `${environmentId}${CONFIG_KEY_DELIMITER}${profileId ?? ""}`; |
| 69 | |
| 70 | const 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 | |
| 80 | const 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 | |
| 101 | export 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); |
| 107 | } |