TypeScript
fix(web): preview images outside the workspace (#24627)
Julius Marminge27805ef76e4f
diff --git a/apps/web/src/components/files/FilePreviewPanel.test.ts b/apps/web/src/components/files/FilePreviewPanel.test.tsindex e8aee4c3ecb..ec543e006a8 100644--- a/apps/web/src/components/files/FilePreviewPanel.test.ts+++ b/apps/web/src/components/files/FilePreviewPanel.test.ts@@ -110,11 +110,26 @@ describe("workspaceAssetResource", () => {absolutePath: "/repo/docs/index.html",};+ // regression: #21067 images outside the workspace load through the host media resource+ it("serves a file outside the workspace as host media and keeps workspace files on workspace access", () => {+ expect(+ workspaceAssetResource({ ...input, absolutePath: "/tmp/screenshots/shot.png", draft: false }),+ ).toEqual({ _tag: "media-file", threadId: "thread-1", path: "/tmp/screenshots/shot.png" });+ expect(workspaceAssetResource({ ...input, draft: false })).toEqual({+ _tag: "workspace-file",+ threadId: "thread-1",+ path: "/repo/docs/index.html",+ });+ });+it("names the workspace root for a draft, which the server cannot resolve from a thread", () => {expect(workspaceAssetResource({ ...input, draft: true })).toEqual({_tag: "draft-workspace-file",cwd: "/repo",path: "/repo/docs/index.html",});+ expect(+ workspaceAssetResource({ ...input, absolutePath: "/tmp/screenshots/shot.png", draft: true }),+ ).toEqual({ _tag: "draft-workspace-file", cwd: "/repo", path: "/tmp/screenshots/shot.png" });});});diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsxindex a0ba43b931c..29f7b9c1db6 100644--- a/apps/web/src/components/files/FilePreviewPanel.tsx+++ b/apps/web/src/components/files/FilePreviewPanel.tsx@@ -225,10 +225,8 @@ function WorkspaceBrowserPreview(props: {readonly title: string;readonly workspaceMutationId: string | null;}) {- const insideWorkspace =- mediaFileReference(props.absolutePath, props.workspaceRoot).relativePath !== undefined;const resource = workspaceAssetResource({- kind: insideWorkspace ? "workspace-file" : "media-file",+ kind: "workspace-file",threadRef: props.threadRef,draft: props.draft,workspaceRoot: props.workspaceRoot,diff --git a/apps/web/src/components/files/filePreviewMode.ts b/apps/web/src/components/files/filePreviewMode.tsindex db39d06d2ee..bc6b0cd2040 100644--- a/apps/web/src/components/files/filePreviewMode.ts+++ b/apps/web/src/components/files/filePreviewMode.ts@@ -1,4 +1,5 @@import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts";+import { mediaFileReference } from "@t3tools/client-runtime/media-reference";import { isHostPath, workspaceRelativeFilePath } from "@t3tools/shared/path";export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {@@ -55,6 +56,11 @@ export function setMarkdownTaskChecked(return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;}+/**+ * The server only signs `workspace-file` assets below the workspace root, so a+ * file opened from outside it, such as an absolute link in the thread, goes+ * through the host `media-file` resource the direct media viewer uses.+ */export function workspaceAssetResource(input: {readonly kind: "workspace-file" | "media-file";readonly threadRef: ScopedThreadRef;@@ -65,5 +71,11 @@ export function workspaceAssetResource(input: {if (input.draft) {return { _tag: "draft-workspace-file", cwd: input.workspaceRoot, path: input.absolutePath };}- return { _tag: input.kind, threadId: input.threadRef.threadId, path: input.absolutePath };+ const insideWorkspace =+ mediaFileReference(input.absolutePath, input.workspaceRoot).relativePath !== undefined;+ return {+ _tag: insideWorkspace ? input.kind : "media-file",+ threadId: input.threadRef.threadId,+ path: input.absolutePath,+ };}