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

openFileInPreview.ts

106 lines3.6 KB

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