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

openFileInPreview.ts

121 lines4.1 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​
26import {
27 browserDefaultOpenProfileId,
28 browserDefaultOpenViewport,
29 resolveBrowserDefaults,
30} from "./browserDefaults";
31​
32export const isBrowserPreviewFile = (path: string): boolean =>
33 /\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
34​
35export class BrowserPreviewUnavailableError extends Data.TaggedError(
36 "BrowserPreviewUnavailableError",
37)<{
38 readonly message: string;
39}> {}
40​
41export type OpenPreviewMutation<E = unknown> = (input: {
42 readonly environmentId: EnvironmentId;
43 readonly input: PreviewOpenInput;
44}) => Promise<AtomCommandResult<PreviewSessionSnapshot, E>>;
45​
46export async function openUrlInPreview<E>(input: {
47 readonly threadRef: ScopedThreadRef;
48 readonly url: string;
49 readonly openPreview: OpenPreviewMutation<E>;
50}): Promise<AtomCommandResult<void, E>> {
51 const defaults = await resolveBrowserDefaults();
52 const result = await input.openPreview({
53 environmentId: input.threadRef.environmentId,
54 input: {
55 threadId: input.threadRef.threadId,
56 url: input.url,
57 // Built here rather than via `openPreviewSession` because this path
58 // maps the result differently, so the configured defaults have to be
59 // applied explicitly or file/link opens would ignore them.
60 viewport: browserDefaultOpenViewport(defaults),
61 profileId: browserDefaultOpenProfileId(defaults),
62 },
63 });
64 return mapAtomCommandResult(result, (snapshot) => {
65 applyPreviewServerSnapshot(input.threadRef, snapshot);
66 rememberPreviewUrl(input.threadRef, input.url);
67 useRightPanelStore.getState().openBrowser(input.threadRef, snapshot.tabId);
68 });
70​
71/**
72 * Opens a browser document in the integrated browser. Inside the workspace the
73 * page may load sibling assets; a file outside it is served on its own.
74 */
75export async function openFileInPreview<AssetError, PreviewError>(input: {
76 readonly threadRef: ScopedThreadRef;
77 readonly filePath: string;
78 readonly workspaceRoot: string | undefined;
79 readonly httpBaseUrl: string;
80 readonly createAssetUrl: (input: {
81 readonly environmentId: EnvironmentId;
82 readonly input: { readonly resource: AssetResource };
83 }) => Promise<AtomCommandResult<AssetCreateUrlResult, AssetError>>;
84 readonly openPreview: OpenPreviewMutation<PreviewError>;
85}): Promise<AtomCommandResult<void, AssetError | PreviewError | BrowserPreviewUnavailableError>> {
86 if (!isPreviewSupportedInRuntime()) {
87 return AsyncResult.failure(
88 Cause.fail(
89 new BrowserPreviewUnavailableError({
90 message: "The integrated browser is unavailable in this runtime.",
91 }),
92 ),
93 );
94 }
95 const insideWorkspace =
96 mediaFileReference(input.filePath, input.workspaceRoot).relativePath !== undefined;
97 const assetResult = await input.createAssetUrl({
98 environmentId: input.threadRef.environmentId,
99 input: {
100 resource: {
101 _tag: insideWorkspace ? "workspace-file" : "media-file",
102 threadId: input.threadRef.threadId,
103 path: input.filePath,
104 },
105 },
106 });
107 if (assetResult._tag === "Failure") {
108 return AsyncResult.failure(assetResult.cause);
109 }
110 const assetUrl = resolveAssetUrl(input.httpBaseUrl, assetResult.value.relativeUrl);
111 if (assetUrl === null) {
112 return AsyncResult.failure(
113 Cause.die(new Error("The environment returned an invalid asset URL.")),
114 );
115 }
116 return openUrlInPreview({
117 threadRef: input.threadRef,
118 url: assetUrl,
119 openPreview: input.openPreview,
120 });