Open History to see the last commit
1import type { LocalApi, ScopedThreadRef } from "@t3tools/contracts";
2import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
3import { isPreviewableUrl } from "@t3tools/shared/preview";
4import * as Schema from "effect/Schema";
5
6import type { OpenPreviewMutation } from "~/browser/openFileInPreview";
7import { applyPreviewServerSnapshot, isPreviewSupportedInRuntime } from "~/previewStateStore";
8import { useRightPanelStore } from "~/rightPanelStore";
9
10const terminalLinkErrorContext = {
11 environmentId: Schema.String,
12 threadId: Schema.String,
13 targetOrigin: Schema.String,
14 cause: Schema.Defect(),
15};
16
17export class TerminalLinkContextMenuShowError extends Schema.TaggedErrorClass<TerminalLinkContextMenuShowError>()(
18 "TerminalLinkContextMenuShowError",
19 terminalLinkErrorContext,
20) {
21 override get message(): string {
22 return `Failed to show the context menu for terminal link ${this.targetOrigin}.`;
23 }
24}
25
26export class TerminalLinkPreviewOpenError extends Schema.TaggedErrorClass<TerminalLinkPreviewOpenError>()(
27 "TerminalLinkPreviewOpenError",
28 terminalLinkErrorContext,
29) {
30 override get message(): string {
31 return `Failed to open terminal link ${this.targetOrigin} in preview for thread ${this.threadId}.`;
32 }
33}
34
35interface OpenTerminalLinkInPreviewInput<E> {
36 readonly url: string;
37 readonly position: { x: number; y: number };
38 readonly threadRef: ScopedThreadRef;
39 readonly openPreview: OpenPreviewMutation<E>;
40 readonly localApi: LocalApi;
41 readonly fallbackToBrowser: () => void;
42}
43
44export async function openTerminalLinkInPreview<E>(
45 input: OpenTerminalLinkInPreviewInput<E>,
46): Promise<void> {
47 const supportsPreview =
48 isPreviewableUrl(input.url) &&
49 isPreviewSupportedInRuntime() &&
50 input.threadRef.threadId.length > 0;
51
52 if (!supportsPreview) {
53 input.fallbackToBrowser();
54 return;
55 }
56
57 const errorContext = {
58 environmentId: input.threadRef.environmentId,
59 threadId: input.threadRef.threadId,
60 targetOrigin: new URL(input.url).origin,
61 };
62
63 let choice: "open-in-preview" | "open-in-browser" | null;
64 try {
65 choice = await input.localApi.contextMenu.show(
66 [
67 { id: "open-in-preview", label: "Open in preview" },
68 { id: "open-in-browser", label: "Open in browser" },
69 ],
70 input.position,
71 );
72 } catch (cause) {
73 console.error(
74 new TerminalLinkContextMenuShowError({
75 ...errorContext,
76 cause,
77 }),
78 );
79 input.fallbackToBrowser();
80 return;
81 }
82
83 if (choice === "open-in-preview") {
84 const result = await input.openPreview({
85 environmentId: input.threadRef.environmentId,
86 input: { threadId: input.threadRef.threadId, url: input.url },
87 });
88 if (result._tag === "Failure") {
89 if (isAtomCommandInterrupted(result)) {
90 return;
91 }
92 console.error(
93 new TerminalLinkPreviewOpenError({
94 ...errorContext,
95 cause: result.cause,
96 }),
97 );
98 input.fallbackToBrowser();
99 return;
100 }
101 applyPreviewServerSnapshot(input.threadRef, result.value);
102 useRightPanelStore.getState().openBrowser(input.threadRef, result.value.tabId);
103 return;
104 }
105
106 if (choice === "open-in-browser") {
107 input.fallbackToBrowser();
108 }
109}