The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | import { resolveBrowserDefaults } from "~/browser/browserDefaults"; |
| 6 | import { isWebUrl, resolveBrowserLinkTargetPreference } from "~/browser/browserLinkTarget"; |
| 7 | import type { OpenPreviewMutation } from "~/browser/openFileInPreview"; |
| 8 | import { isPreviewAvailableFor, previewRuntimeFor } from "~/browser/previewRuntime"; |
| 9 | import { recordVisitForThread } from "~/browserHistoryStore"; |
| 10 | import { applyPreviewServerSnapshot } from "~/previewStateStore"; |
| 11 | import { useRightPanelStore } from "~/rightPanelStore"; |
| 12 | |
| 13 | const terminalLinkErrorContext = { |
| 14 | environmentId: Schema.String, |
| 15 | threadId: Schema.String, |
| 16 | targetOrigin: Schema.String, |
| 17 | cause: Schema.Defect(), |
| 18 | }; |
| 19 | |
| 20 | export class TerminalLinkPreviewOpenError extends Schema.TaggedError<TerminalLinkPreviewOpenError>()( |
| 21 | "TerminalLinkPreviewOpenError", |
| 22 | terminalLinkErrorContext, |
| 23 | ) { |
| 24 | override get message(): string { |
| 25 | return `Failed to open terminal link ${this.targetOrigin} in preview for thread ${this.threadId}.`; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | interface OpenTerminalLinkInPreviewInput<E> { |
| 30 | readonly url: string; |
| 31 | readonly threadRef: ScopedThreadRef; |
| 32 | readonly openPreview: OpenPreviewMutation<E>; |
| 33 | readonly fallbackToBrowser: () => void; |
| 34 | readonly forceBrowser: boolean; |
| 35 | } |
| 36 | |
| 37 | export async function openTerminalLinkInPreview<E>( |
| 38 | input: OpenTerminalLinkInPreviewInput<E>, |
| 39 | ): Promise<void> { |
| 40 | const supportsPreview = |
| 41 | !input.forceBrowser && |
| 42 | isWebUrl(input.url) && |
| 43 | isPreviewAvailableFor(input.threadRef.environmentId) && |
| 44 | input.threadRef.threadId.length > 0 && |
| 45 | (await resolveBrowserLinkTargetPreference()) === "app"; |
| 46 | |
| 47 | if (!supportsPreview) { |
| 48 | input.fallbackToBrowser(); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const errorContext = { |
| 53 | environmentId: input.threadRef.environmentId, |
| 54 | threadId: input.threadRef.threadId, |
| 55 | targetOrigin: new URL(input.url).origin, |
| 56 | }; |
| 57 | |
| 58 | const defaults = await resolveBrowserDefaults(); |
| 59 | const runtime = previewRuntimeFor(input.threadRef.environmentId); |
| 60 | const result = await input.openPreview({ |
| 61 | environmentId: input.threadRef.environmentId, |
| 62 | input: { |
| 63 | threadId: input.threadRef.threadId, |
| 64 | url: input.url, |
| 65 | viewport: defaults.viewport, |
| 66 | profileId: defaults.profileId, |
| 67 | ...(runtime === undefined |
| 68 | ? {} |
| 69 | : { runtime, zoomFactor: defaults.zoomFactor, colorScheme: defaults.appearance }), |
| 70 | }, |
| 71 | }); |
| 72 | if (result._tag === "Failure") { |
| 73 | if (isAtomCommandInterrupted(result)) { |
| 74 | return; |
| 75 | } |
| 76 | console.error( |
| 77 | new TerminalLinkPreviewOpenError({ |
| 78 | ...errorContext, |
| 79 | cause: result.cause, |
| 80 | }), |
| 81 | ); |
| 82 | input.fallbackToBrowser(); |
| 83 | return; |
| 84 | } |
| 85 | recordVisitForThread(input.threadRef, input.url); |
| 86 | applyPreviewServerSnapshot(input.threadRef, result.value); |
| 87 | useRightPanelStore.getState().openBrowser(input.threadRef, result.value.tabId); |
| 88 | } |