Open History to see the last commit
1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
3import * as Schema from "effect/Schema";
4
5import {
6 browserDefaultOpenProfileId,
7 browserDefaultOpenViewport,
8 resolveBrowserDefaults,
9} from "~/browser/browserDefaults";
10import { isWebUrl, resolveBrowserLinkTargetPreference } from "~/browser/browserLinkTarget";
11import type { OpenPreviewMutation } from "~/browser/openFileInPreview";
12import { recordVisitForThread } from "~/browserHistoryStore";
13import { applyPreviewServerSnapshot, isPreviewSupportedInRuntime } from "~/previewStateStore";
14import { useRightPanelStore } from "~/rightPanelStore";
15
16const terminalLinkErrorContext = {
17 environmentId: Schema.String,
18 threadId: Schema.String,
19 targetOrigin: Schema.String,
20 cause: Schema.Defect(),
21};
22
23export class TerminalLinkPreviewOpenError extends Schema.TaggedErrorClass<TerminalLinkPreviewOpenError>()(
24 "TerminalLinkPreviewOpenError",
25 terminalLinkErrorContext,
26) {
27 override get message(): string {
28 return `Failed to open terminal link ${this.targetOrigin} in preview for thread ${this.threadId}.`;
29 }
30}
31
32interface OpenTerminalLinkInPreviewInput<E> {
33 readonly url: string;
34 readonly threadRef: ScopedThreadRef;
35 readonly openPreview: OpenPreviewMutation<E>;
36 readonly fallbackToBrowser: () => void;
37}
38
39/**
40 * Opens a terminal hyperlink where the "Open links in" setting says. Terminal
41 * links are activated with the platform modifier already held, so unlike chat
42 * links the modifier cannot double as the system-browser override; the setting
43 * alone decides, and the system browser is the fallback whenever the in-app
44 * one cannot take the URL.
45 */
46export async function openTerminalLinkInPreview<E>(
47 input: OpenTerminalLinkInPreviewInput<E>,
48): Promise<void> {
49 const supportsPreview =
50 isWebUrl(input.url) &&
51 isPreviewSupportedInRuntime() &&
52 input.threadRef.threadId.length > 0 &&
53 (await resolveBrowserLinkTargetPreference()) === "app";
54
55 if (!supportsPreview) {
56 input.fallbackToBrowser();
57 return;
58 }
59
60 const errorContext = {
61 environmentId: input.threadRef.environmentId,
62 threadId: input.threadRef.threadId,
63 targetOrigin: new URL(input.url).origin,
64 };
65
66 const defaults = await resolveBrowserDefaults();
67 const result = await input.openPreview({
68 environmentId: input.threadRef.environmentId,
69 input: {
70 threadId: input.threadRef.threadId,
71 url: input.url,
72 // Same reason as `openUrlInPreview`: this path handles its own result
73 // mapping, so the configured defaults are applied explicitly.
74 viewport: browserDefaultOpenViewport(defaults),
75 profileId: browserDefaultOpenProfileId(defaults),
76 },
77 });
78 if (result._tag === "Failure") {
79 if (isAtomCommandInterrupted(result)) {
80 return;
81 }
82 console.error(
83 new TerminalLinkPreviewOpenError({
84 ...errorContext,
85 cause: result.cause,
86 }),
87 );
88 input.fallbackToBrowser();
89 return;
90 }
91 recordVisitForThread(input.threadRef, input.url);
92 applyPreviewServerSnapshot(input.threadRef, result.value);
93 useRightPanelStore.getState().openBrowser(input.threadRef, result.value.tabId);
94}