TypeScript
fix(web): images, video, HTML and PDF preview in a thread before its first message (#17352)
Julius Marmingee0c80c198cfb
diff --git a/apps/web/src/components/files/FilePreviewPanel.test.ts b/apps/web/src/components/files/FilePreviewPanel.test.tsindex 84fb69e79cf..45304cb5863 100644--- a/apps/web/src/components/files/FilePreviewPanel.test.ts+++ b/apps/web/src/components/files/FilePreviewPanel.test.ts@@ -1,3 +1,4 @@+import type { ScopedThreadRef } from "@t3tools/contracts";import { ProjectReadFileError } from "@t3tools/contracts";import * as Schema from "effect/Schema";import { describe, expect, it } from "vite-plus/test";@@ -13,6 +14,7 @@ import {resolveFilePreviewPath,setMarkdownTaskChecked,shouldShowFileExplorer,+ workspaceAssetResource,} from "./filePreviewMode";const decodeReadError = Schema.decodeSync(ProjectReadFileError);@@ -189,3 +191,31 @@ describe("resolveFilePreviewPath", () => {}});});++describe("workspaceAssetResource", () => {+ const input = {+ kind: "workspace-file" as const,+ threadRef: {+ environmentId: "env" as ScopedThreadRef["environmentId"],+ threadId: "thread-1" as ScopedThreadRef["threadId"],+ },+ workspaceRoot: "/repo",+ absolutePath: "/repo/docs/index.html",+ };++ it("names the thread once the server knows it", () => {+ 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",+ });+ });+});diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsxindex be77a04d43e..efd7a7fbc90 100644--- a/apps/web/src/components/files/FilePreviewPanel.tsx+++ b/apps/web/src/components/files/FilePreviewPanel.tsx@@ -106,6 +106,7 @@ import {resolveFilePreviewPath,setMarkdownTaskChecked,shouldShowFileExplorer,+ workspaceAssetResource,} from "./filePreviewMode";import { useFileSaveCoordinator } from "./useFileSaveCoordinator";import {@@ -147,18 +148,23 @@ type FilePostRender = <LAnnotation>(function WorkspaceImagePreview(props: {readonly environmentId: EnvironmentId;readonly threadRef: ScopedThreadRef;+ /** The thread is a draft the server does not know yet. */+ readonly draft: boolean;readonly absolutePath: string;readonly workspaceRoot: string;readonly alt: string;readonly workspaceMutationId: string | null;}) {const resource = useMemo(- () => ({- _tag: "workspace-file" as const,- threadId: props.threadRef.threadId,- path: props.absolutePath,- }),- [props.threadRef.threadId, props.absolutePath],+ () =>+ workspaceAssetResource({+ kind: "workspace-file",+ threadRef: props.threadRef,+ draft: props.draft,+ workspaceRoot: props.workspaceRoot,+ absolutePath: props.absolutePath,+ }),+ [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],);const assetUrl = useAssetUrlState(props.environmentId, resource);const [failedUrl, setFailedUrl] = useState<string | null>(null);@@ -213,6 +219,8 @@ function WorkspaceImagePreview(props: {function WorkspaceBrowserPreview(props: {readonly environmentId: EnvironmentId;readonly threadRef: ScopedThreadRef;+ /** The thread is a draft the server does not know yet. */+ readonly draft: boolean;readonly absolutePath: string;readonly workspaceRoot: string;readonly title: string;@@ -221,12 +229,15 @@ function WorkspaceBrowserPreview(props: {const insideWorkspace =mediaFileReference(props.absolutePath, props.workspaceRoot).relativePath !== undefined;const resource = useMemo(- () => ({- _tag: insideWorkspace ? ("workspace-file" as const) : ("media-file" as const),- threadId: props.threadRef.threadId,- path: props.absolutePath,- }),- [insideWorkspace, props.threadRef.threadId, props.absolutePath],+ () =>+ workspaceAssetResource({+ kind: insideWorkspace ? "workspace-file" : "media-file",+ threadRef: props.threadRef,+ draft: props.draft,+ workspaceRoot: props.workspaceRoot,+ absolutePath: props.absolutePath,+ }),+ [insideWorkspace, props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],);const assetUrl = useAssetUrlState(props.environmentId, resource);const revisionSuffix =@@ -260,18 +271,23 @@ function WorkspaceBrowserPreview(props: {function WorkspaceVideoPreview(props: {readonly environmentId: EnvironmentId;readonly threadRef: ScopedThreadRef;+ /** The thread is a draft the server does not know yet. */+ readonly draft: boolean;readonly absolutePath: string;readonly workspaceRoot: string;readonly name: string;readonly workspaceMutationId: string | null;}) {const resource = useMemo(- () => ({- _tag: "media-file" as const,- threadId: props.threadRef.threadId,- path: props.absolutePath,- }),- [props.threadRef.threadId, props.absolutePath],+ () =>+ workspaceAssetResource({+ kind: "media-file",+ threadRef: props.threadRef,+ draft: props.draft,+ workspaceRoot: props.workspaceRoot,+ absolutePath: props.absolutePath,+ }),+ [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],);const assetUrl = useAssetUrlState(props.environmentId, resource);const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);@@ -314,17 +330,23 @@ function WorkspaceVideoPreview(props: {function WorkspaceAudioPreview(props: {readonly environmentId: EnvironmentId;readonly threadRef: ScopedThreadRef;+ /** The thread is a draft the server does not know yet. */+ readonly draft: boolean;readonly absolutePath: string;+ readonly workspaceRoot: string;readonly name: string;readonly workspaceMutationId: string | null;}) {const resource = useMemo(- () => ({- _tag: "media-file" as const,- threadId: props.threadRef.threadId,- path: props.absolutePath,- }),- [props.threadRef.threadId, props.absolutePath],+ () =>+ workspaceAssetResource({+ kind: "media-file",+ threadRef: props.threadRef,+ draft: props.draft,+ workspaceRoot: props.workspaceRoot,+ absolutePath: props.absolutePath,+ }),+ [props.threadRef, props.draft, props.workspaceRoot, props.absolutePath],);const assetUrl = useAssetUrlState(props.environmentId, resource);const refreshAssetUrl = useAssetUrlRefresh(props.environmentId, resource);@@ -1022,6 +1044,8 @@ export default function FilePreviewPanel({}: FilePreviewPanelProps) {const relativePath =attachment === undefined ? resolveFilePreviewPath(requestedPath, cwd) : requestedPath;+ // A draft's composer target is its draft id; a thread the server knows is a ref.+ const draft = typeof composerDraftTarget === "string";const { resolvedTheme } = useTheme();const wordWrap = useClientSettings((settings) => settings.wordWrap);const canOperatePreview = useEnvironmentScope(environmentId, AuthPreviewOperateScope);@@ -1334,6 +1358,7 @@ export default function FilePreviewPanel({key={`${environmentId}:${threadRef.threadId}:${absolutePath}`}environmentId={environmentId}threadRef={threadRef}+ draft={draft}absolutePath={absolutePath}workspaceRoot={cwd}name={relativePath}@@ -1344,7 +1369,9 @@ export default function FilePreviewPanel({key={`${environmentId}:${threadRef.threadId}:${absolutePath}`}environmentId={environmentId}threadRef={threadRef}+ draft={draft}absolutePath={absolutePath}+ workspaceRoot={cwd}name={relativePath}workspaceMutationId={workspaceMutationId}/>@@ -1353,6 +1380,7 @@ export default function FilePreviewPanel({key={absolutePath}environmentId={environmentId}threadRef={threadRef}+ draft={draft}absolutePath={absolutePath}workspaceRoot={cwd}alt={relativePath}@@ -1363,6 +1391,7 @@ export default function FilePreviewPanel({key={absolutePath}environmentId={environmentId}threadRef={threadRef}+ draft={draft}absolutePath={absolutePath}workspaceRoot={cwd}title={relativePath}diff --git a/apps/web/src/components/files/filePreviewMode.ts b/apps/web/src/components/files/filePreviewMode.tsindex 151855404dd..3d1ad62b68c 100644--- a/apps/web/src/components/files/filePreviewMode.ts+++ b/apps/web/src/components/files/filePreviewMode.ts@@ -1,4 +1,4 @@-import type { ProjectReadFileError } from "@t3tools/contracts";+import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts";import { isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";/** Resolve workspace links before choosing between the explorer and a file preview. */@@ -57,3 +57,20 @@ export function setMarkdownTaskChecked(return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;}++/**+ * The asset for a file the panel shows. A draft has no thread on the server+ * yet, so it names its workspace root instead of a thread to resolve one from.+ */+export function workspaceAssetResource(input: {+ readonly kind: "workspace-file" | "media-file";+ readonly threadRef: ScopedThreadRef;+ readonly draft: boolean;+ readonly workspaceRoot: string;+ readonly absolutePath: string;+}): AssetResource {+ 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 };+}