Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openFileInPreview.ts

95 lines3.1 KB

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