Open History to see the last commit
1import type {
2 AssetCreateUrlResult,
3 AssetResource,
4 EnvironmentId,
5 PreviewOpenInput,
6 PreviewSessionSnapshot,
7 ScopedThreadRef,
8} from "@t3tools/contracts";
9import {
10 type AtomCommandResult,
11 mapAtomCommandResult,
12} from "@t3tools/client-runtime/state/runtime";
13import * as Cause from "effect/Cause";
14import * as Data from "effect/Data";
15import { AsyncResult } from "effect/unstable/reactivity";
16
17import { resolveAssetUrl } from "~/assets/assetUrls";
18import {
19 applyPreviewServerSnapshot,
20 isPreviewSupportedInRuntime,
21 rememberPreviewUrl,
22} from "~/previewStateStore";
23import { useRightPanelStore } from "~/rightPanelStore";
24
25export const isBrowserPreviewFile = (path: string): boolean =>
26 /\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
27
28export class BrowserPreviewUnavailableError extends Data.TaggedError(
29 "BrowserPreviewUnavailableError",
30)<{
31 readonly message: string;
32}> {}
33
34export type OpenPreviewMutation<E = unknown> = (input: {
35 readonly environmentId: EnvironmentId;
36 readonly input: PreviewOpenInput;
37}) => Promise<AtomCommandResult<PreviewSessionSnapshot, E>>;
38
39export async function openUrlInPreview<E>(input: {
40 readonly threadRef: ScopedThreadRef;
41 readonly url: string;
42 readonly openPreview: OpenPreviewMutation<E>;
43}): Promise<AtomCommandResult<void, E>> {
44 const result = await input.openPreview({
45 environmentId: input.threadRef.environmentId,
46 input: { threadId: input.threadRef.threadId, url: input.url },
47 });
48 return mapAtomCommandResult(result, (snapshot) => {
49 applyPreviewServerSnapshot(input.threadRef, snapshot);
50 rememberPreviewUrl(input.threadRef, input.url);
51 useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
52 });
53}
54
55export async function openFileInPreview<AssetError, PreviewError>(input: {
56 readonly threadRef: ScopedThreadRef;
57 readonly filePath: string;
58 readonly httpBaseUrl: string;
59 readonly createAssetUrl: (input: {
60 readonly environmentId: EnvironmentId;
61 readonly input: { readonly resource: AssetResource };
62 }) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>;
63 readonly openPreview: OpenPreviewMutation<PreviewError>;
64}): Promise<AtomCommandResult<void, AssetError | PreviewError | BrowserPreviewUnavailableError>> {
65 if (!isPreviewSupportedInRuntime()) {
66 return AsyncResult.failure(
67 Cause.fail(
68 new BrowserPreviewUnavailableError({
69 message: "The integrated browser is unavailable in this runtime.",
70 }),
71 ),
72 );
73 }
74 const assetResult = await input.createAssetUrl({
75 environmentId: input.threadRef.environmentId,
76 input: {
77 resource: {
78 _tag: "workspace-file",
79 threadId: input.threadRef.threadId,
80 path: input.filePath,
81 },
82 },
83 });
84 if (assetResult._tag === "Failure") {
85 return AsyncResult.failure(assetResult.cause);
86 }
87 const assetUrl = resolveAssetUrl(input.httpBaseUrl, assetResult.value.relativeUrl);
88 if (assetUrl === null) {
89 return AsyncResult.failure(
90 Cause.die(new Error("The environment returned an invalid asset URL.")),
91 );
92 }
93 return openUrlInPreview({
94 threadRef: input.threadRef,
95 url: assetUrl,
96 openPreview: input.openPreview,
97 });
98}