Files
t3code/apps/web/src/browser/openFileInPreview.ts
T
Julius Marminge 85d5df6256 Unify browser asset preview routing
- Replace attachment and favicon routes with signed asset URLs
- Harden workspace and attachment asset resolution
- Update browser preview components and shared contracts
2026-06-13 22:18:37 -07:00

37 lines
1.4 KiB
TypeScript

import type { ScopedThreadRef } from "@t3tools/contracts";
import { readEnvironmentApi } from "~/environmentApi";
import { resolveAssetUrl } from "~/assets/assetUrls";
import { isPreviewSupportedInRuntime, usePreviewStateStore } from "~/previewStateStore";
import { useRightPanelStore } from "~/rightPanelStore";
export const isBrowserPreviewFile = (path: string): boolean =>
/\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? "");
export async function openUrlInPreview(threadRef: ScopedThreadRef, url: string): Promise<void> {
const api = readEnvironmentApi(threadRef.environmentId);
if (!api) {
throw new Error("Environment is not connected.");
}
const snapshot = await api.preview.open({ threadId: threadRef.threadId, url });
usePreviewStateStore.getState().applyServerSnapshot(threadRef, snapshot);
usePreviewStateStore.getState().rememberUrl(threadRef, url);
useRightPanelStore.getState().openBrowser(threadRef, snapshot.tabId);
}
export async function openFileInPreview(
threadRef: ScopedThreadRef,
filePath: string,
): Promise<void> {
if (!isPreviewSupportedInRuntime()) {
throw new Error("The integrated browser is unavailable in this runtime.");
}
const asset = await resolveAssetUrl(threadRef.environmentId, {
_tag: "workspace-file",
threadId: threadRef.threadId,
path: filePath,
});
await openUrlInPreview(threadRef, asset.url);
}